Make QueryParser work with analyzers that return tokens with a position

increment > 1. Thanks to Ahmed El-dawy for the fix.


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@331923 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2005-11-08 23:05:44 +00:00
parent 2f2f78e731
commit 2ce046a7a6
3 changed files with 63 additions and 9 deletions

View File

@ -381,8 +381,8 @@ public class QueryParser implements QueryParserConstants {
if (t == null) if (t == null)
break; break;
v.addElement(t); v.addElement(t);
if (t.getPositionIncrement() == 1) if (t.getPositionIncrement() != 0)
positionCount++; positionCount += t.getPositionIncrement();
else else
severalTokensAtSamePosition = true; severalTokensAtSamePosition = true;
} }

View File

@ -404,8 +404,8 @@ public class QueryParser {
if (t == null) if (t == null)
break; break;
v.addElement(t); v.addElement(t);
if (t.getPositionIncrement() == 1) if (t.getPositionIncrement() != 0)
positionCount++; positionCount += t.getPositionIncrement();
else else
severalTokensAtSamePosition = true; severalTokensAtSamePosition = true;
} }

View File

@ -22,6 +22,7 @@ import junit.framework.TestCase;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LowerCaseFilter; import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardTokenizer; import org.apache.lucene.analysis.standard.StandardTokenizer;
@ -36,9 +37,9 @@ public class TestMultiAnalyzer extends TestCase {
private static int multiToken = 0; private static int multiToken = 0;
public void testAnalyzer() throws ParseException { public void testMultiAnalyzer() throws ParseException {
QueryParser qp = new QueryParser("", new TestAnalyzer()); QueryParser qp = new QueryParser("", new MultiAnalyzer());
// trivial, no multiple tokens: // trivial, no multiple tokens:
assertEquals("foo", qp.parse("foo").toString()); assertEquals("foo", qp.parse("foo").toString());
@ -80,13 +81,21 @@ public class TestMultiAnalyzer extends TestCase {
} }
public void testPosIncrementAnalyzer() throws ParseException {
QueryParser qp = new QueryParser("", new PosIncrementAnalyzer());
assertEquals("quick brown", qp.parse("the quick brown").toString());
assertEquals("\"quick brown\"", qp.parse("\"the quick brown\"").toString());
assertEquals("quick brown fox", qp.parse("the quick brown fox").toString());
assertEquals("\"quick brown fox\"", qp.parse("\"the quick brown fox\"").toString());
}
/** /**
* Expands "multi" to "multi" and "multi2", both at the same position, * Expands "multi" to "multi" and "multi2", both at the same position,
* and expands "triplemulti" to "triplemulti", "multi3", and "multi2". * and expands "triplemulti" to "triplemulti", "multi3", and "multi2".
*/ */
private class TestAnalyzer extends Analyzer { private class MultiAnalyzer extends Analyzer {
public TestAnalyzer() { public MultiAnalyzer() {
} }
public TokenStream tokenStream(String fieldName, Reader reader) { public TokenStream tokenStream(String fieldName, Reader reader) {
@ -132,4 +141,49 @@ public class TestMultiAnalyzer extends TestCase {
} }
} }
/**
* Analyzes "the quick brown" as: quick(incr=2) brown(incr=1).
* Does not work correctly for input other than "the quick brown ...".
*/
private class PosIncrementAnalyzer extends Analyzer {
public PosIncrementAnalyzer() {
}
public TokenStream tokenStream(String fieldName, Reader reader) {
TokenStream result = new StandardTokenizer(reader);
result = new TestPosIncrementFilter(result);
result = new LowerCaseFilter(result);
return result;
}
}
private final class TestPosIncrementFilter extends TokenFilter {
public TestPosIncrementFilter(TokenStream in) {
super(in);
}
public final org.apache.lucene.analysis.Token next() throws java.io.IOException {
for (Token t = input.next(); t != null; t = input.next()) {
if (t.termText().equals("the")) {
// stopword, do nothing
} else if (t.termText().equals("quick")) {
org.apache.lucene.analysis.Token token =
new org.apache.lucene.analysis.Token(t.termText(), t.startOffset(),
t.endOffset(), t.type());
token.setPositionIncrement(2);
return token;
} else {
org.apache.lucene.analysis.Token token =
new org.apache.lucene.analysis.Token(t.termText(), t.startOffset(),
t.endOffset(), t.type());
token.setPositionIncrement(1);
return token;
}
}
return null;
}
}
} }