LUCENE-7658: queryparser/xml CoreParser now implements SpanQueryBuilder interface. (Daniel Collins, Christine Poerschke)

This commit is contained in:
Christine Poerschke 2017-01-30 14:37:34 +00:00
parent f73d93ac1b
commit 61ab4e338e
3 changed files with 37 additions and 3 deletions

View File

@ -153,6 +153,9 @@ Other
* LUCENE-7666: Fix typos in lucene-join package info javadoc. * LUCENE-7666: Fix typos in lucene-join package info javadoc.
(Tom Saleeba via Christine Poerschke) (Tom Saleeba via Christine Poerschke)
* LUCENE-7658: queryparser/xml CoreParser now implements SpanQueryBuilder interface.
(Daniel Collins, Christine Poerschke)
======================= Lucene 6.4.1 ======================= ======================= Lucene 6.4.1 =======================
Bug Fixes Bug Fixes

View File

@ -20,6 +20,7 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.xml.builders.*; import org.apache.lucene.queryparser.xml.builders.*;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanQuery;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -31,7 +32,7 @@ import java.io.InputStream;
/** /**
* Assembles a QueryBuilder which uses only core Lucene Query objects * Assembles a QueryBuilder which uses only core Lucene Query objects
*/ */
public class CoreParser implements QueryBuilder { public class CoreParser implements QueryBuilder, SpanQueryBuilder {
protected String defaultField; protected String defaultField;
protected Analyzer analyzer; protected Analyzer analyzer;
@ -114,6 +115,11 @@ public class CoreParser implements QueryBuilder {
return getQuery(parseXML(xmlStream).getDocumentElement()); return getQuery(parseXML(xmlStream).getDocumentElement());
} }
// for test use
SpanQuery parseAsSpanQuery(InputStream xmlStream) throws ParserException {
return getSpanQuery(parseXML(xmlStream).getDocumentElement());
}
public void addQueryBuilder(String nodeName, QueryBuilder builder) { public void addQueryBuilder(String nodeName, QueryBuilder builder) {
queryFactory.addBuilder(nodeName, builder); queryFactory.addBuilder(nodeName, builder);
} }
@ -122,6 +128,11 @@ public class CoreParser implements QueryBuilder {
spanFactory.addBuilder(nodeName, builder); spanFactory.addBuilder(nodeName, builder);
} }
public void addSpanQueryBuilder(String nodeName, SpanQueryBuilder builder) {
queryFactory.addBuilder(nodeName, builder);
spanFactory.addBuilder(nodeName, builder);
}
static Document parseXML(InputStream pXmlFile) throws ParserException { static Document parseXML(InputStream pXmlFile) throws ParserException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null; DocumentBuilder db = null;
@ -144,4 +155,9 @@ public class CoreParser implements QueryBuilder {
public Query getQuery(Element e) throws ParserException { public Query getQuery(Element e) throws ParserException {
return queryFactory.getQuery(e); return queryFactory.getQuery(e);
} }
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
return spanFactory.getSpanQuery(e);
}
} }

View File

@ -27,6 +27,7 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -116,6 +117,9 @@ public class TestCoreParser extends LuceneTestCase {
public void testSpanTermXML() throws Exception { public void testSpanTermXML() throws Exception {
Query q = parse("SpanQuery.xml"); Query q = parse("SpanQuery.xml");
dumpResults("Span Query", q, 5); dumpResults("Span Query", q, 5);
SpanQuery sq = parseAsSpan("SpanQuery.xml");
dumpResults("Span Query", sq, 5);
assertEquals(q, sq);
} }
public void testConstantScoreQueryXML() throws Exception { public void testConstantScoreQueryXML() throws Exception {
@ -207,10 +211,21 @@ public class TestCoreParser extends LuceneTestCase {
} }
protected Query parse(String xmlFileName) throws ParserException, IOException { protected Query parse(String xmlFileName) throws ParserException, IOException {
return implParse(xmlFileName, false);
}
protected SpanQuery parseAsSpan(String xmlFileName) throws ParserException, IOException {
return (SpanQuery)implParse(xmlFileName, true);
}
private Query implParse(String xmlFileName, boolean span) throws ParserException, IOException {
try (InputStream xmlStream = TestCoreParser.class.getResourceAsStream(xmlFileName)) { try (InputStream xmlStream = TestCoreParser.class.getResourceAsStream(xmlFileName)) {
assertNotNull("Test XML file " + xmlFileName + " cannot be found", xmlStream); assertNotNull("Test XML file " + xmlFileName + " cannot be found", xmlStream);
Query result = coreParser().parse(xmlStream); if (span) {
return result; return coreParser().parseAsSpanQuery(xmlStream);
} else {
return coreParser().parse(xmlStream);
}
} }
} }