Fix QueryResponse to deal with the expanded section when using the XMLResponseParser

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1670569 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Varun Thacker 2015-04-01 06:55:06 +00:00
parent 208d0c79eb
commit ddfb4d9d00
5 changed files with 101 additions and 35 deletions

View File

@ -59,7 +59,29 @@ Other Changes
* SOLR-6954: Deprecated SolrClient.shutdown() method removed (Alan Woodward)
================== 5.2.0 ==================
(No Changes)
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release
Versions of Major Components
---------------------
Upgrading from Solr 5.1
-----------------------
Detailed Change List
----------------------
New Features
----------------------
Bug Fixes
----------------------
* SOLR-6709: Fix QueryResponse to deal with the "expanded" section when using the XMLResponseParser
(Varun Thacker, Joel Bernstein)
Optimizations
----------------------
================== 5.1.0 ==================

View File

@ -27,6 +27,15 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.LongObjectMap;
import com.carrotsearch.hppc.LongObjectOpenHashMap;
import com.carrotsearch.hppc.LongOpenHashSet;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.LongCursor;
import com.carrotsearch.hppc.cursors.LongObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.FieldInfo;
@ -62,6 +71,7 @@ import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ExpandParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
@ -82,16 +92,6 @@ import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.util.plugin.PluginInfoInitialized;
import org.apache.solr.util.plugin.SolrCoreAware;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.LongObjectMap;
import com.carrotsearch.hppc.LongObjectOpenHashMap;
import com.carrotsearch.hppc.LongOpenHashSet;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.LongCursor;
import com.carrotsearch.hppc.cursors.LongObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectCursor;
/**
* The ExpandComponent is designed to work with the CollapsingPostFilter.
* The CollapsingPostFilter collapses a result set on a field.
@ -384,7 +384,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
searcher.search(new FilteredQuery(query, pfilter.filter), collector);
}
LongObjectMap groups = ((GroupCollector)groupExpandCollector).getGroups();
Map<String, DocSlice> outMap = new HashMap<>();
NamedList outMap = new SimpleOrderedMap();
CharsRefBuilder charsRef = new CharsRefBuilder();
for (LongObjectCursor cursor : (Iterable<LongObjectCursor>) groups) {
long groupValue = cursor.key;
@ -405,14 +405,14 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
final BytesRef bytesRef = ordBytes.get((int)groupValue);
fieldType.indexedToReadable(bytesRef, charsRef);
String group = charsRef.toString();
outMap.put(group, slice);
outMap.add(group, slice);
} else {
if(fieldType instanceof TrieIntField || fieldType instanceof TrieLongField ) {
outMap.put(Long.toString(groupValue), slice);
outMap.add(Long.toString(groupValue), slice);
} else if(fieldType instanceof TrieFloatField) {
outMap.put(Float.toString(Float.intBitsToFloat((int)groupValue)), slice);
outMap.add(Float.toString(Float.intBitsToFloat((int) groupValue)), slice);
} else if(fieldType instanceof TrieDoubleField) {
outMap.put(Double.toString(Double.longBitsToDouble(groupValue)), slice);
outMap.add(Double.toString(Double.longBitsToDouble(groupValue)), slice);
}
}
}
@ -450,19 +450,19 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
SolrQueryRequest req = rb.req;
Map expanded = (Map) req.getContext().get("expanded");
NamedList expanded = (NamedList) req.getContext().get("expanded");
if (expanded == null) {
expanded = new HashMap();
expanded = new SimpleOrderedMap();
req.getContext().put("expanded", expanded);
}
for (ShardResponse srsp : sreq.responses) {
NamedList response = srsp.getSolrResponse().getResponse();
Map ex = (Map) response.get("expanded");
for (Map.Entry<String, SolrDocumentList> entry : (Iterable<Map.Entry<String, SolrDocumentList>>) ex.entrySet()) {
String name = entry.getKey();
SolrDocumentList val = entry.getValue();
expanded.put(name, val);
NamedList ex = (NamedList) response.get("expanded");
for (int i=0; i<ex.size(); i++) {
String name = ex.getName(i);
SolrDocumentList val = (SolrDocumentList) ex.getVal(i);
expanded.add(name, val);
}
}
}
@ -479,9 +479,9 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
return;
}
Map expanded = (Map) rb.req.getContext().get("expanded");
NamedList expanded = (NamedList) rb.req.getContext().get("expanded");
if (expanded == null) {
expanded = new HashMap();
expanded = new SimpleOrderedMap();
}
rb.rsp.add("expanded", expanded);

View File

@ -78,6 +78,7 @@ public class DistributedExpandComponentTest extends BaseDistributedSearchTestCas
handle.put("q", SKIP);
handle.put("maxScore", SKIPVAL);
handle.put("_version_", SKIP);
handle.put("expanded", UNORDERED);
query("q", "*:*", "fq", "{!collapse field="+group+"}", "defType", "edismax", "bf", "field(test_ti)", "expand", "true", "fl","*,score");
query("q", "*:*", "fq", "{!collapse field="+group+"}", "defType", "edismax", "bf", "field(test_ti)", "expand", "true", "expand.sort", "test_tl desc", "fl","*,score");

View File

@ -17,13 +17,6 @@
package org.apache.solr.client.solrj.response;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CursorMarkParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -32,6 +25,13 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CursorMarkParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
/**
*
*
@ -86,7 +86,7 @@ public class QueryResponse extends SolrResponseBase
// utility variable used for automatic binding -- it should not be serialized
private transient final SolrClient solrClient;
public QueryResponse(){
solrClient = null;
}
@ -134,7 +134,8 @@ public class QueryResponse extends SolrResponseBase
extractGroupedInfo( _groupedInfo );
}
else if("expanded".equals(n)) {
_expandedResults = (Map<String, SolrDocumentList>) res.getVal( i );
NamedList map = (NamedList) res.getVal(i);
_expandedResults = map.asMap(1);
}
else if( "highlighting".equals( n ) ) {
_highlightingInfo = (NamedList<Object>) res.getVal( i );
@ -480,7 +481,13 @@ public class QueryResponse extends SolrResponseBase
return _facetQuery;
}
public Map<String, SolrDocumentList> getExpandedResults(){
/**
*
* @return map with each group value as key and the expanded documents that belong to the group as value.
* There is no guarantee on the order of the keys obtained via an iterator.
*
*/
public Map<String, SolrDocumentList> getExpandedResults() {
return this._expandedResults;
}

View File

@ -46,6 +46,7 @@ import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.AnalysisParams;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.FacetParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
import org.junit.Test;
import org.slf4j.Logger;
@ -1586,6 +1587,41 @@ abstract public class SolrExampleTests extends SolrExampleTestsBase
}
}
@Test
public void testExpandComponent() throws IOException, SolrServerException {
SolrClient server = getSolrClient();
server.deleteByQuery("*:*");
ArrayList<SolrInputDocument> docs = new ArrayList<>();
docs.add( makeTestDoc("id","1", "term_s", "YYYY", "group_s", "group1", "test_ti", "5", "test_tl", "10", "test_tf", "2000", "type_s", "parent"));
docs.add( makeTestDoc("id","2", "term_s","YYYY", "group_s", "group1", "test_ti", "50", "test_tl", "100", "test_tf", "200", "type_s", "child"));
docs.add( makeTestDoc("id","3", "term_s", "YYYY", "test_ti", "5000", "test_tl", "100", "test_tf", "200"));
docs.add( makeTestDoc("id","4", "term_s", "YYYY", "test_ti", "500", "test_tl", "1000", "test_tf", "2000"));
docs.add( makeTestDoc("id","5", "term_s", "YYYY", "group_s", "group2", "test_ti", "4", "test_tl", "10", "test_tf", "2000", "type_s", "parent"));
docs.add( makeTestDoc("id","6", "term_s","YYYY", "group_s", "group2", "test_ti", "10", "test_tl", "100", "test_tf", "200", "type_s", "child"));
docs.add( makeTestDoc("id","7", "term_s", "YYYY", "group_s", "group1", "test_ti", "1", "test_tl", "100000", "test_tf", "2000", "type_s", "child"));
docs.add( makeTestDoc("id","8", "term_s","YYYY", "group_s", "group2", "test_ti", "2", "test_tl", "100000", "test_tf", "200", "type_s", "child"));
server.add(docs);
server.commit();
ModifiableSolrParams msParams = new ModifiableSolrParams();
msParams.add("q", "*:*");
msParams.add("fq", "{!collapse field=group_s}");
msParams.add("defType", "edismax");
msParams.add("bf", "field(test_ti)");
msParams.add("expand", "true");
QueryResponse resp = server.query(msParams);
Map<String, SolrDocumentList> expanded = resp.getExpandedResults();
assertEquals(2, expanded.size());
assertEquals("1", expanded.get("group1").get(0).get("id"));
assertEquals("7", expanded.get("group1").get(1).get("id"));
assertEquals("5", expanded.get("group2").get(0).get("id"));
assertEquals("8", expanded.get("group2").get(1).get("id"));
}
@Test
public void testFieldGlobbing() throws Exception {
SolrClient client = getSolrClient();