From 20362deb7e6814c1922163595e7edeb652d3ce37 Mon Sep 17 00:00:00 2001 From: David Smiley Date: Wed, 28 Dec 2016 22:57:44 -0500 Subject: [PATCH] SOLR-9897: Add hl.requireFieldMatch=false support when using the UnifiedHighlighter --- solr/CHANGES.txt | 7 +++++-- .../solr/highlight/UnifiedSolrHighlighter.java | 15 ++++++++++++++- .../highlight/TestUnifiedSolrHighlighter.java | 10 ++++++++++ .../solr/common/params/HighlightParams.java | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 501527addea..852a30680df 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -109,8 +109,8 @@ Upgrade Notes * SOLR-9708: You are encouraged to try out the UnifiedHighlighter by setting hl.method=unified and report feedback. It might become the default in 7.0. It's more efficient/faster than the other highlighters, especially compared to the - original Highlighter. That said, some options aren't supported yet, notably hl.fragsize and - hl.requireFieldMatch=false. It will get more features in time, especially with your input. See HighlightParams.java + original Highlighter. That said, some options aren't supported yet, notably hl.fragsize. + It will get more features in time, especially with your input. See HighlightParams.java for a listing of highlight parameters annotated with which highlighters use them. hl.useFastVectorHighlighter is now considered deprecated in lieu of hl.method=fastVector. @@ -199,6 +199,9 @@ New Features * SOLR-9880: Add Ganglia, Graphite and SLF4J metrics reporters. (ab) +* SOLR-9897: Add hl.requireFieldMatch toggle support when using the UnifiedHighlighter. Defaults to false like the + other highlighters that support this. (David Smiley) + Optimizations ---------------------- * SOLR-9704: Facet Module / JSON Facet API: Optimize blockChildren facets that have diff --git a/solr/core/src/java/org/apache/solr/highlight/UnifiedSolrHighlighter.java b/solr/core/src/java/org/apache/solr/highlight/UnifiedSolrHighlighter.java index c38546ee116..910fa2b8d75 100644 --- a/solr/core/src/java/org/apache/solr/highlight/UnifiedSolrHighlighter.java +++ b/solr/core/src/java/org/apache/solr/highlight/UnifiedSolrHighlighter.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Predicate; import org.apache.lucene.document.Document; import org.apache.lucene.search.DocIdSetIterator; @@ -221,9 +222,10 @@ public class UnifiedSolrHighlighter extends SolrHighlighter implements PluginInf * From {@link #getHighlighter(org.apache.solr.request.SolrQueryRequest)}. */ protected static class SolrExtendedUnifiedHighlighter extends UnifiedHighlighter { + protected final static Predicate NOT_REQUIRED_FIELD_MATCH_PREDICATE = s -> true; protected final SolrParams params; - protected final IndexSchema schema; + protected final IndexSchema schema; protected final RTimerTree loadFieldValuesTimer; public SolrExtendedUnifiedHighlighter(SolrQueryRequest req) { @@ -360,6 +362,17 @@ public class UnifiedSolrHighlighter extends SolrHighlighter implements PluginInf return params.getFieldBool(field, HighlightParams.USE_PHRASE_HIGHLIGHTER, true); } + @Override + protected Predicate getFieldMatcher(String field) { + // TODO define hl.queryFieldPattern as a more advanced alternative to hl.requireFieldMatch. + + // note that the UH & PH at Lucene level default to effectively "true" + if (params.getFieldBool(field, HighlightParams.FIELD_MATCH, false)) { + return field::equals; // requireFieldMatch + } else { + return NOT_REQUIRED_FIELD_MATCH_PREDICATE; + } + } } } \ No newline at end of file diff --git a/solr/core/src/test/org/apache/solr/highlight/TestUnifiedSolrHighlighter.java b/solr/core/src/test/org/apache/solr/highlight/TestUnifiedSolrHighlighter.java index 95754a4ac1e..e2511bef53d 100644 --- a/solr/core/src/test/org/apache/solr/highlight/TestUnifiedSolrHighlighter.java +++ b/solr/core/src/test/org/apache/solr/highlight/TestUnifiedSolrHighlighter.java @@ -225,5 +225,15 @@ public class TestUnifiedSolrHighlighter extends SolrTestCaseJ4 { req("q", "text:document", "sort", "id asc", "hl", "true", "hl.encoder", "html"), "//lst[@name='highlighting']/lst[@name='103']/arr[@name='text']/str='Document one has a first <i>sentence</i>.'"); } + + public void testRequireFieldMatch() { + // We highlight on field text3 (hl.fl), but our query only references the "text" field. Nonetheless, the query word + // "document" is found in all fields here. + + assertQ(req("q", "id:101", "hl", "true", "hl.q", "text:document", "hl.fl", "text3"), //hl.requireFieldMatch is false by default + "count(//lst[@name='highlighting']/lst[@name='101']/arr[@name='text3']/*)=1"); + assertQ(req("q", "id:101", "hl", "true", "hl.q", "text:document", "hl.fl", "text3", "hl.requireFieldMatch", "true"), + "count(//lst[@name='highlighting']/lst[@name='101']/arr[@name='text3']/*)=0"); + } } diff --git a/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java b/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java index fd752bfc60a..917e9f57926 100644 --- a/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java +++ b/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java @@ -38,7 +38,7 @@ public interface HighlightParams { // query interpretation public static final String Q = HIGHLIGHT+".q"; // all public static final String QPARSER = HIGHLIGHT+".qparser"; // all - public static final String FIELD_MATCH = HIGHLIGHT+".requireFieldMatch"; // OH, FVH + public static final String FIELD_MATCH = HIGHLIGHT+".requireFieldMatch"; // OH, FVH, UH public static final String USE_PHRASE_HIGHLIGHTER = HIGHLIGHT+".usePhraseHighlighter"; // OH, FVH, UH public static final String HIGHLIGHT_MULTI_TERM = HIGHLIGHT+".highlightMultiTerm"; // all