SOLR-1930: remove query parsing w/o qparser ref

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1054007 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-12-30 21:46:25 +00:00
parent 4ce982a287
commit 609a5f2094
8 changed files with 50 additions and 203 deletions

View File

@ -266,24 +266,6 @@ public final class IndexSchema {
public Analyzer getQueryAnalyzer() { return queryAnalyzer; }
/**
* A SolrQueryParser linked to this IndexSchema for field datatype
* information, and populated with default options from the
* <solrQueryParser> configuration for this IndexSchema.
*
* @param defaultField if non-null overrides the schema default
* @deprecated
*/
@Deprecated
public SolrQueryParser getSolrQueryParser(String defaultField) {
SolrQueryParser qp = new SolrQueryParser(this,defaultField);
String operator = getQueryParserDefaultOperator();
qp.setDefaultOperator("AND".equals(operator) ?
QueryParser.Operator.AND : QueryParser.Operator.OR);
return qp;
}
/**
* Name of the default search field specified in the schema file
*/

View File

@ -85,68 +85,6 @@ public class QueryParsing {
}
/**
* Helper utility for parsing a query using the Lucene QueryParser syntax.
*
* @param qs query expression in standard Lucene syntax
* @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information
*/
public static Query parseQuery(String qs, IndexSchema schema) {
return parseQuery(qs, null, schema);
}
/**
* Helper utility for parsing a query using the Lucene QueryParser syntax.
*
* @param qs query expression in standard Lucene syntax
* @param defaultField default field used for unqualified search terms in the query expression
* @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information
*/
public static Query parseQuery(String qs, String defaultField, IndexSchema schema) {
try {
Query query = schema.getSolrQueryParser(defaultField).parse(qs);
if (SolrCore.log.isTraceEnabled()) {
SolrCore.log.trace("After QueryParser:" + query);
}
return query;
} catch (ParseException e) {
SolrCore.log(e);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing Lucene query", e);
}
}
/**
* Helper utility for parsing a query using the Lucene QueryParser syntax.
*
* @param qs query expression in standard Lucene syntax
* @param defaultField default field used for unqualified search terms in the query expression
* @param params used to determine the default operator, overriding the schema specified operator
* @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information
*/
public static Query parseQuery(String qs, String defaultField, SolrParams params, IndexSchema schema) {
try {
SolrQueryParser parser = schema.getSolrQueryParser(defaultField);
parser.setDefaultOperator(getQueryParserDefaultOperator
(schema, params.get(QueryParsing.OP)));
Query query = parser.parse(qs);
if (SolrCore.log.isTraceEnabled()) {
SolrCore.log.trace("After QueryParser:" + query);
}
return query;
} catch (ParseException e) {
SolrCore.log(e);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Query parsing error: " + e.getMessage(), e);
}
}
// note to self: something needs to detect infinite recursion when parsing queries
static int parseLocalParams(String txt, int start, Map<String, String> target, SolrParams params) throws ParseException {
int off = start;
@ -907,45 +845,4 @@ public class QueryParsing {
return out;
}
/**
* Parse a function, returning a FunctionQuery
* <p/>
* <p>
* Syntax Examples....
* </p>
* <p/>
* <pre>
* // Numeric fields default to correct type
* // (ie: IntFieldSource or FloatFieldSource)
* // Others use explicit ord(...) to generate numeric field value
* myfield
* <p/>
* // OrdFieldSource
* ord(myfield)
* <p/>
* // ReverseOrdFieldSource
* rord(myfield)
* <p/>
* // LinearFloatFunction on numeric field value
* linear(myfield,1,2)
* <p/>
* // MaxFloatFunction of LinearFloatFunction on numeric field value or constant
* max(linear(myfield,1,2),100)
* <p/>
* // ReciprocalFloatFunction on numeric field value
* recip(myfield,1,2,3)
* <p/>
* // ReciprocalFloatFunction on ReverseOrdFieldSource
* recip(rord(myfield),1,2,3)
* <p/>
* // ReciprocalFloatFunction on LinearFloatFunction on ReverseOrdFieldSource
* recip(linear(rord(myfield),1,2),3,4,5)
* </pre>
*/
public static FunctionQuery parseFunction(String func, IndexSchema schema) throws ParseException {
SolrCore core = SolrCore.getSolrCore();
return (FunctionQuery) (QParser.getParser(func, "func", new LocalSolrQueryRequest(core, new HashMap())).parse());
// return new FunctionQuery(parseValSource(new StrParser(func), schema));
}
}

View File

@ -39,9 +39,6 @@ import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TextField;
// TODO: implement the analysis of simple fields with
// FieldType.toInternal() instead of going through the
// analyzer. Should lead to faster query parsing.
/**
* A variation on the Lucene QueryParser which knows about the field
@ -57,8 +54,6 @@ import org.apache.solr.schema.TextField;
* If the magic field name "<code>_val_</code>" is used in a term or
* phrase query, the value is parsed as a function.
* </p>
*
* @see QueryParsing#parseFunction
*/
public class SolrQueryParser extends QueryParser {
protected final IndexSchema schema;
@ -67,32 +62,12 @@ public class SolrQueryParser extends QueryParser {
protected final Map<String, ReversedWildcardFilterFactory> leadingWildcards =
new HashMap<String, ReversedWildcardFilterFactory>();
/**
* Constructs a SolrQueryParser using the schema to understand the
* formats and datatypes of each field. Only the defaultSearchField
* will be used from the IndexSchema (unless overridden),
* &lt;solrQueryParser&gt; will not be used.
*
* @param schema Used for default search field name if defaultField is null and field information is used for analysis
* @param defaultField default field used for unspecified search terms. if null, the schema default field is used
* @see IndexSchema#getDefaultSearchFieldName()
*/
public SolrQueryParser(IndexSchema schema, String defaultField) {
super(schema.getSolrConfig().getLuceneVersion("luceneMatchVersion", Version.LUCENE_30), defaultField == null ? schema.getDefaultSearchFieldName() : defaultField, schema.getQueryAnalyzer());
this.schema = schema;
this.parser = null;
this.defaultField = defaultField;
setLowercaseExpandedTerms(false);
setEnablePositionIncrements(true);
checkAllowLeadingWildcards();
}
public SolrQueryParser(QParser parser, String defaultField) {
this(parser, defaultField, parser.getReq().getSchema().getQueryAnalyzer());
}
public SolrQueryParser(QParser parser, String defaultField, Analyzer analyzer) {
super(parser.getReq().getSchema().getSolrConfig().getLuceneVersion("luceneMatchVersion", Version.LUCENE_30), defaultField, analyzer);
super(parser.getReq().getCore().getSolrConfig().getLuceneVersion("luceneMatchVersion", Version.LUCENE_30), defaultField, analyzer);
this.schema = parser.getReq().getSchema();
this.parser = parser;
this.defaultField = defaultField;
@ -138,12 +113,8 @@ public class SolrQueryParser extends QueryParser {
// own functions.
if (field.charAt(0) == '_') {
if ("_val_".equals(field)) {
if (parser==null) {
return QueryParsing.parseFunction(queryText, schema);
} else {
QParser nested = parser.subQuery(queryText, "func");
return nested.getQuery();
}
} else if ("_query_".equals(field) && parser != null) {
return parser.subQuery(queryText, null).getQuery();
}

View File

@ -22,6 +22,7 @@ package org.apache.solr.update;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
@ -47,6 +48,7 @@ import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
@ -226,7 +228,14 @@ public class DirectUpdateHandler2 extends UpdateHandler {
boolean madeIt=false;
boolean delAll=false;
try {
Query q = QueryParsing.parseQuery(cmd.query, schema);
Query q = null;
try {
QParser parser = QParser.getParser(cmd.query, "lucene", cmd.req);
q = parser.getQuery();
} catch (ParseException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
delAll = MatchAllDocsQuery.class == q.getClass();
iwCommit.lock();

View File

@ -667,14 +667,6 @@ public class SolrPluginUtils {
// don't trust that our parent class won't ever change it's default
setDefaultOperator(QueryParser.Operator.OR);
}
public DisjunctionMaxQueryParser(IndexSchema s, String defaultField) {
super(s,defaultField);
// don't trust that our parent class won't ever change it's default
setDefaultOperator(QueryParser.Operator.OR);
}
public DisjunctionMaxQueryParser(IndexSchema s) {
this(s,null);
}
/**
* Add an alias to this query parser.

View File

@ -345,16 +345,6 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {
);
}
/** @see org.apache.solr.analysis.TestRemoveDuplicatesTokenFilter */
@Test
public void testRemoveDuplicatesTokenFilter() {
Query q = QueryParsing.parseQuery("TV", "dedup",
h.getCore().getSchema());
assertTrue("not boolean?", q instanceof BooleanQuery);
assertEquals("unexpected number of stemmed synonym tokens",
2, ((BooleanQuery) q).clauses().size());
}
@Test
public void testTermVectorFields() {

View File

@ -32,7 +32,9 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.SpecialOperations;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SolrQueryParser;
import org.junit.Before;
import org.junit.BeforeClass;
@ -112,15 +114,6 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
@Test
public void testQueryParsing() throws Exception {
SolrQueryParser parserOne = new SolrQueryParser(schema, "one");
assertTrue(parserOne.getAllowLeadingWildcard());
SolrQueryParser parserTwo = new SolrQueryParser(schema, "two");
assertTrue(parserTwo.getAllowLeadingWildcard());
SolrQueryParser parserThree = new SolrQueryParser(schema, "three");
// XXX note: this should be false, but for now we return true for any field,
// XXX if at least one field uses the reversing
assertTrue(parserThree.getAllowLeadingWildcard());
// add some docs
assertU(adoc("id", "1", "one", "one"));
assertU(adoc("id", "2", "two", "two"));
@ -142,6 +135,12 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
req("+id:6 +three:*si\uD834\uDD1Ex"),
"//result[@numFound=1]");
SolrQueryRequest req = req();
QParser qparser = QParser.getParser("id:1", "lucene", req);
SolrQueryParser parserTwo = new SolrQueryParser(qparser, "two");
assertTrue(parserTwo.getAllowLeadingWildcard());
// test conditional reversal
assertTrue(wasReversed(parserTwo, "*hree"));
assertTrue(wasReversed(parserTwo, "t*ree"));
@ -153,6 +152,8 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
assertFalse(wasReversed(parserTwo, "th?*ee"));
assertFalse(wasReversed(parserTwo, "short*token"));
assertTrue(wasReversed(parserTwo, "ver*longtoken"));
req.close();
}
/** fragile assert: depends on our implementation, but cleanest way to check for now */

View File

@ -18,6 +18,8 @@
package org.apache.solr.util;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.util.SolrPluginUtils;
import org.apache.solr.util.SolrPluginUtils.DisjunctionMaxQueryParser;
import org.apache.solr.search.SolrIndexSearcher;
@ -157,8 +159,11 @@ public class SolrPluginUtilsTest extends SolrTestCaseJ4 {
Query out;
String t;
SolrQueryRequest req = req();
QParser qparser = QParser.getParser("hi", "dismax", req);
DisjunctionMaxQueryParser qp =
new SolrPluginUtils.DisjunctionMaxQueryParser(h.getCore().getSchema());
new SolrPluginUtils.DisjunctionMaxQueryParser(qparser, req.getSchema().getDefaultSearchFieldName());
qp.addAlias("hoss", 0.01f, SolrPluginUtils.parseFieldBoosts
("title^2.0 title_stemmed name^1.2 subject^0.5"));