Add 'prelim_sort' helper to SolrJ's TermsFacetMap

This commit is contained in:
Jason Gerlowski 2018-12-04 12:49:02 -05:00
parent 868e005760
commit a5fb6f8adc
2 changed files with 49 additions and 0 deletions

View File

@ -67,6 +67,12 @@ public class TermsFacetMap extends JsonFacetMap<TermsFacetMap> {
*
* Values can be based on 'count' (the number of results in each bucket), 'index' (the natural order of bucket values),
* or on any stat facet that occurs in the bucket. Defaults to "count desc" if not specified.
*
* By default, {@code sort} is calculated for all buckets generated by all shards. If {@code sort} is expensive a
* cheaper approximation can be provided using {@link #setPreliminarySort(String)} that will be run instead during
* bucket collection.
*
* @see TermsFacetMap#setPreliminarySort(String)
*/
public TermsFacetMap setSort(String sortString) {
if (sortString == null) {
@ -76,6 +82,26 @@ public class TermsFacetMap extends JsonFacetMap<TermsFacetMap> {
return this;
}
/**
* Indicates an approximate sort calculation to be performed during initial bucket generation and collection.
*
* Values can be based on 'count' (the number of results in each bucket), 'index' (the natural order of bucket values),
* or on any stat facet that occurs in the bucket. Defaults to "count desc" if not specified.
*
* When no {@code prelim_sort} is provided, {@code sort} is calculated on all buckets generated by all shards. If
* {@code sort} is expensive, {@code prelim_sort} can be used to provide a cheaper approximation calculation that is
* run instead on initial bucket collection. {@code sort} is still used when assembling the final list of buckets.
*
* @see TermsFacetMap#setSort(String)
*/
public TermsFacetMap setPreliminarySort(String preliminarySort) {
if (preliminarySort == null) {
throw new IllegalArgumentException("Parameter 'preliminarySort' must be non-null");
}
put("prelim_sort", preliminarySort);
return this;
}
/**
* Indicates the number of additional buckets to request internally beyond those required by {@link #setLimit(int)}.
*

View File

@ -92,6 +92,29 @@ public class JsonQueryRequestFacetingIntegrationTest extends SolrCloudTestCase {
new FacetBucket("memory", NUM_MEMORY));
}
@Test
public void testTermsFacetWithPrelimSort() throws Exception {
final TermsFacetMap categoriesFacetMap = new TermsFacetMap("cat")
.setPreliminarySort("count desc")
.setSort("index desc")
.setLimit(3);
final JsonQueryRequest request = new JsonQueryRequest()
.setQuery("*:*")
.withFacet("top_cats", categoriesFacetMap);
QueryResponse response = request.process(cluster.getSolrClient(), COLLECTION_NAME);
assertExpectedDocumentsFoundAndReturned(response, NUM_TECHPRODUCTS_DOCS, 10);
final NestableJsonFacet topLevelFacetData = response.getJsonFacetingResponse();
assertEquals(NUM_TECHPRODUCTS_DOCS, topLevelFacetData.getCount());
//The prelim_sort/sort combination should give us the 3 most popular categories in reverse-alpha order
assertHasFacetWithBucketValues(topLevelFacetData, "top_cats",
new FacetBucket("memory", NUM_MEMORY),
new FacetBucket("electronics", NUM_ELECTRONICS),
new FacetBucket("currency", NUM_CURRENCY));
}
@Test
public void testTermsFacetWithNumBucketsRequested() throws Exception {
final TermsFacetMap categoriesFacetMap = new TermsFacetMap("cat")