LUCENE-1396: improve PhraseQuery.toString: gaps are shown with a ? and multiple terms at the same position are joined with |

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@697469 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-09-21 10:41:41 +00:00
parent c206a378b7
commit 6927ee46a4
3 changed files with 39 additions and 3 deletions

View File

@ -57,6 +57,11 @@ Changes in runtime behavior
adding the same Directory more than once was causing duplicates
which led to problems (Mike McCandless)
4. LUCENE-1396: Improve PhraseQuery.toString() so that gaps in the
positions are indicated with a ? and multiple terms at the same
position are joined with a |. (Andrzej Bialecki via Mike
McCandless)
API Changes
1. LUCENE-1084: Changed all IndexWriter constructors to take an

View File

@ -35,6 +35,7 @@ public class PhraseQuery extends Query {
private String field;
private ArrayList terms = new ArrayList(4);
private ArrayList positions = new ArrayList(4);
private int maxPosition = 0;
private int slop = 0;
/** Constructs an empty phrase query. */
@ -87,6 +88,7 @@ public class PhraseQuery extends Query {
terms.add(term);
positions.add(new Integer(position));
if (position > maxPosition) maxPosition = position;
}
/** Returns the set of terms in this phrase. */
@ -261,10 +263,27 @@ public class PhraseQuery extends Query {
}
buffer.append("\"");
String[] pieces = new String[maxPosition + 1];
for (int i = 0; i < terms.size(); i++) {
buffer.append(((Term)terms.get(i)).text());
if (i != terms.size()-1)
buffer.append(" ");
int pos = ((Integer)positions.get(i)).intValue();
String s = pieces[pos];
if (s == null) {
s = ((Term)terms.get(i)).text();
} else {
s = s + "|" + ((Term)terms.get(i)).text();
}
pieces[pos] = s;
}
for (int i = 0; i < pieces.length; i++) {
if (i > 0) {
buffer.append(' ');
}
String s = pieces[i];
if (s == null) {
buffer.append('?');
} else {
buffer.append(s);
}
}
buffer.append("\"");

View File

@ -22,6 +22,7 @@ import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
@ -354,6 +355,17 @@ public class TestPhraseQuery extends LuceneTestCase {
assertEquals(2, hits[2].doc);
QueryUtils.check(query,searcher);
}
public void testToString() throws Exception {
StopAnalyzer analyzer = new StopAnalyzer();
StopFilter.setEnablePositionIncrementsDefault(true);
QueryParser qp = new QueryParser("field", analyzer);
qp.setEnablePositionIncrements(true);
PhraseQuery q = (PhraseQuery)qp.parse("\"this hi this is a test is\"");
assertEquals("field:\"? hi ? ? ? test\"", q.toString());
q.add(new Term("field", "hello"), 1);
assertEquals("field:\"? hi|hello ? ? ? test\"", q.toString());
}
public void testWrappedPhrase() throws IOException {
query.add(new Term("repeated", "first"));