Apache Lucene open-source search software
SOLR-2064 SOLR-2065 SOLR-236: support highlighting and debugging with search grouping
Yonik Seeleyc0ca131db5f7
diff --git a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/src/java/org/apache/solr/handler/component/QueryComponent.javaindex 78c6ccf1c61..0415f2992cc 100644--- a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java+++ b/solr/src/java/org/apache/solr/handler/component/QueryComponent.java@@ -244,10 +244,15 @@ public class QueryComponent extends SearchComponentcmd.groupCommands = null;if (cmd.groupCommands != null) {+ if (rb.doHighlights || rb.isDebug()) {+ // we need a single list of the returned docs+ cmd.setFlags(cmd.getFlags() | SolrIndexSearcher.GET_DOCLIST);+ }+searcher.search(result,cmd);rb.setResult( result );rsp.add("grouped", result.groupedResults);- // TODO: get "hits" a different way+ // TODO: get "hits" a different way to logreturn;}} catch (ParseException e) {diff --git a/solr/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/src/java/org/apache/solr/search/SolrIndexSearcher.javaindex 829e235f196..b62c3ff90e4 100644--- a/solr/src/java/org/apache/solr/search/SolrIndexSearcher.java+++ b/solr/src/java/org/apache/solr/search/SolrIndexSearcher.java@@ -38,7 +38,6 @@ import java.io.IOException;import java.net.URL;import java.util.*;-import org.apache.solr.search.function.DocValues;import org.apache.solr.search.function.ValueSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -898,7 +897,8 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {private static final int NO_CHECK_QCACHE = 0x80000000;private static final int GET_DOCSET = 0x40000000;private static final int NO_CHECK_FILTERCACHE = 0x20000000;-++ public static final int GET_DOCLIST = 0x02; // get the documents actually returned in a responsepublic static final int GET_SCORES = 0x01;@@ -913,6 +913,7 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {boolean needScores = (cmd.getFlags() & GET_SCORES) != 0;boolean getDocSet = (cmd.getFlags() & GET_DOCSET) != 0;+ boolean getDocList = (cmd.getFlags() & GET_DOCLIST) != 0; // doclist needed for debugging or highlightingQuery query = QueryUtils.makeQueryable(cmd.getQuery());final Filter luceneFilter = filter==null ? null : filter.getTopFilter();@@ -977,6 +978,9 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {// TODO: optionally cache docs and feed them back through rather than re-searchingsearch(query, luceneFilter, MultiCollector.wrap(phase2Collectors));+ Set<Integer> idSet = new LinkedHashSet<Integer>(); // used for tracking unique docs when we need a doclist+ int maxMatches = 0;+ float maxScore = Float.NEGATIVE_INFINITY;NamedList grouped = new SimpleOrderedMap();for (int cmdnum=0; cmdnum<cmd.groupCommands.size(); cmdnum++) {@@ -990,7 +994,9 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {NamedList groupResult = new SimpleOrderedMap();grouped.add(groupCommand.key, groupResult); // grouped={ key={- groupResult.add("matches", collector.getMatches());+ int this_matches = collector.getMatches();+ groupResult.add("matches", this_matches);+ maxMatches = Math.max(maxMatches, this_matches);List groupList = new ArrayList();groupResult.add("groups", groupList); // grouped={ key={ groups=[@@ -1014,9 +1020,15 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {scores[i] = topDocs.scoreDocs[i].score;}- DocSlice docs = new DocSlice(0, ids.length, ids, scores, topDocs.totalHits, topDocs.getMaxScore());+ float score = topDocs.getMaxScore();+ maxScore = Math.max(maxScore, score);+ DocSlice docs = new DocSlice(0, ids.length, ids, scores, topDocs.totalHits, score);nl.add("doclist", docs);+ if (getDocList) {+ for (int id : ids)+ idSet.add(id);+ }/*** values from stage 1DocSlice docs = new DocSlice(0, 1, new int[] {group.topDoc}, null, 1, 0);@@ -1030,9 +1042,20 @@ public class SolrIndexSearcher extends IndexSearcher implements SolrInfoMBean {groupResult.add(nl);***/}- qr.groupedResults = grouped;+ }+ qr.groupedResults = grouped;++ if (getDocList) {+ int sz = idSet.size();+ int[] ids = new int[sz];+ int idx = 0;+ for (int val : idSet) {+ ids[idx++] = val;+ }+ qr.docListAndSet.docList = new DocSlice(0, sz, ids, null, maxMatches, maxScore);}+}/**diff --git a/solr/src/test/org/apache/solr/JSONTestUtil.java b/solr/src/test/org/apache/solr/JSONTestUtil.javaindex 15a4c579483..3b242c509cf 100644--- a/solr/src/test/org/apache/solr/JSONTestUtil.java+++ b/solr/src/test/org/apache/solr/JSONTestUtil.java@@ -230,7 +230,7 @@ class CollectionTester {break;}- if (entry.getKey().equals(expectedKey)) {+ if (!entry.getKey().equals(expectedKey)) {popPath();setErr("expected key '" + expectedKey + "' instead of '"+entry.getKey()+"' in ordered map");return false;diff --git a/solr/src/test/org/apache/solr/TestGroupingSearch.java b/solr/src/test/org/apache/solr/TestGroupingSearch.javaindex df5ada634ff..60f72881647 100644--- a/solr/src/test/org/apache/solr/TestGroupingSearch.java+++ b/solr/src/test/org/apache/solr/TestGroupingSearch.java@@ -223,6 +223,20 @@ public class TestGroupingSearch extends SolrTestCaseJ4 {);purgeFieldCache(FieldCache.DEFAULT); // avoid FC insanity+ // test that grouping works with highlighting+ assertJQ(req("fq",filt, "q","{!func}"+f2, "group","true", "group.field",f, "fl","id"+ ,"hl","true", "hl.fl",f)+ ,"/grouped/foo_i/matches:10:"+ ,"/highlighting:{'_ORDERED_':'', '8':{},'3':{},'4':{},'1':{},'2':{}}"+ );++ // test that grouping works with debugging+ assertJQ(req("fq",filt, "q","{!func}"+f2, "group","true", "group.field",f, "fl","id"+ ,"debugQuery","true")+ ,"/grouped/foo_i/matches:10:"+ ,"/debug/explain/8:"+ ,"/debug/explain/2:"+ );};