From ea56fff8f6f10049d2ef0a0fb97a8cca79f7edb1 Mon Sep 17 00:00:00 2001 From: Koji Sekiguchi Date: Mon, 7 Nov 2011 15:26:49 +0000 Subject: [PATCH] SOLR-1926: add hl.q parameter git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1198778 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 ++ .../handler/component/HighlightComponent.java | 23 +++++++++++++++---- .../solr/highlight/HighlighterTest.java | 16 ++++++++++++- .../solr/common/params/HighlightParams.java | 1 + 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 8d2f5391031..8791f7c403f 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -384,6 +384,8 @@ New Features * SOLR-2276: Add support for cologne phonetic to PhoneticFilterFactory. (Marc Pompl via rmuir) +* SOLR-1926: Add hl.q parameter. (koji) + Bug Fixes ---------------------- * SOLR-2748: The CommitTracker used for commitWith or autoCommit by maxTime 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 f055e29faf3..206ef300201 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 org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.search.Query; import org.apache.solr.common.SolrException; import org.apache.solr.common.params.CommonParams; @@ -27,6 +28,7 @@ import org.apache.solr.common.util.SimpleOrderedMap; 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.util.SolrPluginUtils; import org.apache.solr.util.plugin.PluginInfoInitialized; import org.apache.solr.util.plugin.SolrCoreAware; @@ -61,7 +63,19 @@ public class HighlightComponent extends SearchComponent implements PluginInfoIni @Override public void prepare(ResponseBuilder rb) throws IOException { - rb.doHighlights = highlighter.isHighlightingEnabled(rb.req.getParams()); + SolrParams params = rb.req.getParams(); + rb.doHighlights = highlighter.isHighlightingEnabled(params); + if(rb.doHighlights){ + String hlq = params.get(HighlightParams.Q); + if(hlq != null){ + try { + QParser parser = QParser.getParser(hlq, null, rb.req); + rb.setHighlightQuery(parser.getHighlightQuery()); + } catch (ParseException e) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); + } + } + } } public void inform(SolrCore core) { @@ -84,8 +98,8 @@ public class HighlightComponent extends SearchComponent implements PluginInfoIni @Override public void process(ResponseBuilder rb) throws IOException { - SolrQueryRequest req = rb.req; if (rb.doHighlights) { + SolrQueryRequest req = rb.req; SolrParams params = req.getParams(); String[] defaultHighlightFields; //TODO: get from builder by default? @@ -112,10 +126,11 @@ public class HighlightComponent extends SearchComponent implements PluginInfoIni } if(highlightQuery != null) { - boolean rewrite = !(Boolean.valueOf(req.getParams().get(HighlightParams.USE_PHRASE_HIGHLIGHTER, "true")) && Boolean.valueOf(req.getParams().get(HighlightParams.HIGHLIGHT_MULTI_TERM, "true"))); + boolean rewrite = !(Boolean.valueOf(params.get(HighlightParams.USE_PHRASE_HIGHLIGHTER, "true")) && + Boolean.valueOf(params.get(HighlightParams.HIGHLIGHT_MULTI_TERM, "true"))); highlightQuery = rewrite ? highlightQuery.rewrite(req.getSearcher().getIndexReader()) : highlightQuery; } - + // No highlighting if there is no query -- consider q.alt="*:* if( highlightQuery != null ) { NamedList sumData = highlighter.doHighlighting( 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 3ad9c86b28a..5c9246d42ba 100755 --- a/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java +++ b/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java @@ -792,6 +792,7 @@ public class HighlighterTest extends SolrTestCaseJ4 { "//lst[@name='highlighting']/lst[@name='1']" + "/arr[@name='subword_offsets']/str='lorem PowerShot.com ipsum'"); } + public void testSubwordWildcardHighlightWithTermOffsets2() { assertU(adoc("subword_offsets", "lorem PowerShot ipsum", "id", "1")); assertU(commit()); @@ -799,5 +800,18 @@ public class HighlighterTest extends SolrTestCaseJ4 { req("q", "subword_offsets:pow*", "hl", "true", "hl.fl", "subword_offsets"), "//lst[@name='highlighting']/lst[@name='1']" + "/arr[@name='subword_offsets']/str='lorem PowerShot ipsum'"); - } + } + + public void testHlQParameter() { + 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", "title: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'"); + } } 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 90b7c04a3e1..b5b87ae7772 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 @@ -23,6 +23,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 FIELDS = HIGHLIGHT+".fl"; public static final String SNIPPETS = HIGHLIGHT+".snippets"; public static final String FRAGSIZE = HIGHLIGHT+".fragsize";