SOLR-2113: add TermQParserPlugin

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@995607 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-09-09 23:14:01 +00:00
parent c6acdba4a2
commit aa3e1599e9
5 changed files with 96 additions and 0 deletions

View File

@ -255,6 +255,11 @@ New Features
* SOLR-2099: Add ability to throttle rsync based replication using rsync option --bwlimit. * SOLR-2099: Add ability to throttle rsync based replication using rsync option --bwlimit.
(Brandon Evans via koji) (Brandon Evans via koji)
* SOLR-2113: Add TermQParserPlugin, registered as "term". This is useful
when generating filter queries from terms returned from field faceting or
the terms component. Example: fq={!term f=weight}1.5 (hossman, yonik)
Optimizations Optimizations
---------------------- ----------------------

View File

@ -35,6 +35,7 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin {
ExtendedDismaxQParserPlugin.NAME, ExtendedDismaxQParserPlugin.class, ExtendedDismaxQParserPlugin.NAME, ExtendedDismaxQParserPlugin.class,
FieldQParserPlugin.NAME, FieldQParserPlugin.class, FieldQParserPlugin.NAME, FieldQParserPlugin.class,
RawQParserPlugin.NAME, RawQParserPlugin.class, RawQParserPlugin.NAME, RawQParserPlugin.class,
TermQParserPlugin.NAME, TermQParserPlugin.class,
NestedQParserPlugin.NAME, NestedQParserPlugin.class, NestedQParserPlugin.NAME, NestedQParserPlugin.class,
FunctionRangeQParserPlugin.NAME, FunctionRangeQParserPlugin.class, FunctionRangeQParserPlugin.NAME, FunctionRangeQParserPlugin.class,
SpatialFilterQParserPlugin.NAME, SpatialFilterQParserPlugin.class, SpatialFilterQParserPlugin.NAME, SpatialFilterQParserPlugin.class,

View File

@ -26,6 +26,11 @@ import org.apache.solr.request.SolrQueryRequest;
/** /**
* Create a term query from the input value without any text analysis or transformation whatsoever. * Create a term query from the input value without any text analysis or transformation whatsoever.
* This is useful in debugging, or when raw terms are returned from the terms component (this is not the default).
*
* <br>For easy filter construction to drill down in faceting, the {@link TermQParserPlugin} is recommended.
* <br>For full analysis on all fields, including text fields, see the {@link FieldQParserPlugin}.
*
* <br>Other parameters: <code>f</code>, the field * <br>Other parameters: <code>f</code>, the field
* <br>Example: <code>{!raw f=myfield}Foo Bar</code> creates <code>TermQuery(Term("myfield","Foo Bar"))</code> * <br>Example: <code>{!raw f=myfield}Foo Bar</code> creates <code>TermQuery(Term("myfield","Foo Bar"))</code>
*/ */

View File

@ -0,0 +1,66 @@
/**
* 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.
*/
package org.apache.solr.search;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.schema.FieldType;
/**
* Create a single term query from the input value equivalent to readableToIndexed().
* This is useful for generating filter queries from the external human readable terms returned by the
* faceting or terms components.
*
* <p>
* For text fields, no analysis is done since raw terms are already returned from the faceting
* and terms components, and not all text analysis is idempotent.
* To apply analysis to text fields as well, see the {@link FieldQParserPlugin}.
* <br>
* If no analysis or transformation is desired for any type of field, see the {@link RawQParserPlugin}.
*
* <p>Other parameters: <code>f</code>, the field
* <br>Example: <code>{!term f=weight}1.5</code>
*/
public class TermQParserPlugin extends QParserPlugin {
public static String NAME = "term";
public void init(NamedList args) {
}
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
public Query parse() throws ParseException {
String fname = localParams.get(QueryParsing.F);
FieldType ft = req.getSchema().getFieldTypeNoEx(fname);
String val = localParams.get(QueryParsing.V);
BytesRef term = new BytesRef();
if (ft != null) {
ft.readableToIndexed(val, term);
} else {
term.copy(val);
}
return new TermQuery(new Term(fname, term));
}
};
}
}

View File

@ -118,6 +118,8 @@ public class TestQueryTypes extends AbstractSolrTestCase {
req("q","{!raw f=v_t}hello") req("q","{!raw f=v_t}hello")
,"//result[@numFound='2']" ,"//result[@numFound='2']"
); );
// no analysis is done, so these should match nothing
assertQ("test raw query", assertQ("test raw query",
req("q","{!raw f=v_t}Hello") req("q","{!raw f=v_t}Hello")
,"//result[@numFound='0']" ,"//result[@numFound='0']"
@ -127,6 +129,23 @@ public class TestQueryTypes extends AbstractSolrTestCase {
,"//result[@numFound='0']" ,"//result[@numFound='0']"
); );
// test "term" qparser, which should only do readableToIndexed
assertQ(
req("q","{!term f=v_f}1.5")
,"//result[@numFound='1']"
);
// text fields are *not* analyzed since they may not be idempotent
assertQ(
req("q","{!term f=v_t}Hello")
,"//result[@numFound='0']"
);
assertQ(
req("q","{!term f=v_t}hello")
,"//result[@numFound='2']"
);
// //
// test escapes in quoted strings // test escapes in quoted strings
// //