SOLR-497: Added date faceting to QueryResponse

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@638357 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Grant Ingersoll 2008-03-18 13:12:27 +00:00
parent 588fec267a
commit b331ca07fa
5 changed files with 129 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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());
}
}
}

View File

@ -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>