diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 2e89f3bc90d..328f742f748 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -153,6 +153,9 @@ Other * LUCENE-7666: Fix typos in lucene-join package info javadoc. (Tom Saleeba via Christine Poerschke) +* LUCENE-7658: queryparser/xml CoreParser now implements SpanQueryBuilder interface. + (Daniel Collins, Christine Poerschke) + ======================= Lucene 6.4.1 ======================= Bug Fixes diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java index d8aa8ef2b77..8637c4a6fcd 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java @@ -20,6 +20,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.queryparser.xml.builders.*; import org.apache.lucene.search.Query; +import org.apache.lucene.search.spans.SpanQuery; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -31,7 +32,7 @@ import java.io.InputStream; /** * 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 Analyzer analyzer; @@ -114,6 +115,11 @@ public class CoreParser implements QueryBuilder { 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) { queryFactory.addBuilder(nodeName, builder); } @@ -122,6 +128,11 @@ public class CoreParser implements QueryBuilder { 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 { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; @@ -144,4 +155,9 @@ public class CoreParser implements QueryBuilder { public Query getQuery(Element e) throws ParserException { return queryFactory.getQuery(e); } + + @Override + public SpanQuery getSpanQuery(Element e) throws ParserException { + return spanFactory.getSpanQuery(e); + } } diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java index 1906aef1e93..8f07c4a100a 100644 --- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; +import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.util.LuceneTestCase; import org.junit.AfterClass; @@ -116,6 +117,9 @@ public class TestCoreParser extends LuceneTestCase { public void testSpanTermXML() throws Exception { Query q = parse("SpanQuery.xml"); dumpResults("Span Query", q, 5); + SpanQuery sq = parseAsSpan("SpanQuery.xml"); + dumpResults("Span Query", sq, 5); + assertEquals(q, sq); } public void testConstantScoreQueryXML() throws Exception { @@ -207,10 +211,21 @@ public class TestCoreParser extends LuceneTestCase { } 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)) { assertNotNull("Test XML file " + xmlFileName + " cannot be found", xmlStream); - Query result = coreParser().parse(xmlStream); - return result; + if (span) { + return coreParser().parseAsSpanQuery(xmlStream); + } else { + return coreParser().parse(xmlStream); + } } }