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]);
@Override public Page<User> searchByKeyword(@Param("q") String keywords, Pageable pageable) { if(StringUtils.isBlank(keywords)) { keywords = "*"; } // Must be retrieved inside a transaction to take part of final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); // Prepare a search query builder final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(User.class).get(); // This is a boolean junction... I'll add at least a keyword query final BooleanJunction<BooleanJunction> outer = queryBuilder.bool(); outer.must( queryBuilder .keyword() .wildcard() .onFields("username", "fullName") .matching(keywords) .createQuery() ); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(outer.createQuery(), User.class); fullTextQuery.setFirstResult(pageable.getOffset()); fullTextQuery.setMaxResults(pageable.getPageSize()); fullTextQuery.setSort(getSearchSort(pageable)); return new PageImpl<>(fullTextQuery.getResultList(), pageable, fullTextQuery.getResultSize()); }
@Test @TestForIssue(jiraKey = "HSEARCH-1811") public void testWildcardQueryOnMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .wildcard() .onFields( "mythology", "history" ) .matching( "snowbo*" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 3 ); }
@Override public Page<Role> searchByKeyword(String keywords, Pageable pageable) { // Must be retrieved inside a transaction to take part of final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); // Prepare a search query builder final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Role.class).get(); // This is a boolean junction... I'll add at least a keyword query final BooleanJunction<BooleanJunction> outer = queryBuilder.bool(); outer.must( queryBuilder .keyword() .wildcard() .onFields("name") .matching(keywords) .createQuery() ); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(outer.createQuery(), Role.class); fullTextQuery.setFirstResult(pageable.getOffset()); fullTextQuery.setMaxResults(pageable.getPageSize()); fullTextQuery.setSort(getSearchSort(pageable)); return new PageImpl<>(fullTextQuery.getResultList(), pageable, fullTextQuery.getResultSize()); }
@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; }
@Override public Page<DataSource> searchByKeyword(String keywords, Pageable pageable) { // Must be retrieved inside a transaction to take part of final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); // Prepare a search query builder final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(DataSource.class).get(); // This is a boolean junction... I'll add at least a keyword query final BooleanJunction<BooleanJunction> outer = queryBuilder.bool(); outer.must( queryBuilder .keyword() .wildcard() .onFields("name", "description", "alias") .matching(keywords) .createQuery() ); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(outer.createQuery(), DataSource.class); fullTextQuery.setFirstResult(pageable.getOffset()); fullTextQuery.setMaxResults(pageable.getPageSize()); fullTextQuery.setSort(getSearchSort(pageable)); return new PageImpl<>(fullTextQuery.getResultList(), pageable, fullTextQuery.getResultSize()); }
return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(lucenePattern.toString()).createQuery();
return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(lucenePattern.toString()).createQuery();
@Test @SuppressWarnings("unchecked") public void testResultOrderedByDateDescending() throws Exception { EntityTransaction tx = em.getTransaction(); tx.begin(); QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( ProductArticle.class ).get(); org.apache.lucene.search.Query query = builder.keyword().wildcard().onField( "header" ).matching( "hib*" ).createQuery(); Sort dateDescending = builder.sort().byField( "creationDate" ).desc().createSort(); List<ProductArticle> result = em.createFullTextQuery( query, ProductArticle.class ) .setSort( dateDescending ).setFirstResult( 3 ).getResultList(); assertThat( result ).as( "query result" ).hasSize( 3 ); assertThat( result.get( 0 ).getArticleId() ).as( "article id" ).isEqualTo( 3 ); assertThat( result.get( 1 ).getArticleId() ).as( "article id" ).isEqualTo( 2 ); assertThat( result.get( 2 ).getArticleId() ).as( "article id" ).isEqualTo( 1 ); tx.commit(); em.clear(); }
Query query = qb.keyword().wildcard().onField( "owner" ).matching( "p*" ).createQuery();
@Test(expectedExceptions = SearchException.class) public void testWildcardWithWrongName() { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().keyword().wildcard() .onField("wrongname").matching("Goat").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(2, found.size()); }
return applyFieldBridge(true, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().wildcard().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 testWildcardQuery() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .wildcard() .onField( "mythology" ) .matching( "mon*" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 3 ); }
public void testWildcard() { loadNumericTypes(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().keyword().wildcard() .onField("name").matching("*wildcard*").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.contains(type1); assert found.contains(type2); assert found.contains(type3); query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().keyword().wildcard() .onField("name").matching("nothing*").createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(0, found.size()); NumericType type4 = new NumericType(35, 40); type4.setName("nothing special."); cache.put("otherKey", type4); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(type4); query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().keyword().wildcard() .onField("name").matching("*nothing*").createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(type2); assert found.contains(type4); }
private List<Clock> searchModel(String searchString, String tenantId) { FullTextSession session = Search.getFullTextSession( openSessionWithTenantId( tenantId ) ); QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( Clock.class ).get(); Query luceneQuery = queryBuilder.keyword().wildcard().onField( "brand" ).matching( searchString ).createQuery(); Transaction transaction = session.beginTransaction(); @SuppressWarnings("unchecked") List<Clock> list = session.createFullTextQuery( luceneQuery ).list(); transaction.commit(); session.clear(); session.close(); return list; }
public void testAllExceptWithoutAnalyzer() { loadNumericTypes(); Query subQuery = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().keyword() .wildcard().onField("name").matching("*string*").createQuery(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().all().except().createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.contains(type1); assert found.contains(type2); assert found.contains(type3); query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().all().except(subQuery).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(0, found.size()); }
@Test public void testSimpleEntityMassIndexing() throws Exception { { Session session = openSession(); Transaction transaction = session.beginTransaction(); Insurance insurance = new Insurance(); insurance.setName( "Insurance Corporation" ); session.persist( insurance ); transaction.commit(); session.clear(); session.close(); } { purgeAll( Insurance.class ); startAndWaitMassIndexing( Insurance.class ); } { FullTextSession session = Search.getFullTextSession( openSession() ); QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( Insurance.class ).get(); Query luceneQuery = queryBuilder.keyword().wildcard().onField( "name" ).matching( "ins*" ).createQuery(); Transaction transaction = session.beginTransaction(); @SuppressWarnings("unchecked") List<Insurance> list = session.createFullTextQuery( luceneQuery ).list(); assertThat( list ).hasSize( 1 ); assertThat( list.get( 0 ).getName() ).isEqualTo( "Insurance Corporation" ); transaction.commit(); session.clear(); session.close(); } }
@SuppressWarnings("unchecked") private void assertAssociatedElementsHaveBeenIndexed() throws Exception { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( createEntityManager() ); fullTextEm.getTransaction().begin(); QueryBuilder b = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity( IndexedLabel.class ).get(); { Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "tes*" ).createQuery(); List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList(); assertThat( labels ).hasSize( 1 ); assertThat( contains( labels, "test" ) ).isTrue(); } { Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "mas*" ).createQuery(); List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList(); assertThat( labels ).hasSize( 1 ); assertThat( contains( labels, "massindex" ) ).isTrue(); } fullTextEm.getTransaction().commit(); fullTextEm.close(); }