You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by er...@apache.org on 2014/03/16 19:23:09 UTC
svn commit: r1578134 - in /lucene/dev/branches/branch_4x: ./ lucene/
lucene/queryparser/
lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/
lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/
Author: erick
Date: Sun Mar 16 18:23:09 2014
New Revision: 1578134
URL: http://svn.apache.org/r1578134
Log:
Fix for LUCENE-3578, the ability to specify order for complex phrase queries
Modified:
lucene/dev/branches/branch_4x/ (props changed)
lucene/dev/branches/branch_4x/lucene/ (props changed)
lucene/dev/branches/branch_4x/lucene/CHANGES.txt (contents, props changed)
lucene/dev/branches/branch_4x/lucene/queryparser/ (props changed)
lucene/dev/branches/branch_4x/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java
lucene/dev/branches/branch_4x/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
Modified: lucene/dev/branches/branch_4x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/CHANGES.txt?rev=1578134&r1=1578133&r2=1578134&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/lucene/CHANGES.txt Sun Mar 16 18:23:09 2014
@@ -44,6 +44,9 @@ New Features
* LUCENE-5493: SortingMergePolicy, and EarlyTerminatingSortingCollector
support arbitrary Sort specifications.
(Robert Muir, Mike McCandless, Adrien Grand)
+
+* LUCENE-3758: Allow the ComplexPhraseQueryParser to search order or
+ un-order proximity queries. (Ahmet Arslan via Erick Erickson)
API Changes
Modified: lucene/dev/branches/branch_4x/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java?rev=1578134&r1=1578133&r2=1578134&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java (original)
+++ lucene/dev/branches/branch_4x/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java Sun Mar 16 18:23:09 2014
@@ -66,6 +66,18 @@ public class ComplexPhraseQueryParser ex
private boolean isPass2ResolvingPhrases;
+ private boolean inOrder = true;
+
+ /**
+ * When <code>inOrder</code> is true, the search terms must
+ * exists in the documents as the same order as in query.
+ *
+ * @param inOrder parameter to choose between ordered or un-ordered proximity search
+ */
+ public void setInOrder(final boolean inOrder) {
+ this.inOrder = inOrder;
+ }
+
private ComplexPhraseQuery currentPhraseQuery = null;
public ComplexPhraseQueryParser(Version matchVersion, String f, Analyzer a) {
@@ -74,7 +86,7 @@ public class ComplexPhraseQueryParser ex
@Override
protected Query getFieldQuery(String field, String queryText, int slop) {
- ComplexPhraseQuery cpq = new ComplexPhraseQuery(field, queryText, slop);
+ ComplexPhraseQuery cpq = new ComplexPhraseQuery(field, queryText, slop, inOrder);
complexPhrases.add(cpq); // add to list of phrases to be parsed once
// we
// are through with this pass
@@ -202,14 +214,17 @@ public class ComplexPhraseQueryParser ex
int slopFactor;
+ private final boolean inOrder;
+
private Query contents;
public ComplexPhraseQuery(String field, String phrasedQueryStringContents,
- int slopFactor) {
+ int slopFactor, boolean inOrder) {
super();
this.field = field;
this.phrasedQueryStringContents = phrasedQueryStringContents;
this.slopFactor = slopFactor;
+ this.inOrder = inOrder;
}
// Called by ComplexPhraseQueryParser for each phrase after the main
@@ -280,7 +295,7 @@ public class ComplexPhraseQueryParser ex
}
if (numNegatives == 0) {
// The simple case - no negative elements in phrase
- return new SpanNearQuery(allSpanClauses, slopFactor, true);
+ return new SpanNearQuery(allSpanClauses, slopFactor, inOrder);
}
// Complex case - we have mixed positives and negatives in the
// sequence.
@@ -302,11 +317,11 @@ public class ComplexPhraseQueryParser ex
// need to increase slop factor based on gaps introduced by
// negatives
include = new SpanNearQuery(includeClauses, slopFactor + numNegatives,
- true);
+ inOrder);
}
// Use sequence of positive and negative values as the exclude.
SpanNearQuery exclude = new SpanNearQuery(allSpanClauses, slopFactor,
- true);
+ inOrder);
SpanNotQuery snot = new SpanNotQuery(include, exclude);
return snot;
}
Modified: lucene/dev/branches/branch_4x/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java?rev=1578134&r1=1578133&r2=1578134&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java (original)
+++ lucene/dev/branches/branch_4x/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java Sun Mar 16 18:23:09 2014
@@ -48,6 +48,8 @@ public class TestComplexPhraseQuery exte
String defaultFieldName = "name";
+ boolean inOrder = true;
+
public void testComplexPhrases() throws Exception {
checkMatches("\"john smith\"", "1"); // Simple multi-term still works
checkMatches("\"j* smyth~\"", "1,2"); // wildcards and fuzzies are OK in
@@ -71,8 +73,20 @@ public class TestComplexPhraseQuery exte
checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
}
+
+ public void testUnOrderedProximitySearches() throws Exception {
+
+ inOrder = true;
+ checkMatches("\"smith jo*\"~2", ""); // ordered proximity produces empty set
+
+ inOrder = false;
+ checkMatches("\"smith jo*\"~2", "1,2,3"); // un-ordered proximity
+
+ }
+
private void checkBadQuery(String qString) {
- QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
+ ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
+ qp.setInOrder(inOrder);
Throwable expected = null;
try {
qp.parse(qString);
@@ -85,7 +99,8 @@ public class TestComplexPhraseQuery exte
private void checkMatches(String qString, String expectedVals)
throws Exception {
- QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
+ ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
+ qp.setInOrder(inOrder);
qp.setFuzzyPrefixLength(1); // usually a good idea
Query q = qp.parse(qString);