mirror of https://github.com/apache/lucene.git
SOLR-1926: add hl.q parameter
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1198778 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a23a9bf325
commit
ea56fff8f6
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -792,6 +792,7 @@ public class HighlighterTest extends SolrTestCaseJ4 {
|
|||
"//lst[@name='highlighting']/lst[@name='1']" +
|
||||
"/arr[@name='subword_offsets']/str='lorem <em>PowerShot.com</em> 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 <em>PowerShot</em> 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 <em>Software</em> 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 <em>Foundation</em>'");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
|
Loading…
Reference in New Issue