Skip to content

Commit

Permalink
feat: track total hits in ES7 if configured (#124)
Browse files Browse the repository at this point in the history
* feat: track total hits in ES7 if configured

* address comments

* no need to set the const

* remove query template
  • Loading branch information
jywadhwani authored Aug 6, 2021
1 parent d379e9f commit 89a52f7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@
public class ESBrowseDAO extends BaseBrowseDAO {
private final RestHighLevelClient _client;
private final BaseBrowseConfig _config;
private int _lowerBoundHits = Integer.MAX_VALUE;

public ESBrowseDAO(@Nonnull RestHighLevelClient esClient, @Nonnull BaseBrowseConfig config) {
this._client = esClient;
this._config = config;
}

/**
* Set "track_total_hits" query parameter to a custom lower bound if you do not need accurate results. It is a good
* trade off to speed up searches if you don’t need the accurate number of hits after a certain threshold.
*/
public void setTrackTotalHits(int lowermost) {
_lowerBoundHits = lowermost;
}

/**
* Gets a list of groups/entities that match given browse request.
*
Expand Down Expand Up @@ -110,6 +119,8 @@ private AggregationBuilder buildAggregations(@Nonnull String path) {
protected SearchRequest constructGroupsSearchRequest(@Nonnull String path, @Nonnull Map<String, String> requestMap) {
final SearchRequest searchRequest = new SearchRequest(_config.getIndexName());
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHitsUpTo(_lowerBoundHits);

searchSourceBuilder.query(buildQueryString(path, requestMap, true));
searchSourceBuilder.aggregation(buildAggregations(path));
searchRequest.source(searchSourceBuilder);
Expand Down Expand Up @@ -171,6 +182,9 @@ SearchRequest constructEntitiesSearchRequest(@Nonnull String path, @Nonnull Map<
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);

searchSourceBuilder.trackTotalHitsUpTo(_lowerBoundHits);

searchSourceBuilder.fetchSource(new String[]{_config.getBrowsePathFieldName(), _config.getUrnFieldName()}, null);
searchSourceBuilder.sort(_config.getSortingField(), SortOrder.ASC);
searchSourceBuilder.query(buildQueryString(path, requestMap, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ESSearchDAO<DOCUMENT extends RecordTemplate> extends BaseSearchDAO<
private BaseESAutoCompleteQuery _autoCompleteQueryForLowCardFields;
private BaseESAutoCompleteQuery _autoCompleteQueryForHighCardFields;
private int _maxTermBucketSize = DEFAULT_TERM_BUCKETS_SIZE_100;
private int _lowerBoundHits = Integer.MAX_VALUE;

// Regex patterns for matching original field names to the highlighted field name returned by elasticsearch
private Map<String, Pattern> _highlightedFieldNamePatterns;
Expand All @@ -94,6 +95,14 @@ public ESSearchDAO(@Nonnull RestHighLevelClient esClient, @Nonnull Class<DOCUMEN
.collect(Collectors.toMap(Function.identity(), fieldName -> Pattern.compile(fieldName + "(\\..+)?")));
}

/**
* Set "track_total_hits" query parameter to a custom lower bound if you do not need accurate results. It is a good
* trade off to speed up searches if you don’t need the accurate number of hits after a certain threshold.
*/
public void setTrackTotalHits(int lowermost) {
_lowerBoundHits = lowermost;
}

@Nonnull
protected BaseESAutoCompleteQuery getAutocompleteQueryGenerator(@Nonnull String field) {
if (_config.getLowCardinalityFields() != null && _config.getLowCardinalityFields().contains(field)) {
Expand Down Expand Up @@ -200,6 +209,7 @@ SearchRequest getFilteredSearchQuery(@Nullable Filter filters, @Nullable SortCri
}
final SearchRequest searchRequest = new SearchRequest(_config.getIndexName());
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHitsUpTo(_lowerBoundHits);
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.from(from).size(size);
ESUtils.buildSortOrder(searchSourceBuilder, sortCriterion);
Expand Down Expand Up @@ -252,6 +262,8 @@ SearchRequest constructSearchQuery(@Nonnull String input, @Nullable Filter filte
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);

searchSourceBuilder.trackTotalHitsUpTo(_lowerBoundHits);

searchSourceBuilder.query(buildQueryString(input));
searchSourceBuilder.postFilter(ESUtils.buildFilterQuery(filter));
buildAggregations(searchSourceBuilder, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ public void testFilteredQueryWithTermsFilter() throws IOException {
assertEquals(searchRequest.indices(), new String[]{_testSearchConfig.getIndexName()});
}

@Test
public void testFilteredQueryApproxHits() throws IOException {
int from = 0;
int size = 10;
_searchDAO.setTrackTotalHits(100);
Filter filter = newFilter(ImmutableMap.of("key1", "value1, value2 ", "key2", "urn:li:entity:(foo,bar,baz)"));
SortCriterion sortCriterion = new SortCriterion().setOrder(SortOrder.ASCENDING).setField("urn");

SearchRequest searchRequest = _searchDAO.getFilteredSearchQuery(filter, sortCriterion, from, size);
assertEquals(searchRequest.source().toString(), loadJsonFromResource("UrnFilterApproxHitsQuery.json"));
assertEquals(searchRequest.indices(), new String[]{_testSearchConfig.getIndexName()});

// set the value of "track_total_hits" back to the default i.e. maximum integer value
_searchDAO.setTrackTotalHits(Integer.MAX_VALUE);
}

@Test
public void testFilteredQueryWithUrnValue() throws IOException {
int from = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"order" : "asc"
}
}
]
],
"track_total_hits" : 2147483647
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"order" : "asc"
}
}
]
],
"track_total_hits" : 2147483647
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
"order" : "asc"
}
}
]
],
"track_total_hits" : 2147483647
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"order" : "asc"
}
}
]
],
"track_total_hits" : 2147483647
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"from" : 0,
"size" : 10,
"query" : {
"bool" : {
"filter" : [
{
"terms" : {
"key1" : [
"value1",
"value2"
],
"boost" : 1.0
}
},
{
"terms" : {
"key2" : [
"urn:li:entity:(foo,bar,baz)"
],
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"sort" : [
{
"urn" : {
"order" : "asc"
}
}
],
"track_total_hits" : 100
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"order" : "asc"
}
}
]
],
"track_total_hits" : 2147483647
}

0 comments on commit 89a52f7

Please sign in to comment.