LUCENE-1792: properly default rewrite method to CONSTANT_SCORE_AUTO for wildcard & prefix queries

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@803053 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-08-11 10:14:31 +00:00
parent cbfee9d814
commit 7ac73cb0a6
6 changed files with 90 additions and 6 deletions

View File

@ -18,6 +18,7 @@ package org.apache.lucene.queryParser.core.nodes;
*/
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
import org.apache.lucene.search.MultiTermQuery;
/**
* A {@link WildcardQueryNode} represents wildcard query This does not apply to
@ -25,6 +26,7 @@ import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
*/
public class WildcardQueryNode extends FieldQueryNode {
private static final long serialVersionUID = 0L;
private MultiTermQuery.RewriteMethod multiTermRewriteMethod;
/**
* @param field
@ -60,4 +62,15 @@ public class WildcardQueryNode extends FieldQueryNode {
return clone;
}
/**
* @return the rewrite method
*/
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod() {
return multiTermRewriteMethod;
}
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method) {
multiTermRewriteMethod = method;
}
}

View File

@ -36,9 +36,10 @@ public class PrefixWildcardQueryNodeBuilder implements StandardQueryBuilder {
public PrefixQuery build(QueryNode queryNode) throws QueryNodeException {
PrefixWildcardQueryNode wildcardNode = (PrefixWildcardQueryNode) queryNode;
return new PrefixQuery(new Term(wildcardNode.getFieldAsString(),
PrefixQuery q = new PrefixQuery(new Term(wildcardNode.getFieldAsString(),
wildcardNode.getTextAsString()));
q.setRewriteMethod(wildcardNode.getMultiTermRewriteMethod());
return q;
}
}

View File

@ -36,9 +36,10 @@ public class WildcardQueryNodeBuilder implements StandardQueryBuilder {
public WildcardQuery build(QueryNode queryNode) throws QueryNodeException {
WildcardQueryNode wildcardNode = (WildcardQueryNode) queryNode;
return new WildcardQuery(new Term(wildcardNode.getFieldAsString(),
WildcardQuery q = new WildcardQuery(new Term(wildcardNode.getFieldAsString(),
wildcardNode.getTextAsString()));
q.setRewriteMethod(wildcardNode.getMultiTermRewriteMethod());
return q;
}
}

View File

@ -0,0 +1,53 @@
package org.apache.lucene.queryParser.standard.processors;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.List;
import org.apache.lucene.queryParser.core.nodes.QueryNode;
import org.apache.lucene.queryParser.core.nodes.WildcardQueryNode;
import org.apache.lucene.queryParser.core.nodes.PrefixWildcardQueryNode;
import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
import org.apache.lucene.search.MultiTermQuery;
/**
* This processor instates the default {@link
* MultiTermQuery.RewriteMethod}, {@link
* MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}, for
* multi-term query nodes.
*/
public class MultiTermRewriteDefaultProcessor extends QueryNodeProcessorImpl {
protected QueryNode postProcessNode(QueryNode node) {
if (node instanceof WildcardQueryNode) {
((WildcardQueryNode) node).setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
} else if (node instanceof PrefixWildcardQueryNode) {
((PrefixWildcardQueryNode) node).setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
}
return node;
}
protected QueryNode preProcessNode(QueryNode node) {
return node;
}
protected List<QueryNode> setChildrenOrder(List<QueryNode> children) {
return children;
}
}

View File

@ -64,6 +64,7 @@ public class StandardQueryNodeProcessorPipeline extends
addProcessor(new BooleanSingleChildOptimizationQueryNodeProcessor());
addProcessor(new DefaultPhraseSlopQueryNodeProcessor());
addProcessor(new BoostQueryNodeProcessor());
addProcessor(new MultiTermRewriteDefaultProcessor());
}

View File

@ -320,6 +320,21 @@ public class TestQPHelper extends LuceneTestCase {
}
}
public void testConstantScoreAutoRewrite() throws Exception {
StandardQueryParser qp = new StandardQueryParser(new WhitespaceAnalyzer());
Query q = qp.parse("foo*bar", "field");
assertTrue(q instanceof WildcardQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery) q).getRewriteMethod());
q = qp.parse("foo*", "field");
assertTrue(q instanceof PrefixQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery) q).getRewriteMethod());
q = qp.parse("[a TO z]", "field");
assertTrue(q instanceof TermRangeQuery);
assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((MultiTermQuery) q).getRewriteMethod());
}
public void testCJK() throws Exception {
// Test Ideographic Space - As wide as a CJK character cell (fullwidth)
// used google to translate the word "term" to japanese -> ??