Skip to content

Commit

Permalink
Add missing min doc count param to terms agg (#974)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Burkholder <[email protected]>
  • Loading branch information
bryanlb and bryanlb authored Jun 28, 2024
1 parent cb89d84 commit 7c5890c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private static List<AstraSearch.SearchRequest.SearchAggregation> getRecursive(Js
.ValueSourceAggregation.TermsAggregation.newBuilder()
.setSize(getSize(terms))
.putAllOrder(getTermsOrder(terms))
.setMinDocCount(getTermsMinDocCount(terms))
.build())
.build());
} else if (aggregationObject.equals(AvgAggBuilder.TYPE)) {
Expand Down Expand Up @@ -579,6 +580,11 @@ private static long getHistogramMinDocCount(JsonNode dateHistogram) {
return Long.parseLong(dateHistogram.get("min_doc_count").asText());
}

private static long getTermsMinDocCount(JsonNode terms) {
// min_doc_count is provided as a string in the json payload
return Long.parseLong(terms.get("min_doc_count").asText());
}

private static Map<String, Long> getDateHistogramExtendedBounds(JsonNode dateHistogram) {
if (dateHistogram.has("extended_bounds")
&& dateHistogram.get("extended_bounds").has("min")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,25 @@ public void testFilters() throws IOException {
assertThat(filtersAggregation.getFilters().getFiltersMap().get("bar").getAnalyzeWildcard())
.isEqualTo(false);
}

@Test
public void testTermsAggregation() throws IOException {
String rawRequest = getRawQueryString("terms");

OpenSearchRequest openSearchRequest = new OpenSearchRequest();
List<AstraSearch.SearchRequest> parsedRequestList =
openSearchRequest.parseHttpPostBody(rawRequest);
assertThat(parsedRequestList.size()).isEqualTo(1);

AstraSearch.SearchRequest.SearchAggregation rawTermsAggregation =
parsedRequestList.get(0).getAggregations();

AstraSearch.SearchRequest.SearchAggregation.ValueSourceAggregation.TermsAggregation
typedTermsAggregation = rawTermsAggregation.getValueSource().getTerms();

assertThat(rawTermsAggregation.getValueSource().getField()).isEqualTo("host");
assertThat(typedTermsAggregation.getMinDocCount()).isEqualTo(1);
assertThat(typedTermsAggregation.getSize()).isEqualTo(10);
assertThat(typedTermsAggregation.getOrderMap().get("_term")).isEqualTo("desc");
}
}
2 changes: 2 additions & 0 deletions astra/src/test/resources/opensearchRequest/terms.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"search_type":"query_then_fetch","ignore_unavailable":true,"index":"_all"}
{"size":0,"query":{"bool":{"filter":[{"range":{"_timesinceepoch":{"gte":1683041914647,"lte":1683045514647,"format":"epoch_millis"}}},{"query_string":{"analyze_wildcard":true,"query":"*"}}]}},"aggs":{"2":{"terms":{"field":"host","size":10,"order":{"_term":"desc"},"min_doc_count":1}}}}

0 comments on commit 7c5890c

Please sign in to comment.