Added support for endsWith.

This commit is contained in:
Vesa Martilla 2010-03-19 14:33:16 +00:00
parent ad2b0c7a39
commit 4d709d9a6d
2 changed files with 29 additions and 1 deletions

View File

@ -107,7 +107,7 @@ public class LuceneSerializer {
String term = operation.getArg(1).toString();
return new PrefixQuery(new Term(toField((PString) operation.getArg(0)), lowerCase ? term.toLowerCase() : term));
} else if (op == Ops.STRING_CONTAINS) {
// TODO This is basically a special case of Ops.LIKE
// TODO This is basically a special case of Ops.LIKE *...*
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
}
@ -117,6 +117,17 @@ public class LuceneSerializer {
toPhraseQuery(operation, terms);
}
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (lowerCase ? term.toLowerCase() : term) + "*"));
} else if (op == Ops.ENDS_WITH) {
// TODO Speacial case of Ops.LIKE *...
if (!(operation.getArg(1) instanceof Constant<?>)) {
throw new IllegalArgumentException("operation argument was not of type Constant.");
}
String term = QueryParser.escape(operation.getArg(1).toString());
String[] terms = StringUtils.split(term);
if (terms.length > 1) {
toPhraseQuery(operation, terms);
}
return new WildcardQuery(new Term(toField((PString) operation.getArg(0)), "*" + (lowerCase ? term.toLowerCase() : term)));
}
throw new UnsupportedOperationException();
}

View File

@ -235,6 +235,23 @@ public class SimpleTest {
assertEquals("title:jurassic par*", q.toString());
}
@Test
public void test_endsWith() throws Exception {
Query q = serializer.toQuery(title.endsWith("ark"));
TopDocs docs = searcher.search(q, 100);
assertEquals(1, docs.totalHits);
assertEquals("title:*ark", q.toString());
}
@Test
@Ignore
public void test_endsWith_Phrase() throws Exception {
Query q = serializer.toQuery(title.startsWith("Jurassic Par"));
TopDocs docs = searcher.search(q, 100);
assertEquals(1, docs.totalHits);
assertEquals("title:*sic park", q.toString());
}
@Test
public void test_contains() throws Exception {
Query q = serializer.toQuery(title.contains("rassi"));