Apache Lucene open-source search software
LUCENE-5530 Allow the ComplexPhraseQueryParser to search order or un-order proximity queries.
Erick Ericksonbcedf1bba16c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 4988a2f5ef5..9fcb57dc1cc 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -100,6 +100,9 @@ New Features* LUCENE-3758: Allow the ComplexPhraseQueryParser to search order orun-order proximity queries. (Ahmet Arslan via Erick Erickson)++* LUCENE-5530: ComplexPhraseQueryParser throws ParseException for fielded queries.+ (Erick Erickson via Tomas Fernandez Lobbe and Ahmet Arslan)API Changesdiff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/QueryParserBase.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/QueryParserBase.javaindex 0b8d803b17e..b42dd36fc52 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/QueryParserBase.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/QueryParserBase.java@@ -65,7 +65,7 @@ public abstract class QueryParserBase extends QueryBuilder implements CommonQuerMultiTermQuery.RewriteMethod multiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;boolean allowLeadingWildcard = false;- String field;+ protected String field;int phraseSlop = 0;float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity;int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.javaindex 0472d9943f0..c32f4c249ef 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java@@ -230,13 +230,22 @@ public class ComplexPhraseQueryParser extends QueryParser {// Called by ComplexPhraseQueryParser for each phrase after the main// parse// thread is through- protected void parsePhraseElements(QueryParser qp) throws ParseException {+ protected void parsePhraseElements(ComplexPhraseQueryParser qp) throws ParseException {// TODO ensure that field-sensitivity is preserved ie the query// string below is parsed as// field+":("+phrasedQueryStringContents+")"// but this will need code in rewrite to unwrap the first layer of// boolean query- contents = qp.parse(phrasedQueryStringContents);++ String oldDefaultParserField = qp.field;+ try {+ //temporarily set the QueryParser to be parsing the default field for this phrase e.g author:"fred* smith"+ qp.field = this.field;+ contents = qp.parse(phrasedQueryStringContents);+ }+ finally {+ qp.field = oldDefaultParserField;+ }}@Overridediff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.javaindex 5540b6e5f5f..e29ecb0704c 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java@@ -38,11 +38,12 @@ import org.apache.lucene.util.LuceneTestCase;public class TestComplexPhraseQuery extends LuceneTestCase {Directory rd;Analyzer analyzer;-- DocData docsContent[] = { new DocData("john smith", "1"),- new DocData("johathon smith", "2"),- new DocData("john percival smith", "3"),- new DocData("jackson waits tom", "4") };+ DocData docsContent[] = {+ new DocData("john smith", "1", "developer"),+ new DocData("johathon smith", "2", "developer"),+ new DocData("john percival smith", "3", "designer"),+ new DocData("jackson waits tom", "4", "project manager")+ };private IndexSearcher searcher;private IndexReader reader;@@ -126,7 +127,19 @@ public class TestComplexPhraseQuery extends LuceneTestCase {assertEquals(qString + " missing some matches ", 0, expecteds.size());}-++ public void testFieldedQuery() throws Exception {+ checkMatches("name:\"john smith\"", "1");+ checkMatches("name:\"j* smyth~\"", "1,2");+ checkMatches("role:\"developer\"", "1,2");+ checkMatches("role:\"p* manager\"", "4");+ checkMatches("role:de*", "1,2,3");+ checkMatches("name:\"j* smyth~\"~5", "1,2,3");+ checkMatches("role:\"p* manager\" AND name:jack*", "4");+ checkMatches("+role:developer +name:jack*", "");+ checkMatches("name:\"john smith\"~2 AND role:designer AND id:3", "3");+ }+@Overridepublic void setUp() throws Exception {super.setUp();@@ -138,6 +151,7 @@ public class TestComplexPhraseQuery extends LuceneTestCase {Document doc = new Document();doc.add(newTextField("name", docsContent[i].name, Field.Store.YES));doc.add(newTextField("id", docsContent[i].id, Field.Store.YES));+ doc.add(newTextField("role", docsContent[i].role, Field.Store.YES));w.addDocument(doc);}w.close();@@ -156,11 +170,14 @@ public class TestComplexPhraseQuery extends LuceneTestCase {String name;String id;++ String role;- public DocData(String name, String id) {+ public DocData(String name, String id, String role) {super();this.name = name;this.id = id;+ this.role = role;}}