.fuzzy().withEditDistanceUpTo(fullTextTermExpr.getFuzzySlop()) .onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery();
.fuzzy().withEditDistanceUpTo(fullTextTermExpr.getFuzzySlop()) .onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery();
public void testFuzzyWithThresholdWithPrefixLength() { person1 = new Person("yyJohn", "Eat anything", 10); person2 = new Person("yyJonn", "Eat anything", 10); cache.put(key1, person1); cache.put(key2, person2); //Ignore "yy" at the beginning (prefix==2), the difference between the remaining parts of two terms //must be no more than edit distance -> return only 1 person Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword() .fuzzy().withEditDistanceUpTo(1).withPrefixLength(2).onField("name").matching("yyJohny").createQuery(); List<Object> found = Search.getSearchManager(cache).getQuery(query).list(); assertEquals(1, found.size()); assertTrue(found.contains(person1)); //return all as edit distance excluding the prefix fit all documents Query queryReturnAll = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword() .fuzzy().withEditDistanceUpTo(2).withPrefixLength(2).onField("name").matching("yyJohn").createQuery(); List<Object> foundWithLowerThreshold = Search.getSearchManager(cache).getQuery(queryReturnAll).list(); assertEquals(2, foundWithLowerThreshold.size()); assertTrue(foundWithLowerThreshold.contains(person1)); assertTrue(foundWithLowerThreshold.contains(person2)); }
@Test public void testFuzzyQuery() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //fuzzy search with custom threshold and prefix Query query = monthQb .keyword() .fuzzy() .withEditDistanceUpTo( 1 ) .withPrefixLength( 1 ) .onField( "mythology" ) .matching( "calder" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test public void testFuzzyQueryOnMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .fuzzy() .withEditDistanceUpTo( 2 ) .withPrefixLength( 1 ) .onFields( "mythology", "history" ) .matching( "showboarding" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 2 ); }
@Test public void testDSLKeywordWithFuzziness() throws Exception { try ( Session session = openSession() ) { FullTextSession fullTextSession = Search.getFullTextSession( session ); final QueryBuilder queryBuilder = queryBuilder( fullTextSession ); Query query = queryBuilder .keyword() .fuzzy() .withEditDistanceUpTo( 2 ) .onField( "message" ) .matching( "A very important matter" ) .createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class ); String queryString = fullTextQuery.getQueryString(); assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','fuzziness':2}}}}", queryString ); } }