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 _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 _facetQuery = null; private List _facetFields = null; private List _limitingFacets = null; + private List _facetDates = null; // Highlight Info private Map>> _highlighting = null; @@ -143,6 +145,30 @@ public class QueryResponse extends SolrResponseBase } } } + + //Parse date facets + NamedList> df = (NamedList>) info.get("facet_dates"); + if (df != null) { + // System.out.println(df); + _facetDates = new ArrayList( df.size() ); + for (Map.Entry> facet : df) { + // System.out.println("Key: " + facet.getKey() + " Value: " + facet.getValue()); + NamedList 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 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 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 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 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 @@ + + +00NOW/DAY-5DAYStruetrue*:*timestamptimestamp2+1DAYALLNOW/DAY+1DAY0000000+1DAY2008-03-12T00:00:00Z1600000000+1DAY2008-03-12T00:00:00Z000 +