.phrase() .withSlop(2) .onField(theFieldName).boostedTo(4.0f) } else { String joinedTerms = StringUtils.join(terms, ' '); theBoolean.must(theQueryBuilder.keyword().onField(theFieldName).matching(joinedTerms).createQuery());
TermContext keyword = buildQuery.keyword(); WildcardContext wildcard = keyword.wildcard(); String[] searchfields = Compound.getSearchfields(); TermMatchingContext onFields = wildcard.onField(searchfields[0]); for (int i = 1; i < searchfields.length; i++) onFields.andField(searchfields[i]); TermTermination matching = onFields.matching(input.toLowerCase()); Query query = matching.createQuery();
Date startPublishDate, Date endPublishDate, String orderField, Integer pageIndex, Integer pageSize) { QueryBuilder queryBuilder = getFullTextQueryBuilder(); MustJunction termination = queryBuilder.bool() .must(queryBuilder.keyword().onFields(CommonUtils.empty(tagId) ? textFields : tagFields) .matching(CommonUtils.empty(tagId) ? text : tagId).createQuery()) .must(new TermQuery(new Term("siteId", siteId.toString()))); if (null != startPublishDate) { termination.must(queryBuilder.range().onField("publishDate").above(startPublishDate).createQuery());
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); } }
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); }
entityManager); QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder() .forEntity(ArtificerArtifact.class).get(); BooleanJunction<BooleanJunction> junction = qb.bool(); junction.must(qb.keyword().onField("trashed").matching(false).createQuery()); junction.must(qb.keyword() .onFields("description", "name", "comments.text", "properties.key", "properties.value") .andField("content").ignoreFieldBridge() .andField("contentPath").ignoreFieldBridge() .matching(query) .createQuery()); qb.keyword().onField("model").matching(artifactModel).createQuery()); qb.keyword().onField("type").matching(artifactType).createQuery());
if (isMultiTermText(propertyValueExpr.getPropertyPath(), text)) { PhraseContext phrase = queryBuilder.phrase(); if (fullTextTermExpr.getFuzzySlop() != null) { phrase = phrase.withSlop(fullTextTermExpr.getFuzzySlop()); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword() .fuzzy().withEditDistanceUpTo(fullTextTermExpr.getFuzzySlop()) .onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery(); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery(); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery();
@Test public void testMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //combined query, January and February both contain whitening but February in a longer text Query query = monthQb.keyword() .onField( "mythology" ) .andField( "history" ) .matching( "whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //combined query, January and February both contain whitening but February in a longer text query = monthQb.keyword() .onFields( "mythology", "history" ) .boostedTo( 30 ) .matching( "whitening" ).createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //boosted query, January and February both contain whitening but February in a longer text //since history is boosted, February should come first though query = monthQb.keyword() .onField( "mythology" ) .andField( "history" ) .boostedTo( 30 ) .matching( "whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 1 ); }
TermMatchingContext wildcardTermMatchingContext() { if ( fieldBridge != null ) { return ( (ConnectedTermMatchingContext) builder.keyword().wildcard().onField( propertyName ) ).withFieldBridge( fieldBridge ).ignoreAnalyzer(); } else { return builder.keyword().wildcard().onField( propertyName ); } }
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); }
@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 ); }
if (isMultiTermText(propertyValueExpr.getPropertyPath(), text)) { PhraseContext phrase = queryBuilder.phrase(); if (fullTextTermExpr.getFuzzySlop() != null) { phrase = phrase.withSlop(fullTextTermExpr.getFuzzySlop()); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword() .fuzzy().withEditDistanceUpTo(fullTextTermExpr.getFuzzySlop()) .onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery(); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery(); return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(text).createQuery();
QueryBuilder qb = em.getSearchFactory().buildQueryBuilder().forEntity(ResourceTable.class).get(); BooleanJunction<?> bool = qb.bool(); bool.must(qb.keyword().onField("myResourceLinksField").matching(theReferencingPid.toString()).createQuery()); bool.must(qb.keyword().onField("myResourceType").matching(theResourceName).createQuery());
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addMustWildcardWithStartingAttributesOnFields(Map<String, String> startSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : startSentenceOnField.keySet()){ String value = startSentenceOnField.get(field); if(!value.isEmpty()){ String wildcardValue = value + "*"; Query luceneQuery = qb.keyword().wildcard() .onField(field) .matching(wildcardValue) .createQuery(); combinedQuery.must(luceneQuery); } } return combinedQuery; }
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); }
@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 ); }
Date startPublishDate, Date endPublishDate, String orderField, Integer pageIndex, Integer pageSize) { QueryBuilder queryBuilder = getFullTextQueryBuilder(); MustJunction termination = queryBuilder.bool() .must(queryBuilder.keyword().onFields(CommonUtils.empty(tagId) ? textFields : tagFields) .matching(CommonUtils.empty(tagId) ? text : tagId).createQuery()) .must(new TermQuery(new Term("siteId", siteId.toString()))); if (null != startPublishDate) { termination.must(queryBuilder.range().onField("publishDate").above(startPublishDate).createQuery());
QueryBuilder qb = em.getSearchFactory().buildQueryBuilder().forEntity(ResourceTable.class).get(); .phrase() .withSlop(2) .onField("myContentText").boostedTo(4.0f) .sentence(theText.toLowerCase()).createQuery(); Query query = qb.bool() .must(qb.keyword().onField("myResourceLinksField").matching(pid.toString()).createQuery()) .must(textQuery) .createQuery();
return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(lucenePattern.toString()).createQuery();