From 609a5f2094d3928c120fcf03970010d0fddc28d7 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Thu, 30 Dec 2010 21:46:25 +0000 Subject: [PATCH] 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 --- .../org/apache/solr/schema/IndexSchema.java | 18 --- .../org/apache/solr/search/QueryParsing.java | 103 ------------------ .../apache/solr/search/SolrQueryParser.java | 35 +----- .../solr/update/DirectUpdateHandler2.java | 49 +++++---- .../org/apache/solr/util/SolrPluginUtils.java | 8 -- .../apache/solr/BasicFunctionalityTest.java | 10 -- .../TestReversedWildcardFilterFactory.java | 21 ++-- .../apache/solr/util/SolrPluginUtilsTest.java | 9 +- 8 files changed, 50 insertions(+), 203 deletions(-) diff --git a/solr/src/java/org/apache/solr/schema/IndexSchema.java b/solr/src/java/org/apache/solr/schema/IndexSchema.java index f97556691e4..30b93d1e1f1 100644 --- a/solr/src/java/org/apache/solr/schema/IndexSchema.java +++ b/solr/src/java/org/apache/solr/schema/IndexSchema.java @@ -265,24 +265,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 diff --git a/solr/src/java/org/apache/solr/search/QueryParsing.java b/solr/src/java/org/apache/solr/search/QueryParsing.java index bfc9b1ac6e8..624f627797d 100644 --- a/solr/src/java/org/apache/solr/search/QueryParsing.java +++ b/solr/src/java/org/apache/solr/search/QueryParsing.java @@ -83,68 +83,6 @@ public class QueryParsing { if (null == val) val = sch.getQueryParserDefaultOperator(); return "AND".equals(val) ? Operator.AND : Operator.OR; } - - - - /** - * 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 @@ -907,45 +845,4 @@ public class QueryParsing { return out; } - /** - * Parse a function, returning a FunctionQuery - *

- *

- * Syntax Examples.... - *

- *

- *

-   * // Numeric fields default to correct type
-   * // (ie: IntFieldSource or FloatFieldSource)
-   * // Others use explicit ord(...) to generate numeric field value
-   * myfield
-   * 

- * // OrdFieldSource - * ord(myfield) - *

- * // ReverseOrdFieldSource - * rord(myfield) - *

- * // LinearFloatFunction on numeric field value - * linear(myfield,1,2) - *

- * // MaxFloatFunction of LinearFloatFunction on numeric field value or constant - * max(linear(myfield,1,2),100) - *

- * // ReciprocalFloatFunction on numeric field value - * recip(myfield,1,2,3) - *

- * // ReciprocalFloatFunction on ReverseOrdFieldSource - * recip(rord(myfield),1,2,3) - *

- * // ReciprocalFloatFunction on LinearFloatFunction on ReverseOrdFieldSource - * recip(linear(rord(myfield),1,2),3,4,5) - *

- */ - 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)); - } - } diff --git a/solr/src/java/org/apache/solr/search/SolrQueryParser.java b/solr/src/java/org/apache/solr/search/SolrQueryParser.java index 63097fb444b..d0df3423673 100644 --- a/solr/src/java/org/apache/solr/search/SolrQueryParser.java +++ b/solr/src/java/org/apache/solr/search/SolrQueryParser.java @@ -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 "_val_" is used in a term or * phrase query, the value is parsed as a function. *

- * - * @see QueryParsing#parseFunction */ public class SolrQueryParser extends QueryParser { protected final IndexSchema schema; @@ -67,32 +62,12 @@ public class SolrQueryParser extends QueryParser { protected final Map leadingWildcards = new HashMap(); - /** - * 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), - * <solrQueryParser> 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(); - } + QParser nested = parser.subQuery(queryText, "func"); + return nested.getQuery(); } else if ("_query_".equals(field) && parser != null) { return parser.subQuery(queryText, null).getQuery(); } diff --git a/solr/src/java/org/apache/solr/update/DirectUpdateHandler2.java b/solr/src/java/org/apache/solr/update/DirectUpdateHandler2.java index 6f5e97137ce..b096b383a76 100644 --- a/solr/src/java/org/apache/solr/update/DirectUpdateHandler2.java +++ b/solr/src/java/org/apache/solr/update/DirectUpdateHandler2.java @@ -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; @@ -219,33 +221,40 @@ public class DirectUpdateHandler2 extends UpdateHandler { // why not return number of docs deleted? // Depending on implementation, we may not be able to immediately determine the num... - public void deleteByQuery(DeleteUpdateCommand cmd) throws IOException { - deleteByQueryCommands.incrementAndGet(); - deleteByQueryCommandsCumulative.incrementAndGet(); + public void deleteByQuery(DeleteUpdateCommand cmd) throws IOException { + deleteByQueryCommands.incrementAndGet(); + deleteByQueryCommandsCumulative.incrementAndGet(); boolean madeIt=false; boolean delAll=false; try { - Query q = QueryParsing.parseQuery(cmd.query, schema); - delAll = MatchAllDocsQuery.class == q.getClass(); + 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); + } - iwCommit.lock(); - try { - if (delAll) { - deleteAll(); - } else { - openWriter(); - writer.deleteDocuments(q); - } - } finally { - iwCommit.unlock(); - } + delAll = MatchAllDocsQuery.class == q.getClass(); - madeIt=true; + iwCommit.lock(); + try { + if (delAll) { + deleteAll(); + } else { + openWriter(); + writer.deleteDocuments(q); + } + } finally { + iwCommit.unlock(); + } - if( tracker.timeUpperBound > 0 ) { - tracker.scheduleCommitWithin( tracker.timeUpperBound ); - } + madeIt=true; + + if( tracker.timeUpperBound > 0 ) { + tracker.scheduleCommitWithin( tracker.timeUpperBound ); + } } finally { if (!madeIt) { numErrors.incrementAndGet(); diff --git a/solr/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/src/java/org/apache/solr/util/SolrPluginUtils.java index 950d940808e..b26dbf35727 100644 --- a/solr/src/java/org/apache/solr/util/SolrPluginUtils.java +++ b/solr/src/java/org/apache/solr/util/SolrPluginUtils.java @@ -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. diff --git a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java b/solr/src/test/org/apache/solr/BasicFunctionalityTest.java index e8d5371ccff..f50bbe1c1e3 100644 --- a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java +++ b/solr/src/test/org/apache/solr/BasicFunctionalityTest.java @@ -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() { diff --git a/solr/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java b/solr/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java index 1d98747c8a1..5673ea8676e 100644 --- a/solr/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java +++ b/solr/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java @@ -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")); @@ -141,7 +134,13 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 { assertQ("should have matched", 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 */ diff --git a/solr/src/test/org/apache/solr/util/SolrPluginUtilsTest.java b/solr/src/test/org/apache/solr/util/SolrPluginUtilsTest.java index cb56f5a47ee..b824d55dbfd 100644 --- a/solr/src/test/org/apache/solr/util/SolrPluginUtilsTest.java +++ b/solr/src/test/org/apache/solr/util/SolrPluginUtilsTest.java @@ -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; @@ -156,9 +158,12 @@ 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"));