@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 ); }
private FullTextQuery getSearchByNameQuery(String searchTerm) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager()); QueryBuilder projectQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder() .forEntity(Project.class).get(); BooleanJunction<?> booleanJunction = projectQueryBuilder.bool(); if (StringUtils.hasText(searchTerm)) { booleanJunction.must(projectQueryBuilder .keyword() .fuzzy().withPrefixLength(1).withThreshold(0.8F) .onField(Binding.project().name().getPath()) .matching(searchTerm) .createQuery()); } else { booleanJunction.must(projectQueryBuilder.all().createQuery()); } return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), Project.class); } }
@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 ); } }
public void testFuzzyOnFieldsAndField() { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword().fuzzy(). onField("name").matching("Goat").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(person2); assert found.contains(person3); person4 = new Person(); person4.setName("Test"); person4.setBlurb("Test goat"); cache.put("testKey", person4); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword().fuzzy(). onField("name").andField("blurb").matching("goat").createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(3, found.size()); assert found.contains(person2); assert found.contains(person3); assert found.contains(person4); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword().fuzzy(). onFields("name", "blurb").matching("goat").createQuery(); List<?> foundOnFields = Search.getSearchManager(cache).getQuery(query).list(); assertEquals(3, found.size()); assert found.contains(person2); assert found.contains(person3); assert found.contains(person4); }
.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)); }
.fuzzy().withEditDistanceUpTo(fullTextTermExpr.getFuzzySlop()) .onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery();
private FullTextQuery getSearchQuery(String searchTerm) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager()); QueryBuilder userQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder() .forEntity(User.class).get(); BooleanJunction<?> booleanJunction = userQueryBuilder.bool(); if (StringUtils.hasText(searchTerm)) { booleanJunction.must(userQueryBuilder .keyword() .fuzzy().withPrefixLength(1).withThreshold(0.8F) .onField(Binding.user().userName().getPath()) .andField(Binding.user().fullName().getPath()) .matching(searchTerm) .createQuery()); } else { booleanJunction.must(userQueryBuilder.all().createQuery()); } return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), User.class); }
private FullTextQuery getSearchByNameQuery(String searchTerm, ArtifactDeprecationStatus deprecation) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager()); QueryBuilder artifactQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder() .forEntity(Artifact.class).get(); BooleanJunction<?> booleanJunction = artifactQueryBuilder.bool(); if (deprecation != null) { booleanJunction.must(artifactQueryBuilder .keyword() .onField(Binding.artifact().deprecationStatus().getPath()) .matching(deprecation) .createQuery()); } if (StringUtils.hasText(searchTerm)) { booleanJunction.must(artifactQueryBuilder .keyword() .fuzzy().withPrefixLength(1).withThreshold(0.8F) .onField(Binding.artifact().artifactId().getPath()) .andField(Binding.artifact().group().groupId().getPath()) .matching(searchTerm) .createQuery()); } else { booleanJunction.must(artifactQueryBuilder.all().createQuery()); } return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), Artifact.class); }