From 7e8e19b7c0c786aea67fa681609abeda92d910ef Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Tue, 18 Mar 2014 09:46:48 +0000 Subject: [PATCH] SOLR-5858, SOLR-4812: Allow queryparser to be defined for highlight query, and edismax and dismax to be used for this purpose git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1578800 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 6 ++++ .../handler/component/HighlightComponent.java | 15 +++++--- .../org/apache/solr/search/DisMaxQParser.java | 6 +++- .../solr/search/ExtendedDismaxQParser.java | 25 ++++++++------ .../solr/highlight/HighlighterTest.java | 34 ++++++++++++++++--- .../solr/common/params/HighlightParams.java | 1 + 6 files changed, 66 insertions(+), 21 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index bc8af296e6c..d6641ac8fc1 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -101,6 +101,9 @@ System Requirements * LUCENE-4747, LUCENE-5514: Move to Java 7 as minimum Java version. (Robert Muir, Uwe Schindler) +* SOLR-5858: Add a hl.qparser parameter to allow you to define a queryparser + for hl.q highlight queries. If no queryparser is defined, Solr will use + the overall query's defType. (Alan Woodward) New Features ---------------------- @@ -177,6 +180,9 @@ Bug Fixes * SOLR-5550: shards.info is not returned by a short circuited distributed query. (Timothy Potter, shalin) +* SOLR-5858, SOLR-4812: edismax and dismax query parsers can be used for parsing + highlight queries. (Alan Woodward, Tien Nguyen Manh) + Optimizations ---------------------- * SOLR-1880: Distributed Search skips GET_FIELDS stage if EXECUTE_QUERY diff --git a/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java b/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java index fea94e62bb2..2bf24a261a1 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java @@ -17,6 +17,7 @@ package org.apache.solr.handler.component; +import com.google.common.base.Objects; import org.apache.lucene.search.Query; import org.apache.solr.common.SolrException; import org.apache.solr.common.params.CommonParams; @@ -24,22 +25,24 @@ import org.apache.solr.common.params.HighlightParams; 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.highlight.DefaultSolrHighlighter; import org.apache.solr.highlight.PostingsSolrHighlighter; import org.apache.solr.highlight.SolrHighlighter; -import org.apache.solr.highlight.DefaultSolrHighlighter; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.search.QParser; +import org.apache.solr.search.QParserPlugin; +import org.apache.solr.search.QueryParsing; import org.apache.solr.search.SyntaxError; import org.apache.solr.util.SolrPluginUtils; import org.apache.solr.util.plugin.PluginInfoInitialized; import org.apache.solr.util.plugin.SolrCoreAware; -import org.apache.solr.core.PluginInfo; -import org.apache.solr.core.SolrCore; import java.io.IOException; import java.net.URL; -import java.util.Map; import java.util.List; +import java.util.Map; /** * TODO! @@ -69,9 +72,11 @@ public class HighlightComponent extends SearchComponent implements PluginInfoIni rb.doHighlights = highlighter.isHighlightingEnabled(params); if(rb.doHighlights){ String hlq = params.get(HighlightParams.Q); + String hlparser = Objects.firstNonNull(params.get(HighlightParams.QPARSER), + params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE)); if(hlq != null){ try { - QParser parser = QParser.getParser(hlq, null, rb.req); + QParser parser = QParser.getParser(hlq, hlparser, rb.req); rb.setHighlightQuery(parser.getHighlightQuery()); } catch (SyntaxError e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); diff --git a/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java b/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java index 748b17622e4..670432da6f7 100644 --- a/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java +++ b/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java @@ -91,11 +91,13 @@ public class DisMaxQParser extends QParser { protected String[] boostParams; protected List boostQueries; protected Query altUserQuery; - protected QParser altQParser; + + private boolean parsed = false; @Override public Query parse() throws SyntaxError { + parsed = true; SolrParams solrParams = SolrParams.wrapDefaults(localParams, params); queryFields = parseQueryFields(req.getSchema(), solrParams); @@ -263,6 +265,8 @@ public class DisMaxQParser extends QParser { @Override public Query getHighlightQuery() throws SyntaxError { + if (!parsed) + parse(); return parsedUserQuery == null ? altUserQuery : parsedUserQuery; } diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java index df106cc1f7a..315cb7ec7e6 100644 --- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java +++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java @@ -17,16 +17,6 @@ package org.apache.solr.search; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.core.StopFilterFactory; import org.apache.lucene.analysis.util.TokenFilterFactory; @@ -52,6 +42,16 @@ import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.schema.FieldType; import org.apache.solr.util.SolrPluginUtils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * Query parser that generates DisjunctionMaxQueries based on user configuration. * See Wiki page http://wiki.apache.org/solr/ExtendedDisMax @@ -100,6 +100,7 @@ public class ExtendedDismaxQParser extends QParser { private Query parsedUserQuery; private Query altUserQuery; private List boostQueries; + private boolean parsed = false; public ExtendedDismaxQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { @@ -109,6 +110,8 @@ public class ExtendedDismaxQParser extends QParser { @Override public Query parse() throws SyntaxError { + + parsed = true; /* the main query we will execute. we disable the coord because * this query is an artificial construct @@ -565,6 +568,8 @@ public class ExtendedDismaxQParser extends QParser { @Override public Query getHighlightQuery() throws SyntaxError { + if (!parsed) + parse(); return parsedUserQuery == null ? altUserQuery : parsedUserQuery; } 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 749897417d0..2b84608130f 100644 --- a/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java +++ b/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java @@ -21,11 +21,10 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.handler.component.HighlightComponent; -import org.apache.solr.request.LocalSolrQueryRequest; -import org.apache.solr.request.SolrQueryRequest; -import org.apache.solr.util.*; import org.apache.solr.common.params.HighlightParams; +import org.apache.solr.handler.component.HighlightComponent; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.util.TestHarness; import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; @@ -848,7 +847,7 @@ public class HighlighterTest extends SolrTestCaseJ4 { } public void testHlQParameter() { - assertU(adoc("title", "Apache Software Foundation", "id", "1")); + assertU(adoc("title", "Apache Software Foundation", "t_text", "apache software foundation", "id", "1")); assertU(commit()); assertQ("hl.q parameter overrides q parameter", req("q", "title:Apache", "hl", "true", "hl.fl", "title", "hl.q", "title:Software"), @@ -858,6 +857,31 @@ public class HighlighterTest extends SolrTestCaseJ4 { req("q", "title:Apache", "hl", "true", "hl.fl", "title", "hl.q", "{!v=$qq}", "qq", "title:Foundation"), "//lst[@name='highlighting']/lst[@name='1']" + "/arr[@name='title']/str='Apache Software Foundation'"); + assertQ("hl.q parameter uses localparam parser definition correctly", + req("q", "Apache", "defType", "edismax", "qf", "title t_text", "hl", "true", "hl.fl", "title", "hl.q", "{!edismax}Software"), + "//lst[@name='highlighting']/lst[@name='1']" + + "/arr[@name='title']/str='Apache Software Foundation'"); + assertQ("hl.q parameter uses defType correctly", + req("q", "Apache", "defType", "edismax", "qf", "title t_text", "hl", "true", "hl.fl", "title", "hl.q", "Software"), + "//lst[@name='highlighting']/lst[@name='1']" + + "/arr[@name='title']/str='Apache Software Foundation'"); + assertQ("hl.q parameter uses hl.qparser param correctly", + req("q", "t_text:Apache", "qf", "title t_text", "hl", "true", "hl.fl", "title", "hl.q", "Software", "hl.qparser", "edismax"), + "//lst[@name='highlighting']/lst[@name='1']" + + "/arr[@name='title']/str='Apache Software Foundation'"); + } + + public void testHlQEdismaxParameter() { + assertU(adoc("title", "Apache Software Foundation", "id", "1")); + assertU(commit()); + assertQ("hl.q parameter overrides q parameter", + req("q", "title:Apache", "hl", "true", "hl.fl", "title", "hl.q", "{!edismax qf=title v=Software}"), + "//lst[@name='highlighting']/lst[@name='1']" + + "/arr[@name='title']/str='Apache Software Foundation'"); + assertQ("hl.q parameter overrides q parameter", + req("q", "title:Apache", "hl", "true", "hl.fl", "title", "hl.q", "{!v=$qq}", "qq", "title:Foundation"), + "//lst[@name='highlighting']/lst[@name='1']" + + "/arr[@name='title']/str='Apache Software Foundation'"); } @Test 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 2f3de781a38..49e82ef3096 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 @@ -24,6 +24,7 @@ package org.apache.solr.common.params; public interface HighlightParams { public static final String HIGHLIGHT = "hl"; public static final String Q = HIGHLIGHT+".q"; + public static final String QPARSER = HIGHLIGHT+".qparser"; public static final String FIELDS = HIGHLIGHT+".fl"; public static final String SNIPPETS = HIGHLIGHT+".snippets"; public static final String FRAGSIZE = HIGHLIGHT+".fragsize";