Apache Lucene open-source search software
Fix QueryResponse to deal with the expanded section when using the XMLResponseParser
Varun Thackerddfb4d9d00d1
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex e60b7144100..b344b421c14 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -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 ==================diff --git a/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java b/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.javaindex 8fc41bfaa8e..c775bb70135 100644--- a/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java@@ -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 PluginInfoInitiasearcher.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 PluginInfoInitiafinal 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 PluginInfoInitiaif ((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 PluginInfoInitiareturn;}- 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);diff --git a/solr/core/src/test/org/apache/solr/handler/component/DistributedExpandComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/DistributedExpandComponentTest.javaindex a2d48809d86..03275acaacb 100644--- a/solr/core/src/test/org/apache/solr/handler/component/DistributedExpandComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/DistributedExpandComponentTest.java@@ -78,6 +78,7 @@ public class DistributedExpandComponentTest extends BaseDistributedSearchTestCashandle.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");diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.javaindex 89cd9717ab7..59846ee80b1 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java@@ -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 serializedprivate transient final SolrClient solrClient;-+public QueryResponse(){solrClient = null;}@@ -134,7 +134,8 @@ public class QueryResponse extends SolrResponseBaseextractGroupedInfo( _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 SolrResponseBasereturn _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;}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.javaindex 5410c1fa2fd..9ed751fe8be 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java@@ -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"));++ }+@Testpublic void testFieldGlobbing() throws Exception {SolrClient client = getSolrClient();