Apache Lucene open-source search software
SOLR-839: XML QueryParser support (deftype=xmlparser)
Christine Poerschke284ff5235da2
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9a10cd7b390..4db9391dfa1 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -290,6 +290,23 @@ New Features* SOLR-8477: Let users choose compression mode in SchemaCodecFactory (Tomás Fernández Löbbe)+* SOLR-839: XML QueryParser support (defType=xmlparser)+ Lucene includes a queryparser that supports the creation of Lucene queries from XML.+ The queries supported by lucene.queryparser.xml.CoreParser are now supported by the newly+ created solr.search.SolrCoreParser and in future SolrCoreParser could support additional+ queries also.+ Example: <BooleanQuery fieldName="description">+ <Clause occurs="must"> <TermQuery>shirt</TermQuery> </Clause>+ <Clause occurs="mustnot"> <TermQuery>plain</TermQuery> </Clause>+ <Clause occurs="should"> <TermQuery>cotton</TermQuery> </Clause>+ <Clause occurs="must">+ <BooleanQuery fieldName="size">+ <Clause occurs="should"> <TermsQuery>S M L</TermsQuery> </Clause>+ </BooleanQuery>+ </Clause>+ </BooleanQuery>+ (Erik Hatcher, Karl Wettin, Daniel Collins, Nathan Visagan, Christine Poerschke)+Bug Fixes----------------------diff --git a/solr/common-build.xml b/solr/common-build.xmlindex ca4ae3de5f8..7d3f0b6a0eb 100644--- a/solr/common-build.xml+++ b/solr/common-build.xml@@ -461,6 +461,12 @@</sequential></macrodef>+ <target name="-compile-test-lucene-queryparser">+ <ant dir="${common.dir}/queryparser" target="compile-test" inheritAll="false">+ <propertyset refid="uptodate.and.compiled.properties"/>+ </ant>+ </target>+<!-- Solr contrib targets --><target name="-compile-analysis-extras"><ant dir="${common-solr.dir}/contrib/analysis-extras" target="compile" inheritAll="false">diff --git a/solr/core/build.xml b/solr/core/build.xmlindex dd9d07fa781..b3a654ee8c8 100644--- a/solr/core/build.xml+++ b/solr/core/build.xml@@ -31,13 +31,14 @@<target name="compile-core" depends="compile-solrj,common-solr.compile-core"/>- <target name="compile-test" depends="jar-analyzers-icu,-compile-analysis-extras,common-solr.compile-test"/>+ <target name="compile-test" depends="jar-analyzers-icu,-compile-test-lucene-queryparser,-compile-analysis-extras,common-solr.compile-test"/><path id="test.classpath"><path refid="solr.test.base.classpath"/><fileset dir="${test.lib.dir}" includes="*.jar"/><pathelement location="${analyzers-icu.jar}"/><pathelement location="${common-solr.dir}/build/contrib/solr-analysis-extras/classes/java"/>+ <pathelement location="${common.dir}/build/queryparser/classes/test"/><fileset dir="${common-solr.dir}/contrib/analysis-extras/lib" includes="icu4j*.jar"/></path>diff --git a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java b/solr/core/src/java/org/apache/solr/search/QParserPlugin.javaindex 63859f891e2..3a38c4cd91e 100644--- a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/QParserPlugin.java@@ -45,7 +45,7 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrIpublic static final Map<String, Class<? extends QParserPlugin>> standardPlugins;static {- HashMap<String, Class<? extends QParserPlugin>> map = new HashMap<>(29, 1);+ HashMap<String, Class<? extends QParserPlugin>> map = new HashMap<>(30, 1);map.put(LuceneQParserPlugin.NAME, LuceneQParserPlugin.class);map.put(OldLuceneQParserPlugin.NAME, OldLuceneQParserPlugin.class);map.put(FunctionQParserPlugin.NAME, FunctionQParserPlugin.class);@@ -75,6 +75,7 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrImap.put(MLTQParserPlugin.NAME, MLTQParserPlugin.class);map.put(HashQParserPlugin.NAME, HashQParserPlugin.class);map.put(GraphQParserPlugin.NAME, GraphQParserPlugin.class);+ map.put(XmlQParserPlugin.NAME, XmlQParserPlugin.class);standardPlugins = Collections.unmodifiableMap(map);}diff --git a/solr/core/src/java/org/apache/solr/search/SolrCoreParser.java b/solr/core/src/java/org/apache/solr/search/SolrCoreParser.javanew file mode 100755index 00000000000..8d54beb8c79--- /dev/null+++ b/solr/core/src/java/org/apache/solr/search/SolrCoreParser.java@@ -0,0 +1,39 @@+package org.apache.solr.search;++/*+ * 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 org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.queryparser.xml.CoreParser;++import org.apache.solr.request.SolrQueryRequest;++/**+ * Assembles a QueryBuilder which uses Query objects from Solr's <code>search</code> module+ * in addition to Query objects supported by the Lucene <code>CoreParser</code>.+ */+public class SolrCoreParser extends CoreParser {++ public SolrCoreParser(String defaultField, Analyzer analyzer,+ SolrQueryRequest req) {+ super(defaultField, analyzer);++ // final IndexSchema schema = req.getSchema();+ // lucene_parser.addQueryBuilder("SomeOtherQuery", new SomeOtherQueryBuilder(schema));+ }++}diff --git a/solr/core/src/java/org/apache/solr/search/XmlQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/XmlQParserPlugin.javanew file mode 100755index 00000000000..d260996289f--- /dev/null+++ b/solr/core/src/java/org/apache/solr/search/XmlQParserPlugin.java@@ -0,0 +1,74 @@+package org.apache.solr.search;++/*+ * 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.io.ByteArrayInputStream;+import java.io.UnsupportedEncodingException;++import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.queryparser.xml.ParserException;+import org.apache.lucene.search.Query;++import org.apache.solr.common.params.CommonParams;+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.IndexSchema;++public class XmlQParserPlugin extends QParserPlugin {+ public static final String NAME = "xmlparser";++ private class XmlQParser extends QParser {++ private final String contentEncoding = "UTF8";++ public XmlQParser(String qstr, SolrParams localParams,+ SolrParams params, SolrQueryRequest req) {+ super(qstr, localParams, params, req);+ }++ public Query parse() throws SyntaxError {+ final String qstr = getString();+ if (qstr == null || qstr.isEmpty()) {+ return null;+ }+ final IndexSchema schema = req.getSchema();+ final String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF));+ final Analyzer analyzer = schema.getQueryAnalyzer();+ final SolrCoreParser solrParser = new SolrCoreParser(defaultField, analyzer, req);+ try {+ return solrParser.parse(new ByteArrayInputStream(qstr.getBytes(contentEncoding)));+ } catch (UnsupportedEncodingException e) {+ throw new SyntaxError(e.getMessage() + " in " + req.toString());+ } catch (ParserException e) {+ throw new SyntaxError(e.getMessage() + " in " + req.toString());+ }+ }++ }++ @Override+ public void init(NamedList args) {+ }++ public QParser createParser(String qstr, SolrParams localParams,+ SolrParams params, SolrQueryRequest req) {+ return new XmlQParser(qstr, localParams, params, req);+ }++}diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.javaindex 6ab43e9e93f..249ac3df465 100644--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java@@ -190,6 +190,14 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {}}+ public void testMatchAllDocsQueryXmlParser() throws Exception {+ final String type = "xmlparser";+ assertQueryEquals(type,+ "{!"+type+"}<MatchAllDocsQuery/>",+ "<MatchAllDocsQuery/>",+ "<MatchAllDocsQuery></MatchAllDocsQuery>");+ }+public void testQueryDismax() throws Exception {for (final String type : new String[]{"dismax","edismax"}) {assertQueryEquals(type, "{!"+type+"}apache solr",diff --git a/solr/core/src/test/org/apache/solr/search/TestXmlQParser.java b/solr/core/src/test/org/apache/solr/search/TestXmlQParser.javanew file mode 100755index 00000000000..847b47d9e23--- /dev/null+++ b/solr/core/src/test/org/apache/solr/search/TestXmlQParser.java@@ -0,0 +1,43 @@+package org.apache.solr.search;++/*+ * 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 org.apache.lucene.queryparser.xml.CoreParser;+import org.apache.lucene.queryparser.xml.TestCoreParser;++public class TestXmlQParser extends TestCoreParser {++ private CoreParser solrCoreParser;++ @Override+ protected CoreParser coreParser() {+ if (solrCoreParser == null) {+ solrCoreParser = new SolrCoreParser(+ super.defaultField(),+ super.analyzer(),+ null);+ }+ return solrCoreParser;+ }++ //public void testSomeOtherQuery() {+ // Query q = parse("SomeOtherQuery.xml");+ // dumpResults("SomeOtherQuery", q, ?);+ //}++}