Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Terms Method to Support Ascending and Descending Ordering #3540

Merged
merged 3 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,19 @@ public SearchResult search(SearchesConfig config) {
return new SearchResult(r.getHits(), indices, config.query(), request.source(), r.getTook());
}

public TermsResult terms(String field, int size, String query, String filter, TimeRange range) {
public TermsResult terms(String field, int size, String query, String filter, TimeRange range, Sorting.Direction sorting) {
Terms.Order termsOrder;

if (size == 0) {
size = 50;
}

if (sorting == Sorting.Direction.DESC){
termsOrder = Terms.Order.count(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @joschi. I am using count because I am ordering based on the returned document count of the aggregation instead of the term itself.

Per the docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/_bucket_aggregations.html#_order

Ordering the buckets by their doc_count in an ascending manner:

AggregationBuilders
    .terms("genders")
    .field("gender")
    .order(Terms.Order.count(true))

} else {
termsOrder = Terms.Order.count(true);
}

SearchRequestBuilder srb;
if (filter == null) {
srb = standardSearchRequest(query, determineAffectedIndices(range, null), range);
Expand All @@ -279,7 +287,8 @@ public TermsResult terms(String field, int size, String query, String filter, Ti
.subAggregation(
AggregationBuilders.terms(AGG_TERMS)
.field(field)
.size(size))
.size(size)
.order(termsOrder))
.subAggregation(
AggregationBuilders.missing("missing")
.field(field))
Expand All @@ -302,8 +311,12 @@ public TermsResult terms(String field, int size, String query, String filter, Ti
);
}

public TermsResult terms(String field, int size, String query, String filter, TimeRange range) {
return terms(field, size, query, filter, range, Sorting.Direction.DESC);
}

public TermsResult terms(String field, int size, String query, TimeRange range) {
return terms(field, size, query, null, range);
return terms(field, size, query, null, range, Sorting.Direction.DESC);
}

public TermsStatsResult termsStats(String keyField, String valueField, TermsStatsOrder order, int size, String query, String filter, TimeRange range) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ public void termsRecordsMetrics() throws Exception {
assertThat(histogram.getSnapshot().getValues()).containsExactly(86400L);
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testTermsAscending() throws Exception {
TermsResult result = searches.terms("n", 1, "*", null, AbsoluteRange.create(
new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC),
new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC)), Sorting.Direction.ASC);

assertThat(result.getTotal()).isEqualTo(10L);
assertThat(result.getMissing()).isEqualTo(2L);
assertThat(result.getTerms())
.hasSize(1)
.containsEntry("4", 1L);
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testTermsStats() throws Exception {
Expand Down