Skip to content

Apache Lucene open-source search software

SOLR-497: Added date faceting to QueryResponse

Grant Ingersollb331ca07fabf
diff --git a/CHANGES.txt b/CHANGES.txt
index feb3ae1873f..452e9b3d136 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -214,6 +214,8 @@ New Features
42. SOLR-494: Added cool admin Ajaxed schema explorer.
(Greg Ludington via ehatcher)
+
+43. SOLR-497: Added date faceting to the QueryResponse in SolrJ and QueryResponseTest (Shalin Shekhar Mangar via gsingers)
Changes in runtime behavior
diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/response/FacetField.java b/client/java/solrj/src/org/apache/solr/client/solrj/response/FacetField.java
index 42ec2ab0bd7..bbfaa479e2a 100644
--- a/client/java/solrj/src/org/apache/solr/client/solrj/response/FacetField.java
+++ b/client/java/solrj/src/org/apache/solr/client/solrj/response/FacetField.java
@@ -19,6 +19,7 @@ package org.apache.solr.client.solrj.response;
import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
import org.apache.solr.client.solrj.util.ClientUtils;
@@ -86,11 +87,37 @@ import org.apache.solr.client.solrj.util.ClientUtils;
private String _name = null;
private List<Count> _values = null;
+ private String _gap = null;
+ private Date _end = null;
public FacetField( final String n )
{
_name = n;
}
+
+ public FacetField(String name, String gap, Date end) {
+ _name = name;
+ _gap = gap;
+ _end = end;
+ }
+
+ /**
+ * Date Gap Facet parameter
+ *
+ * @return the value specified for facet.date.gap
+ */
+ public String getGap() {
+ return _gap;
+ }
+
+ /**
+ * Date End Facet parameter
+ *
+ * @return the value specified for facet.date.end
+ */
+ public Date getEnd() {
+ return _end;
+ }
/**
* Insert at the end of the list
diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/response/QueryResponse.java b/client/java/solrj/src/org/apache/solr/client/solrj/response/QueryResponse.java
index 4083d147f79..5f773479413 100644
--- a/client/java/solrj/src/org/apache/solr/client/solrj/response/QueryResponse.java
+++ b/client/java/solrj/src/org/apache/solr/client/solrj/response/QueryResponse.java
@@ -18,6 +18,7 @@
package org.apache.solr.client.solrj.response;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -45,6 +46,7 @@ public class QueryResponse extends SolrResponseBase
private Map<String,Integer> _facetQuery = null;
private List<FacetField> _facetFields = null;
private List<FacetField> _limitingFacets = null;
+ private List<FacetField> _facetDates = null;
// Highlight Info
private Map<String,Map<String,List<String>>> _highlighting = null;
@@ -143,6 +145,30 @@ public class QueryResponse extends SolrResponseBase
}
}
}
+
+ //Parse date facets
+ NamedList<NamedList<Object>> df = (NamedList<NamedList<Object>>) info.get("facet_dates");
+ if (df != null) {
+ // System.out.println(df);
+ _facetDates = new ArrayList<FacetField>( df.size() );
+ for (Map.Entry<String, NamedList<Object>> facet : df) {
+ // System.out.println("Key: " + facet.getKey() + " Value: " + facet.getValue());
+ NamedList<Object> values = facet.getValue();
+ String gap = (String) values.get("gap");
+ Date end = (Date) values.get("end");
+ FacetField f = new FacetField(facet.getKey(), gap, end);
+
+ for (Map.Entry<String, Object> entry : values) {
+ try {
+ f.add(entry.getKey(), Long.parseLong(entry.getValue().toString()));
+ } catch (NumberFormatException e) {
+ //Ignore for non-number responses which are already handled above
+ }
+ }
+
+ _facetDates.add(f);
+ }
+ }
}
//------------------------------------------------------
@@ -186,6 +212,10 @@ public class QueryResponse extends SolrResponseBase
return _facetFields;
}
+ public List<FacetField> getFacetDates() {
+ return _facetDates;
+ }
+
/** get
*
* @param name the name of the
@@ -199,6 +229,15 @@ public class QueryResponse extends SolrResponseBase
return null;
}
+ public FacetField getFacetDate(String name) {
+ if (_facetDates == null)
+ return null;
+ for (FacetField f : _facetDates)
+ if (f.getName().equals(name))
+ return f;
+ return null;
+ }
+
public List<FacetField> getLimitingFacets() {
return _limitingFacets;
}
diff --git a/client/java/solrj/test/org/apache/solr/client/solrj/response/QueryResponseTest.java b/client/java/solrj/test/org/apache/solr/client/solrj/response/QueryResponseTest.java
new file mode 100644
index 00000000000..0e7fbd76f7f
--- /dev/null
+++ b/client/java/solrj/test/org/apache/solr/client/solrj/response/QueryResponseTest.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.response;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.FileReader;
+
+import junit.framework.Assert;
+
+import org.apache.solr.client.solrj.impl.XMLResponseParser;
+import org.apache.solr.common.util.NamedList;
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Simple test for Date facet support in QueryResponse
+ *
+ * @since solr 1.3
+ */
+public class QueryResponseTest {
+ @Test
+ public void testDateFacets() throws Exception {
+ XMLResponseParser parser = new XMLResponseParser();
+ FileReader in = new FileReader("sampleDateFacetResponse.xml");
+ assertTrue("in is null and it shouldn't be", in != null);
+ NamedList<Object> response = parser.processResponse(in);
+ in.close();
+
+ QueryResponse qr = new QueryResponse(response);
+ Assert.assertNotNull(qr);
+
+ Assert.assertNotNull(qr.getFacetDates());
+
+ for (FacetField f : qr.getFacetDates()) {
+ Assert.assertNotNull(f);
+ System.out.println(f.toString());
+ System.out.println("GAP: " + f.getGap());
+ System.out.println("END: " + f.getEnd());
+ }
+ }
+}
diff --git a/src/test/test-files/sampleDateFacetResponse.xml b/src/test/test-files/sampleDateFacetResponse.xml
new file mode 100644
index 00000000000..12e32c2f2b3
--- /dev/null
+++ b/src/test/test-files/sampleDateFacetResponse.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<response>
+<lst name="responseHeader"><int name="status">0</int><int name="QTime">0</int><lst name="params"><str name="facet.date.start">NOW/DAY-5DAYS</str><str name="facet">true</str><str name="facet.date.hardend">true</str><str name="q">*:*</str><arr name="facet.date"><str>timestamp</str><str>timestamp2</str></arr><str name="facet.date.gap">+1DAY</str><str name="facet.date.other">ALL</str><str name="facet.date.end">NOW/DAY+1DAY</str><str name="rows">0</str></lst></lst><result name="response" numFound="16" start="0"/><lst name="facet_counts"><lst name="facet_queries"/><lst name="facet_fields"/><lst name="facet_dates"><lst name="timestamp"><int name="2008-03-06T00:00:00.000Z">0</int><int name="2008-03-07T00:00:00.000Z">0</int><int name="2008-03-08T00:00:00.000Z">0</int><int name="2008-03-09T00:00:00.000Z">0</int><int name="2008-03-10T00:00:00.000Z">0</int><int name="2008-03-11T00:00:00.000Z">0</int><str name="gap">+1DAY</str><date name="end">2008-03-12T00:00:00Z</date><int name="before">16</int><int name="after">0</int><int name="between">0</int></lst><lst name="timestamp2"><int name="2008-03-06T00:00:00.000Z">0</int><int name="2008-03-07T00:00:00.000Z">0</int><int name="2008-03-08T00:00:00.000Z">0</int><int name="2008-03-09T00:00:00.000Z">0</int><int name="2008-03-10T00:00:00.000Z">0</int><int name="2008-03-11T00:00:00.000Z">0</int><str name="gap">+1DAY</str><date name="end">2008-03-12T00:00:00Z</date><int name="before">0</int><int name="after">0</int><int name="between">0</int></lst></lst></lst>
+</response>