You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2013/11/23 18:27:23 UTC

svn commit: r1544841 - in /lucene/dev/branches/branch_4x: ./ solr/ solr/core/ solr/core/src/java/org/apache/solr/search/ solr/core/src/test-files/solr/collection1/conf/ solr/core/src/test/org/apache/solr/search/

Author: rmuir
Date: Sat Nov 23 17:27:23 2013
New Revision: 1544841

URL: http://svn.apache.org/r1544841
Log:
SOLR-5490: SimpleQParser uses multiterm analysis chain for prefixes

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/solr/   (props changed)
    lucene/dev/branches/branch_4x/solr/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_4x/solr/core/   (props changed)
    lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/search/SimpleQParserPlugin.java
    lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-simpleqpplugin.xml
    lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestSimpleQParserPlugin.java

Modified: lucene/dev/branches/branch_4x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/CHANGES.txt?rev=1544841&r1=1544840&r2=1544841&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/solr/CHANGES.txt Sat Nov 23 17:27:23 2013
@@ -52,7 +52,7 @@ New Features
 * SOLR-5287: You can edit files in the conf directory from the admin UI 
   (Erick Erickson, Stefan Matheis)
 
-* SOLR-5447: Add a QParserPlugin for Lucene's SimpleQueryParser.
+* SOLR-5447, SOLR-5490: Add a QParserPlugin for Lucene's SimpleQueryParser.
   (Jack Conradson via shalin)
 
 * SOLR-5446: Admin UI - Allow changing Schema and Config (steffkes)

Modified: lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/search/SimpleQParserPlugin.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/search/SimpleQParserPlugin.java?rev=1544841&r1=1544840&r2=1544841&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/search/SimpleQParserPlugin.java (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/search/SimpleQParserPlugin.java Sat Nov 23 17:27:23 2013
@@ -17,8 +17,12 @@ package org.apache.solr.search;
  * limitations under the License.
  */
 
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.index.Term;
 import org.apache.lucene.queryparser.simple.SimpleQueryParser;
 import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.PrefixQuery;
 import org.apache.lucene.search.Query;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.params.SimpleParams;
@@ -26,6 +30,9 @@ import org.apache.solr.common.params.Sol
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.parser.QueryParser;
 import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.schema.FieldType;
+import org.apache.solr.schema.IndexSchema;
+import org.apache.solr.schema.TextField;
 import org.apache.solr.util.SolrPluginUtils;
 
 import java.util.HashMap;
@@ -154,7 +161,35 @@ public class SimpleQParserPlugin extends
     }
 
     // Create a SimpleQueryParser using the analyzer from the schema.
-    final SimpleQueryParser parser = new SimpleQueryParser(req.getSchema().getAnalyzer(), queryFields, enabledOps);
+    final IndexSchema schema = req.getSchema();
+    final SimpleQueryParser parser = new SimpleQueryParser(req.getSchema().getAnalyzer(), queryFields, enabledOps) {
+      // Override newPrefixQuery to provide a multi term analyzer for prefix queries run against TextFields.
+      @Override
+      protected Query newPrefixQuery(String text) {
+        BooleanQuery bq = new BooleanQuery(true);
+
+        for (Map.Entry<String, Float> entry : weights.entrySet()) {
+          String field = entry.getKey();
+          FieldType type = schema.getFieldType(field);
+          Query prefix;
+
+          if (type instanceof TextField) {
+            // If the field type is a TextField then use the multi term analyzer.
+            Analyzer analyzer = ((TextField)type).getMultiTermAnalyzer();
+            String term = TextField.analyzeMultiTerm(field, text, analyzer).utf8ToString();
+            prefix = new PrefixQuery(new Term(field, term));
+          } else {
+            // If the type is *not* a TextField don't do any analysis.
+            prefix = new PrefixQuery(new Term(entry.getKey(), text));
+          }
+
+          prefix.setBoost(entry.getValue());
+          bq.add(prefix, BooleanClause.Occur.SHOULD);
+        }
+
+        return simplify(bq);
+      }
+    };
 
     // Set the default operator to be either 'AND' or 'OR' for the query.
     QueryParser.Operator defaultOp = QueryParsing.getQueryParserDefaultOperator(req.getSchema(), defaultParams.get(QueryParsing.OP));

Modified: lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-simpleqpplugin.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-simpleqpplugin.xml?rev=1544841&r1=1544840&r2=1544841&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-simpleqpplugin.xml (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-simpleqpplugin.xml Sat Nov 23 17:27:23 2013
@@ -28,6 +28,7 @@
       <analyzer>
         <tokenizer class="solr.MockTokenizerFactory" pattern="whitespace"/>
         <filter class="solr.LowerCaseFilterFactory"/>
+        <filter class="solr.ASCIIFoldingFilterFactory"/>
       </analyzer>
     </fieldtype>
 

Modified: lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestSimpleQParserPlugin.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestSimpleQParserPlugin.java?rev=1544841&r1=1544840&r2=1544841&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestSimpleQParserPlugin.java (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestSimpleQParserPlugin.java Sat Nov 23 17:27:23 2013
@@ -17,6 +17,9 @@ package org.apache.solr.search;
  * limitations under the License.
  */
 
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.PrefixQuery;
+import org.apache.lucene.search.Query;
 import org.apache.solr.SolrTestCaseJ4;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -44,7 +47,8 @@ public class TestSimpleQParserPlugin ext
     assertU(adoc("id", "53", "text0", "close", "text1", ")", "text-keyword0", ")"));
     assertU(adoc("id", "54", "text0", "escape", "text1", "\\", "text-keyword0", "\\"));
     assertU(adoc("id", "55", "text0", "whitespace", "text1", "whitespace", "text-keyword0", " "));
-    assertU(adoc("id", "55", "text0", "whitespace", "text1", "whitespace", "text-keyword0", "\n"));
+    assertU(adoc("id", "56", "text0", "whitespace", "text1", "whitespace", "text-keyword0", "\n"));
+    assertU(adoc("id", "57", "text0", "foobar", "text1", "foo bar", "text-keyword0", "fb"));
     assertU(commit());
   }
 
@@ -205,4 +209,12 @@ public class TestSimpleQParserPlugin ext
         "q.op", "AND"), "/response/numFound==1");
     assertJQ(req("defType", "simple", "qf", "text1", "q", "t2 t3"), "/response/numFound==2");
   }
+
+  /** Test that multiterm analysis chain is used for prefix. */
+  public void testPrefixChain() throws Exception {
+    assertJQ(req("defType", "simple", "qf", "text0", "q", "FOOBAR*"), "/response/numFound==1");
+    assertJQ(req("defType", "simple", "qf", "text0", "q", "Fóóbar*"), "/response/numFound==1");
+    assertJQ(req("defType", "simple", "qf", "text0", "q", "FOO*"), "/response/numFound==1");
+    assertJQ(req("defType", "simple", "qf", "text0", "q", "BAR*"), "/response/numFound==0");
+  }
 }