Skip to content

Commit bede4c9

Browse files
animodak7Aniket Modak
andauthored
handled empty results in terms aggs (opensearch-project#20451)
Co-authored-by: Aniket Modak <animodak@amazon.com>
1 parent b4ff131 commit bede4c9

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

server/src/main/java/org/opensearch/search/aggregations/bucket/composite/CompositeAggregator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ public void collect(int doc, long zeroBucket) throws IOException {
735735

736736
@Override
737737
public List<InternalAggregation> convert(Map<String, Object[]> shardResult, SearchContext searchContext) {
738+
if(shardResult.isEmpty()) {
739+
return Collections.singletonList(buildEmptyAggregation());
740+
}
738741
// Generate the composite keys
739742
List<Comparable<?>> currentCompositeKey = new ArrayList<>(sourceConfigs.length);
740743
List<CompositeKey> compositeKeys = new ArrayList<>(shardResult.size());

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/AbstractStringTermsAggregator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ protected SignificantStringTerms buildEmptySignificantTermsAggregation(long subs
121121

122122
@Override
123123
public List<InternalAggregation> convert(Map<String, Object[]> shardResult, SearchContext searchContext) {
124+
if(shardResult.isEmpty()) {
125+
return Collections.singletonList(buildEmptyTermsAggregation());
126+
}
124127
int rowCount = shardResult.get(shardResult.keySet().stream().findFirst().get()).length;
125128
List<StringTerms.Bucket> buckets = new ArrayList<>(rowCount);
126129
for (int row = 0; row < rowCount; row++) {

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/NumericTermsAggregator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ DoubleTerms buildEmptyResult() {
641641

642642
@Override
643643
public List<InternalAggregation> convert(Map<String, Object[]> shardResult, SearchContext searchContext) {
644+
if(shardResult.isEmpty()) {
645+
return Collections.singletonList(buildEmptyAggregation());
646+
}
644647
int rowCount = shardResult.isEmpty() ? 0 : shardResult.get(name).length ;
645648
List<DoubleTerms.Bucket> buckets = new ArrayList<>(rowCount);
646649
for (int i = 0; i < rowCount; i++) {
@@ -875,6 +878,9 @@ public void close() {
875878

876879
@Override
877880
public List<InternalAggregation> convert(Map<String, Object[]> shardResult, SearchContext searchContext) {
881+
if(shardResult.isEmpty()) {
882+
return Collections.singletonList(buildEmptyAggregation());
883+
}
878884
if (resultStrategy instanceof ShardResultConvertor) {
879885
return ((ShardResultConvertor) resultStrategy).convert(shardResult, searchContext);
880886
} else {

server/src/main/java/org/opensearch/search/query/SearchEngineResultConversionUtils.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ public static void convertDFResultGeneric(SearchContext searchContext) {
6363
}
6464
}).toList();
6565

66-
InternalAggregations internalAggregations = InternalAggregations.EMPTY;
67-
68-
if(searchContext.getDFResults().isEmpty() == false) {
69-
internalAggregations = InternalAggregations.from(
66+
InternalAggregations internalAggregations = InternalAggregations.from(
7067
shardResultConvertors.stream().flatMap(x -> x.convert(dfResult, searchContext).stream()).collect(Collectors.toList())
7168
);
72-
}
69+
7370
//LOGGER.info("Converted DF result to internal aggregations: {}", internalAggregations.asList());
7471
searchContext.queryResult().aggregations(internalAggregations);
7572
} catch (IOException e) {

0 commit comments

Comments
 (0)