Add ~N syntax to phrase queries in query parser to allow for user-settable slop

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149691 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brian Goetz 2002-02-19 00:44:00 +00:00
parent 1ec9086829
commit 7e7d8c6ac0
3 changed files with 24 additions and 1 deletions

View File

@ -288,6 +288,7 @@ PARSER_END(QueryParser)
| <QUOTED: "\"" (~["\""])+ "\"">
| <TERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* >
| <FUZZY: "~" >
| <SLOP: "~" (<_NUM_CHAR>)+ >
| <PREFIXTERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* "*" >
| <WILDTERM: <_TERM_START_CHAR>
(<_TERM_CHAR> | ( [ "*", "?" ] ))* >
@ -375,7 +376,7 @@ Query Clause(String field) : {
Query Term(String field) : {
Token term, boost=null;
Token term, boost=null, slop=null;
boolean prefix = false;
boolean wildcard = false;
boolean fuzzy = false;
@ -411,10 +412,18 @@ Query Term(String field) : {
rangein);
}
| term=<QUOTED>
[ slop=<SLOP> ]
[ <CARAT> boost=<NUMBER> ]
{
q = getFieldQuery(field, analyzer,
term.image.substring(1, term.image.length()-1));
if (slop != null && q instanceof PhraseQuery) {
try {
int s = Float.valueOf(slop.image.substring(1)).intValue();
((PhraseQuery) q).setSlop(s);
}
catch (Exception ignored) { }
}
}
)
{

View File

@ -163,6 +163,11 @@ final public class PhraseQuery extends Query {
}
buffer.append("\"");
if (slop != 0) {
buffer.append("~");
buffer.append(slop);
}
if (boost != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(boost));

View File

@ -166,6 +166,7 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("term^2.0", null, "term^2.0");
assertQueryEquals("term^2", null, "term^2.0");
assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
"+(foo bar) +(baz boo)");
@ -184,6 +185,14 @@ public class TestQueryParser extends TestCase {
assertQueryEquals(".NET", a, ".NET");
}
public void testSlop() throws Exception {
assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
assertQueryEquals("\"term germ\"~2 flork", null, "\"term germ\"~2 flork");
assertQueryEquals("\"term\"~2", null, "term");
assertQueryEquals("\" \"~2 germ", null, "germ");
assertQueryEquals("\"term germ\"~2^2", null, "\"term germ\"~2^2.0");
}
public void testNumber() throws Exception {
// The numbers go away because SimpleAnalzyer ignores them
assertQueryEquals("3", null, "");