diff --git a/solr/core/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java b/solr/core/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java index 9855f8beb61..4dd35968484 100644 --- a/solr/core/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java +++ b/solr/core/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java @@ -446,6 +446,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf listFields.add(field.stringValue()); } + // preserve order of values in a multiValued list boolean preserveMulti = params.getFieldBool(fieldName, HighlightParams.PRESERVE_MULTI, false); String[] docTexts = (String[]) listFields.toArray(new String[listFields.size()]); @@ -551,7 +552,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf } } - if (fragTexts.size() >= numFragments) break; + if (fragTexts.size() >= numFragments && !preserveMulti) break; } summaries = fragTexts.toArray(new String[0]); if (summaries.length > 0) diff --git a/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java b/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java index 2db08fa2075..b1339bfb762 100755 --- a/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java +++ b/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java @@ -299,31 +299,22 @@ public class HighlighterTest extends SolrTestCaseJ4 { ); } - @Test - public void testPreserveMulti() { - HashMap args = new HashMap(); - args.put("hl", "true"); - args.put("hl.fl", "cat"); - args.put("hl.snippets", "2"); - args.put("f.cat.hl.preserveMulti", "true"); - TestHarness.LocalRequestFactory sumLRF = h.getRequestFactory( - "standard", 0, 200, args); - assertU(adoc("cat", "electronics", - "cat", "monitor", - "id", "1")); + @Test + public void testPreserveMulti() throws Exception { + assertU(adoc("id","1", "cat", "electronics", "cat", "monitor")); assertU(commit()); - assertU(optimize()); - assertQ("Preserve multi", - sumLRF.makeRequest("cat:monitor"), - "//lst[@name='highlighting']/lst[@name='1']", - "//lst[@name='1']/arr[@name='cat']/str[.=\'electronics\']", - "//lst[@name='1']/arr[@name='cat']/str[.=\'monitor\']" + + assertJQ(req("q", "cat:monitor", "hl", "true", "hl.fl", "cat", "hl.snippets", "2", "f.cat.hl.preserveMulti", "true"), + "/highlighting/1/cat==['electronics','monitor']" + ); + + // No match still lists all snippets? + assertJQ(req("q", "id:1 OR cat:duuuude", "hl", "true", "hl.fl", "cat", "hl.snippets", "2", "f.cat.hl.preserveMulti", "true"), + "/highlighting/1/cat==['electronics','monitor']" ); } - - @Test public void testDefaultFieldHighlight() {