You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ko...@apache.org on 2011/11/07 16:26:50 UTC

svn commit: r1198778 - in /lucene/dev/trunk/solr: ./ core/src/java/org/apache/solr/handler/component/ core/src/test/org/apache/solr/highlight/ solrj/src/java/org/apache/solr/common/params/

Author: koji
Date: Mon Nov  7 15:26:49 2011
New Revision: 1198778

URL: http://svn.apache.org/viewvc?rev=1198778&view=rev
Log:
SOLR-1926: add hl.q parameter

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1198778&r1=1198777&r2=1198778&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Mon Nov  7 15:26:49 2011
@@ -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

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java?rev=1198778&r1=1198777&r2=1198778&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/HighlightComponent.java Mon Nov  7 15:26:49 2011
@@ -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.Simpl
 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 
 
   @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 
 
   @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 
       }
       
       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(

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java?rev=1198778&r1=1198777&r2=1198778&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java Mon Nov  7 15:26:49 2011
@@ -792,6 +792,7 @@ public class HighlighterTest extends Sol
             "//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 Sol
             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>'");
+  }
 }

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java?rev=1198778&r1=1198777&r2=1198778&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/params/HighlightParams.java Mon Nov  7 15:26:49 2011
@@ -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";