SOLR-839: XML QueryParser support (deftype=xmlparser)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1724293 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christine Poerschke 2016-01-12 17:49:28 +00:00
parent d25c982e0d
commit 284ff5235d
8 changed files with 191 additions and 2 deletions

View File

@ -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
----------------------

View File

@ -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">

View File

@ -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>

View File

@ -45,7 +45,7 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrI
public 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, SolrI
map.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);
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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",

View File

@ -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, ?);
//}
}