Apache Lucene open-source search software
Add 'prelim_sort' helper to SolrJ's TermsFacetMap
Jason Gerlowskia5fb6f8adc6a
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/TermsFacetMap.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/TermsFacetMap.javaindex e28f8a83d16..ea7c2fd2c70 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/TermsFacetMap.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/TermsFacetMap.java@@ -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)}.*diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestFacetingIntegrationTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestFacetingIntegrationTest.javaindex 344fdd2de78..4852c76518b 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestFacetingIntegrationTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestFacetingIntegrationTest.java@@ -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));++ }+@Testpublic void testTermsFacetWithNumBucketsRequested() throws Exception {final TermsFacetMap categoriesFacetMap = new TermsFacetMap("cat")