-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing RareTerms aggregation response failed in RestHighLevelCli…
…ent (#64454) - Add LongRareTerms and StringRareTerms to the DefaultNamedXContents, ensure that the response of RareTerms aggregation can be parsed correctly. - Add testSearchWithRareTermsAgg method to test the response of RareTerms aggregation can be parsed correctly. - Add some test code to ensure the AggregationsTests can execute successfully.
- Loading branch information
1 parent
9fef6e7
commit e4a90d5
Showing
10 changed files
with
632 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...src/main/java/org/elasticsearch/search/aggregations/bucket/terms/ParsedLongRareTerms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms; | ||
|
||
|
||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
|
||
public class ParsedLongRareTerms extends ParsedRareTerms { | ||
@Override | ||
public String getType() { | ||
return LongRareTerms.NAME; | ||
} | ||
|
||
private static final ObjectParser<ParsedLongRareTerms, Void> PARSER = | ||
new ObjectParser<>(ParsedLongRareTerms.class.getSimpleName(), true, ParsedLongRareTerms::new); | ||
|
||
static { | ||
declareParsedTermsFields(PARSER, ParsedBucket::fromXContent); | ||
} | ||
|
||
public static ParsedLongRareTerms fromXContent(XContentParser parser, String name) throws IOException { | ||
ParsedLongRareTerms aggregation = PARSER.parse(parser, null); | ||
aggregation.setName(name); | ||
return aggregation; | ||
} | ||
|
||
public static class ParsedBucket extends ParsedRareTerms.ParsedBucket { | ||
|
||
private Long key; | ||
|
||
@Override | ||
public Object getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public String getKeyAsString() { | ||
String keyAsString = super.getKeyAsString(); | ||
if (keyAsString != null) { | ||
return keyAsString; | ||
} | ||
if (key != null) { | ||
return Long.toString(key); | ||
} | ||
return null; | ||
} | ||
|
||
public Number getKeyAsNumber() { | ||
return key; | ||
} | ||
|
||
@Override | ||
protected XContentBuilder keyToXContent(XContentBuilder builder) throws IOException { | ||
builder.field(CommonFields.KEY.getPreferredName(), key); | ||
if (super.getKeyAsString() != null) { | ||
builder.field(CommonFields.KEY_AS_STRING.getPreferredName(), getKeyAsString()); | ||
} | ||
return builder; | ||
} | ||
|
||
static ParsedLongRareTerms.ParsedBucket fromXContent(XContentParser parser) throws IOException { | ||
return parseRareTermsBucketXContent(parser, ParsedLongRareTerms.ParsedBucket::new, (p, bucket) -> bucket.key = p.longValue()); | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/ParsedRareTerms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms; | ||
|
||
import org.elasticsearch.common.CheckedBiConsumer; | ||
import org.elasticsearch.common.CheckedFunction; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.Aggregations; | ||
import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
public abstract class ParsedRareTerms extends ParsedMultiBucketAggregation<ParsedRareTerms.ParsedBucket> implements RareTerms { | ||
@Override | ||
public List<? extends RareTerms.Bucket> getBuckets() { | ||
return buckets; | ||
} | ||
|
||
@Override | ||
public RareTerms.Bucket getBucketByKey(String term) { | ||
for (RareTerms.Bucket bucket : getBuckets()) { | ||
if (bucket.getKeyAsString().equals(term)) { | ||
return bucket; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { | ||
builder.startArray(CommonFields.BUCKETS.getPreferredName()); | ||
for (RareTerms.Bucket bucket : getBuckets()) { | ||
bucket.toXContent(builder, params); | ||
} | ||
builder.endArray(); | ||
return builder; | ||
} | ||
|
||
static void declareParsedTermsFields(final ObjectParser<? extends ParsedRareTerms, Void> objectParser, | ||
final CheckedFunction<XContentParser, ParsedBucket, IOException> bucketParser) { | ||
declareMultiBucketAggregationFields(objectParser, bucketParser::apply, bucketParser::apply); | ||
} | ||
|
||
public abstract static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket implements RareTerms.Bucket { | ||
|
||
@Override | ||
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
keyToXContent(builder); | ||
builder.field(CommonFields.DOC_COUNT.getPreferredName(), getDocCount()); | ||
getAggregations().toXContentInternal(builder, params); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
|
||
static <B extends ParsedBucket> B parseRareTermsBucketXContent(final XContentParser parser, final Supplier<B> bucketSupplier, | ||
final CheckedBiConsumer<XContentParser, B, IOException> keyConsumer) | ||
throws IOException { | ||
|
||
final B bucket = bucketSupplier.get(); | ||
final List<Aggregation> aggregations = new ArrayList<>(); | ||
|
||
XContentParser.Token token; | ||
String currentFieldName = parser.currentName(); | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
} else if (token.isValue()) { | ||
if (CommonFields.KEY_AS_STRING.getPreferredName().equals(currentFieldName)) { | ||
bucket.setKeyAsString(parser.text()); | ||
} else if (CommonFields.KEY.getPreferredName().equals(currentFieldName)) { | ||
keyConsumer.accept(parser, bucket); | ||
} else if (CommonFields.DOC_COUNT.getPreferredName().equals(currentFieldName)) { | ||
bucket.setDocCount(parser.longValue()); | ||
} | ||
} else if (token == XContentParser.Token.START_OBJECT) { | ||
XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class, | ||
aggregations::add); | ||
} | ||
} | ||
bucket.setAggregations(new Aggregations(aggregations)); | ||
return bucket; | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...c/main/java/org/elasticsearch/search/aggregations/bucket/terms/ParsedStringRareTerms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms; | ||
|
||
import java.io.IOException; | ||
import java.nio.CharBuffer; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
public class ParsedStringRareTerms extends ParsedRareTerms { | ||
@Override | ||
public String getType() { | ||
return StringRareTerms.NAME; | ||
} | ||
|
||
private static final ObjectParser<ParsedStringRareTerms, Void> PARSER = | ||
new ObjectParser<>(ParsedStringRareTerms.class.getSimpleName(), true, ParsedStringRareTerms::new); | ||
|
||
static { | ||
declareParsedTermsFields(PARSER, ParsedBucket::fromXContent); | ||
} | ||
|
||
public static ParsedStringRareTerms fromXContent(XContentParser parser, String name) throws IOException { | ||
ParsedStringRareTerms aggregation = PARSER.parse(parser, null); | ||
aggregation.setName(name); | ||
return aggregation; | ||
} | ||
|
||
public static class ParsedBucket extends ParsedRareTerms.ParsedBucket { | ||
|
||
private BytesRef key; | ||
|
||
@Override | ||
public Object getKey() { | ||
return getKeyAsString(); | ||
} | ||
|
||
@Override | ||
public String getKeyAsString() { | ||
String keyAsString = super.getKeyAsString(); | ||
if (keyAsString != null) { | ||
return keyAsString; | ||
} | ||
if (key != null) { | ||
return key.utf8ToString(); | ||
} | ||
return null; | ||
} | ||
|
||
public Number getKeyAsNumber() { | ||
if (key != null) { | ||
return Double.parseDouble(key.utf8ToString()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected XContentBuilder keyToXContent(XContentBuilder builder) throws IOException { | ||
return builder.field(CommonFields.KEY.getPreferredName(), getKey()); | ||
} | ||
|
||
static ParsedStringRareTerms.ParsedBucket fromXContent(XContentParser parser) throws IOException { | ||
return parseRareTermsBucketXContent(parser, ParsedStringRareTerms.ParsedBucket::new, (p, bucket) -> { | ||
CharBuffer cb = p.charBufferOrNull(); | ||
if (cb == null) { | ||
bucket.key = null; | ||
} else { | ||
bucket.key = new BytesRef(cb); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.