lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject svn commit: r1351219 - in /lucene/dev/trunk/solr: CHANGES.txt core/src/java/org/apache/solr/request/SimpleFacets.java core/src/test/org/apache/solr/request/SimpleFacetsTest.java
Date Mon, 18 Jun 2012 07:16:41 GMT
Author: mvg
Date: Mon Jun 18 07:16:40 2012
New Revision: 1351219

URL: http://svn.apache.org/viewvc?rev=1351219&view=rev
Log:
SOLR-3406: Extended grouped faceting support to facet.query and facet.range parameters.

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/SimpleFacets.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1351219&r1=1351218&r2=1351219&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Mon Jun 18 07:16:40 2012
@@ -266,6 +266,9 @@ New Features
   (Russell Black, ryan)
 
 * SOLR-2898: Support grouped faceting. (Martijn van Groningen)
+  Additional Work:
+  SOLR-3406: Extended grouped faceting support to facet.query and facet.range parameters.
+             (David Boychuck, Martijn van Groningen)
 
 * SOLR-2949: QueryElevationComponent is now supported with distributed search.
   (Mark Miller, yonik)

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/SimpleFacets.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/SimpleFacets.java?rev=1351219&r1=1351218&r2=1351219&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/SimpleFacets.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/SimpleFacets.java Mon Jun
18 07:16:40 2012
@@ -22,6 +22,7 @@ import org.apache.lucene.queryparser.cla
 import org.apache.lucene.search.*;
 import org.apache.lucene.search.grouping.AbstractAllGroupHeadsCollector;
 import org.apache.lucene.search.grouping.term.TermGroupFacetCollector;
+import org.apache.lucene.search.grouping.term.TermAllGroupsCollector;
 import org.apache.lucene.util.*;
 import org.apache.lucene.util.packed.PackedInts;
 import org.apache.solr.common.SolrException;
@@ -233,19 +234,45 @@ public class SimpleFacets {
 
     String[] facetQs = params.getParams(FacetParams.FACET_QUERY);
 
+    
     if (null != facetQs && 0 != facetQs.length) {
       for (String q : facetQs) {
         parseParams(FacetParams.FACET_QUERY, q);
 
         // TODO: slight optimization would prevent double-parsing of any localParams
         Query qobj = QParser.getParser(q, null, req).getQuery();
-        res.add(key, searcher.numDocs(qobj, base));
+
+        if (params.getBool(GroupParams.GROUP_FACET, false)) {
+          res.add(key, getGroupedFacetQueryCount(qobj));
+        } else {
+          res.add(key, searcher.numDocs(qobj, base));
+        }
       }
     }
 
     return res;
   }
-
+  
+  /**
+   * Returns a grouped facet count for the facet query
+   *
+   * @see FacetParams#FACET_QUERY
+   */
+  public int getGroupedFacetQueryCount(Query facetQuery) throws IOException {
+    GroupingSpecification groupingSpecification = rb.getGroupingSpec();
+    String groupField  = groupingSpecification != null ? groupingSpecification.getFields()[0]
: null;
+    if (groupField == null) {
+      throw new SolrException (
+          SolrException.ErrorCode.BAD_REQUEST,
+          "Specify the group.field as parameter or local parameter"
+      );
+    }
+    
+    TermAllGroupsCollector collector = new TermAllGroupsCollector(groupField);
+    Filter mainQueryFilter = docs.getTopFilter(); // This returns a filter that only matches
documents matching with q param and fq params
+    searcher.search(facetQuery, mainQueryFilter, collector);
+    return collector.getGroupCount();
+  }
 
   public NamedList<Integer> getTermCounts(String field) throws IOException {
     int offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0);
@@ -1172,7 +1199,11 @@ public class SimpleFacets {
   protected int rangeCount(SchemaField sf, String low, String high,
                            boolean iLow, boolean iHigh) throws IOException {
     Query rangeQ = sf.getType().getRangeQuery(null, sf,low,high,iLow,iHigh);
-    return searcher.numDocs(rangeQ ,base);
+    if (params.getBool(GroupParams.GROUP_FACET, false)) {
+      return getGroupedFacetQueryCount(rangeQ);
+    } else {
+      return searcher.numDocs(rangeQ ,base);
+    }
   }
 
   /**

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java?rev=1351219&r1=1351218&r2=1351219&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java Mon
Jun 18 07:16:40 2012
@@ -26,14 +26,9 @@ import org.apache.solr.util.TimeZoneUtil
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.TimeZone;
 
 
 public class SimpleFacetsTest extends SolrTestCaseJ4 {
@@ -120,6 +115,42 @@ public class SimpleFacetsTest extends So
   }
 
   @Test
+  public void testSimpleGroupedQueryRangeFacets() throws Exception {
+    assertQ(
+        req(
+            "q", "*:*",
+            "fq", "id:[2000 TO 2004]",
+            "group", "true",
+            "group.facet", "true",
+            "group.field", "hotel_s1",
+            "facet", "true",
+            "facet.query", "airport_s1:ams"
+        ),
+        "//lst[@name='facet_queries']/int[@name='airport_s1:ams'][.='2']"
+    );
+    assertQ(
+        req(
+            "q", "*:*",
+            "fq", "id:[2000 TO 2004]",
+            "group", "true",
+            "group.facet", "true",
+            "group.field", "hotel_s1",
+            "facet", "true",
+            "facet.range", "duration_i1",
+            "facet.range.start", "5",
+            "facet.range.end", "11",
+            "facet.range.gap", "1"
+        ),
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='5'][.='2']",
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='6'][.='0']",
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='7'][.='0']",
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='8'][.='0']",
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='9'][.='0']",
+        "//lst[@name='facet_ranges']/lst[@name='duration_i1']/lst[@name='counts']/int[@name='10'][.='2']"
+    );
+  }
+
+  @Test
   public void testSimpleGroupedFacets() throws Exception {
     assertQ(
         "Return 5 docs with id range 1937 till 1940",



Mime
View raw message