LUCENE-5530 Allow the ComplexPhraseQueryParser to search order or un-order proximity queries.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1578148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erick Erickson 2014-03-16 19:48:39 +00:00
parent e3779cf420
commit bcedf1bba1
4 changed files with 39 additions and 10 deletions

View File

@ -100,6 +100,9 @@ New Features
* LUCENE-3758: Allow the ComplexPhraseQueryParser to search order or * LUCENE-3758: Allow the ComplexPhraseQueryParser to search order or
un-order proximity queries. (Ahmet Arslan via Erick Erickson) un-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 Changes API Changes

View File

@ -65,7 +65,7 @@ public abstract class QueryParserBase extends QueryBuilder implements CommonQuer
MultiTermQuery.RewriteMethod multiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; MultiTermQuery.RewriteMethod multiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
boolean allowLeadingWildcard = false; boolean allowLeadingWildcard = false;
String field; protected String field;
int phraseSlop = 0; int phraseSlop = 0;
float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity; float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity;
int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength; int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;

View File

@ -230,13 +230,22 @@ public class ComplexPhraseQueryParser extends QueryParser {
// Called by ComplexPhraseQueryParser for each phrase after the main // Called by ComplexPhraseQueryParser for each phrase after the main
// parse // parse
// thread is through // 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 // TODO ensure that field-sensitivity is preserved ie the query
// string below is parsed as // string below is parsed as
// field+":("+phrasedQueryStringContents+")" // field+":("+phrasedQueryStringContents+")"
// but this will need code in rewrite to unwrap the first layer of // but this will need code in rewrite to unwrap the first layer of
// boolean query // 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;
}
} }
@Override @Override

View File

@ -38,11 +38,12 @@ import org.apache.lucene.util.LuceneTestCase;
public class TestComplexPhraseQuery extends LuceneTestCase { public class TestComplexPhraseQuery extends LuceneTestCase {
Directory rd; Directory rd;
Analyzer analyzer; Analyzer analyzer;
DocData docsContent[] = {
DocData docsContent[] = { new DocData("john smith", "1"), new DocData("john smith", "1", "developer"),
new DocData("johathon smith", "2"), new DocData("johathon smith", "2", "developer"),
new DocData("john percival smith", "3"), new DocData("john percival smith", "3", "designer"),
new DocData("jackson waits tom", "4") }; new DocData("jackson waits tom", "4", "project manager")
};
private IndexSearcher searcher; private IndexSearcher searcher;
private IndexReader reader; private IndexReader reader;
@ -126,7 +127,19 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
assertEquals(qString + " missing some matches ", 0, expecteds.size()); 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");
}
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -138,6 +151,7 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
Document doc = new Document(); Document doc = new Document();
doc.add(newTextField("name", docsContent[i].name, Field.Store.YES)); doc.add(newTextField("name", docsContent[i].name, Field.Store.YES));
doc.add(newTextField("id", docsContent[i].id, 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.addDocument(doc);
} }
w.close(); w.close();
@ -156,11 +170,14 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
String name; String name;
String id; String id;
String role;
public DocData(String name, String id) { public DocData(String name, String id, String role) {
super(); super();
this.name = name; this.name = name;
this.id = id; this.id = id;
this.role = role;
} }
} }