diff --git a/querydsl-hibernate-search/src/main/java/com/mysema/query/search/LuceneSerializer.java b/querydsl-hibernate-search/src/main/java/com/mysema/query/search/LuceneSerializer.java index 6d5b02ab0..9635c932a 100644 --- a/querydsl-hibernate-search/src/main/java/com/mysema/query/search/LuceneSerializer.java +++ b/querydsl-hibernate-search/src/main/java/com/mysema/query/search/LuceneSerializer.java @@ -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(); } diff --git a/querydsl-hibernate-search/src/test/java/com/mysema/query/search/SimpleTest.java b/querydsl-hibernate-search/src/test/java/com/mysema/query/search/SimpleTest.java index 0791bd613..14127784a 100644 --- a/querydsl-hibernate-search/src/test/java/com/mysema/query/search/SimpleTest.java +++ b/querydsl-hibernate-search/src/test/java/com/mysema/query/search/SimpleTest.java @@ -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"));