@Override public FullTextQuery getFTSQuery(String filterText, final Class<?> entityType, Integer maxResults, String... fields) { // entityManager para busquedas de tipo FTS final FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager); // se crea el query usando Hibernate Search query DSL final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(entityType).get(); // se crea el query sobre los campos indicados final Query query = queryBuilder.keyword().onFields(fields).matching(filterText.trim()).createQuery(); // se enmascara el query de Lucene en uno de Hibernate final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, entityType); // se define la cantidad maxima de resultados si es mayor a cero if (maxResults > 0) { jpaQuery.setMaxResults(maxResults); } // se retorna el query listo para ejecución o para inyeccion de criterias return jpaQuery; }
@Transactional @Override public Page<Book> search(String query, Pageable page) { FullTextEntityManager manager = this.getFullTextEntityManager(); QueryBuilder builder = manager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get(); Query luceneQuery = builder.keyword().fuzzy() .onFields("author", "title", "lang", "description", "isbn", "genre", "location").matching(query) .createQuery(); FullTextQuery q = manager.createFullTextQuery(luceneQuery, Book.class); q.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE); long total = q.getResultSize(); q.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize()); @SuppressWarnings("unchecked") List<Object[]> result = q.getResultList(); List<Book> fine = new LinkedList<>(); for (Object[] i : result) { fine.add((Book) i[0]); } return new PageImpl<>(fine, page, total); }
@Test public void searchSubscriptions() throws Exception { DataServiceBean bean = spy(new DataServiceBean()); SubscriptionSearchServiceBean ssb = spy( new SubscriptionSearchServiceBean()); doReturn(bean).when(ssb).getDm(); FullTextEntityManager ftem = mock(FullTextEntityManager.class, Mockito.RETURNS_DEEP_STUBS); doReturn(ftem).when(ssb).getFtem(); Subscription sub = new Subscription(); sub.setKey(1L); FullTextQuery fullTextQuery = mock(FullTextQuery.class); when(ftem.createFullTextQuery(any(BooleanQuery.class), any(Class.class))).thenReturn(fullTextQuery); doReturn(Arrays.asList(sub)).when(fullTextQuery).getResultList(); Collection<Long> result = ssb.searchSubscriptions("searchphrase"); assertTrue(result.contains(new Long(1L))); }
/** * Searches the lucene store for a specific query * * @param <T> What type of information are we searching * @param clazz The class of the information we are searching * @param queryText The query text * @return list of entities * @throws ParseException the parse exception */ public final <T extends BaseEntity> List<Object[]> search(final Class<T> clazz, final String queryText) throws ParseException { final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); final SearchFactory searchFactory = fullTextEntityManager.getSearchFactory(); final QueryParser parser = new MultiFieldQueryParser(getClassLuceneFields(clazz), searchFactory.getAnalyzer(clazz)); final List<Query> parsedQueries = Arrays.stream(queryText.split("AND")) .map(e -> parseQuery(e, parser)) .filter(Objects::nonNull) .collect(Collectors.toList()); final BooleanQuery.Builder bq = new BooleanQuery.Builder(); parsedQueries.forEach(e -> bq.add(e, BooleanClause.Occur.MUST)); final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(bq.build(), clazz); jpaQuery.setProjection(ProjectionConstants.SCORE, ProjectionConstants.EXPLANATION, ProjectionConstants.THIS); return (List<Object[]>) jpaQuery.getResultList(); }
@Override public FullTextQuery getFTSWildcardQuery(String filterText, final Class<?> entityType, Integer maxResults, String field) { // entityManager para busquedas de tipo FTS final FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager); // se crea el query usando Hibernate Search query DSL final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(entityType).get(); // se crea el query sobre los campos indicados final Query query = queryBuilder.phrase().onField(field).sentence(filterText.trim()).createQuery(); // se enmascara el query de Lucene en uno de Hibernate final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, entityType); // se define la cantidad maxima de resultados si es mayor a cero if (maxResults > 0) { jpaQuery.setMaxResults(maxResults); } // se retorna el query listo para ejecución o para inyeccion de criterias return jpaQuery; }
@Transactional @Override public Collection<User> search(String query) { FullTextEntityManager manager = this.getFullTextEntityManager(); QueryBuilder builder = manager.getSearchFactory().buildQueryBuilder().forEntity(User.class).get(); Query luceneQuery = builder.keyword().fuzzy() .onFields("username").matching(query) .createQuery(); FullTextQuery q = manager.createFullTextQuery(luceneQuery, User.class); q.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE); @SuppressWarnings("unchecked") List<Object[]> result = q.getResultList(); List<User> fine = new LinkedList<>(); result.stream().forEach((i) -> { fine.add((User) i[0]); }); return fine; }
@Override public List<Long> getIdResults(String terminology, String version, String branch, String query, String literalField, Class<?> clazz, PfsParameter pfs, int[] totalCt, EntityManager manager) throws Exception { final FullTextQuery fullTextQuery = helper(terminology, version, branch, query, literalField, clazz, pfs, manager); totalCt[0] = fullTextQuery.getResultSize(); // Perform the final query and save score values fullTextQuery.setProjection(ProjectionConstants.ID); final List<Long> ids = new ArrayList<>(); @SuppressWarnings("unchecked") final List<Object[]> results = fullTextQuery.getResultList(); for (final Object[] result : results) { Long l = (Long) result[0]; ids.add(l); } return ids; }
@Override public List<Long> getIdResults(String terminology, String version, String branch, String query, String literalField, Class<?> clazz, PfsParameter pfs, int[] totalCt, EntityManager manager) throws Exception { final FullTextQuery fullTextQuery = helper(terminology, version, branch, query, literalField, clazz, pfs, totalCt, manager); // Retrieve the scores for the returned objects fullTextQuery.setProjection(ProjectionConstants.ID); final List<Long> ids = new ArrayList<>(); @SuppressWarnings("unchecked") final List<Object[]> results = fullTextQuery.getResultList(); for (final Object[] result : results) { final Long id = (Long) result[0]; ids.add(id); } return ids; }
@SuppressWarnings("unchecked") @Override public List<QueuedTaskHolder> search(TaskSearchQueryParameters searchParameters, Long limit, Long offset) throws ServiceException { try { FullTextQuery fullTextQuery = getSearchTaskQuery(searchParameters); if (offset != null) { fullTextQuery.setFirstResult(offset.intValue()); } if (limit != null) { fullTextQuery.setMaxResults(limit.intValue()); } Sort sort = SortUtils.getLuceneSortWithDefaults(searchParameters.getSort(), QueuedTaskHolderSort.ID); SortFieldUtil.setSort(fullTextQuery, getEntityManager(), QueuedTaskHolder.class, sort); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SECOND_LEVEL_CACHE, DatabaseRetrievalMethod.QUERY); return (List<QueuedTaskHolder>) fullTextQuery.getResultList(); } catch (RuntimeException e) { throw new ServiceException("Error while searching tasks.", e); } }
private FullTextQuery getFullTextQuery() { if (fullTextQuery == null) { addFilterBeforeCreateQuery(); if (junction.isEmpty()) { switch (getDefaultResult()) { case EVERYTHING: junction.must(getDefaultQueryBuilder().all().createQuery()); break; case NOTHING: junction.must(LuceneUtils.NO_RESULT_QUERY); break; } } fullTextQuery = fullTextEntityManager.createFullTextQuery(junction.createQuery(), classes); } return fullTextQuery; }
private FullTextQuery getFullTextQueryList(Long offset, Long limit) { FullTextQuery fullTextQuery = getFullTextQuery(); if (offset != null) { fullTextQuery.setFirstResult(Ints.saturatedCast(offset)); } if (limit != null) { fullTextQuery.setMaxResults(Ints.saturatedCast(limit)); } Sort sort = SortUtils.getLuceneSortWithDefaults(sortMap, defaultSorts); if (sort != null && sort.getSort().length > 0) { SortFieldUtil.setSort(fullTextQuery, fullTextEntityManager, mainClass, sort); } return fullTextQuery; }
@Nullable @SuppressWarnings({ CompilerWarnings.UNCHECKED }) private Long[] processKeywords(EntityManager entityManager, @Nullable Integer maxResults) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(this.entityImplClass).get(); BooleanJunction<?> conjunction = queryBuilder.bool(); PropertyMetadata keywordPropMetadata; for (String keywordPropName : this.keywords.keySet()) { conjunction = conjunction.must(queryBuilder.keyword() .onFields((keywordPropMetadata = this.entityPropMetadatas.get(keywordPropName)).getLowercaseFieldName(), keywordPropMetadata.getEdgeNgramFieldName(), keywordPropMetadata.getNgramFieldName(), keywordPropMetadata.getPhoneticFieldName()) .matching(StringUtils.join(this.keywords.get(keywordPropName), StringUtils.SPACE)).createQuery()); } FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(new InterceptingQuery(conjunction.createQuery()), this.entityImplClass); fullTextQuery.setProjection(ProjectionConstants.ID); if (maxResults != null) { fullTextQuery.setMaxResults(maxResults); } return ((List<Object[]>) fullTextQuery.getResultList()).stream().map(fullTextResult -> ((Long) fullTextResult[0])).toArray(Long[]::new); }
@SuppressWarnings("unchecked") public <T> List<T> executeSearchQuery( RestSearchKey key, Class<T> typeClass, FullTextEntityManager fullTextEntityManager, QueryBuilder queryBuilder, Map<String, Query> crtsMap ) { FullTextQuery fullTextQuery = createFullTextQuery(typeClass, fullTextEntityManager, queryBuilder, crtsMap); if(key.hasOrders()){ hibernateSearchBuilder.defineOrder(key, fullTextQuery); } hibernateSearchBuilder.definePaging(key, fullTextQuery); return fullTextQuery.getResultList(); }
private <T> FullTextQuery createFullTextQuery(Class<T> typeClass, FullTextEntityManager fullTextEntityManager, QueryBuilder queryBuilder, Map<String, Query> crtsMap) { BooleanJunction<?> booleanJunction = hibernateSearchBuilder.createFinalBooleanJunction( queryBuilder, crtsMap); FullTextQuery fullTextQuery = null; if(booleanJunction == null){ fullTextQuery = fullTextEntityManager.createFullTextQuery( queryBuilder.all().createQuery(), typeClass); } else { fullTextQuery = fullTextEntityManager.createFullTextQuery( booleanJunction.createQuery(), typeClass); } return fullTextQuery; }
@SuppressWarnings("unchecked") public <T> List<T> find(Class<T> clazz, SearchParameters sp, List<SingularAttribute<?, ?>> availableProperties) { // log.info("Searching {} with terms : {} with available Properties: {}", new Object[]{clazz.getSimpleName(), sp.getTerms(), availableProperties}); FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(entityManager); Query query = sp.getLuceneQueryBuilder().build(fullTextEntityManager, sp, availableProperties); if (query == null) { return null; } FullTextQuery ftq = fullTextEntityManager.createFullTextQuery( // query, clazz); if (sp.getMaxResults() > 0) { ftq.setMaxResults(sp.getMaxResults()); } return ftq.getResultList(); }
@SuppressWarnings("unchecked") public <T> List<Serializable> findId(Class<T> clazz, SearchParameters sp, List<SingularAttribute<?, ?>> availableProperties) { //log.info("Searching {} with terms : {} with available Properties: {}", new Object[]{clazz.getSimpleName(), sp.getTerms(), availableProperties}); FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(entityManager); Query query = sp.getLuceneQueryBuilder().build(fullTextEntityManager, sp, availableProperties); if (query == null) { return null; } FullTextQuery ftq = fullTextEntityManager.createFullTextQuery( // query, clazz); ftq.setProjection("id"); if (sp.getMaxResults() > 0) { ftq.setMaxResults(sp.getMaxResults()); } List<Serializable> ids = newArrayList(); List<Object[]> resultList = ftq.getResultList(); for (Object[] result : resultList) { ids.add((Serializable) result[0]); } return ids; }
public List<String> extractCategories() { FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager( em ); QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( Categories.class ).get(); org.apache.lucene.search.Query query = queryBuilder.all().createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( query , Categories.class ); fullTextQuery.setProjection( FullTextQuery.ID , "category" ); Sort sort = new Sort( new SortField( "category" , SortField.STRING ) ); fullTextQuery.setSort( sort ); //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); List<String> results = fullTextQuery.getResultList(); return results; }
public Map<Integer , List<Products>> extractProducts( String id , int page ) { FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager( em ); QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( Products.class ).get(); org.apache.lucene.search.Query query = queryBuilder.keyword().onField( "category.id" ).matching( id ).createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( query , Products.class ); Sort sort = new Sort( new SortField( "price" , SortField.DOUBLE ) ); fullTextQuery.setSort( sort ); //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); fullTextQuery.setFirstResult( page * 3 ); fullTextQuery.setMaxResults( 3 ); List<Products> results = fullTextQuery.getResultList(); Map<Integer , List<Products>> results_and_total = new HashMap<Integer , List<Products>>(); results_and_total.put( fullTextQuery.getResultSize() , results ); return results_and_total; }
public List<Products> searchProducts( String search ) { FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager( em ); QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( Products.class ).get(); org.apache.lucene.search.Query query = queryBuilder.keyword().onField( "product" ).matching( search ).createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( query , Products.class ); //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); fullTextQuery.setMaxResults( 3 ); List results = fullTextQuery.getResultList(); return results; }
public List<Products> extractPromotionalProducts() { FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager( em ); org.apache.lucene.search.Query query = NumericRangeQuery.newDoubleRange( "old_price" , 0.0d , 1000d , false , true ); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( query , Products.class ); Sort sort = new Sort( new SortField( "price" , SortField.DOUBLE ) ); fullTextQuery.setSort( sort ); //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); List results = fullTextQuery.getResultList(); return results; }
public Customers extractCustomer( String email , String password ) { FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager( em ); QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( Customers.class ).get(); org.apache.lucene.search.Query query = queryBuilder.bool().must( queryBuilder.keyword() .onField( "email" ).matching( email ).createQuery() ).must( queryBuilder.keyword() .onField( "password" ).matching( password ).createQuery() ).createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( query , Customers.class ); //fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); List results = fullTextQuery.getResultList(); if ( results.isEmpty() ) { return null; } return ( Customers ) results.get( 0 ); }
@SuppressWarnings("unchecked") private <T> FullTextQuery getQuery(Class<? extends T> type, TermSelector<T, ?> termSelector) { FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(entityManager); Query query; if (termSelector instanceof StringTermSelector) { query = luceneQueryBuilder.build(fullTextEntityManager, (StringTermSelector<T>) termSelector, type); } else if (termSelector instanceof ObjectTermSelector) { query = luceneQueryBuilder.build(fullTextEntityManager, (ObjectTermSelector<T>) termSelector, type); } else { throw new RuntimeException("Unknown TermSelector: " + termSelector.getClass().getName()); } if (query == null) { return null; } logger.debug("Query: {}.", query.toString()); FullTextQuery ftq = fullTextEntityManager.createFullTextQuery( // query, type); return ftq; }
public Page<Affiliate> findFullTextAndMember(String q, Member homeMember, StandingState standingState, boolean standingStateNot, Pageable pageable) { Query query = buildQuery(q, homeMember, standingState, standingStateNot); LOG.debug("Query: {}", query); FullTextQuery ftQuery = getFullTextEntityManager().createFullTextQuery(query, Affiliate.class); ftQuery.setFirstResult(pageableUtil.getStartPosition(pageable)); ftQuery.setMaxResults(pageable.getPageSize()); @SuppressWarnings("unchecked") List<Affiliate> resultList = ftQuery.getResultList(); dumpDebugInfoWithScores(ftQuery); LOG.debug("Found {} results for query: {}", ftQuery.getResultSize(), q); return new PageImpl<>(resultList, pageable, ftQuery.getResultSize()); }
@SuppressWarnings("unchecked") void dumpDebugInfoWithScores(FullTextQuery ftQuery) { if (LOG.isTraceEnabled()) { ftQuery.setProjection( FullTextQuery.DOCUMENT_ID, FullTextQuery.SCORE, FullTextQuery.EXPLANATION, "affiliateDetails.organizationName" ); for (Object[] projection : (List<Object[]>) ftQuery.getResultList()) { LOG.trace("Projection: {}",Arrays.asList(projection)); } } }
@Test public void testGetSingleResult() { FullTextEntityManager fem = this.searchFactory.getFullTextEntityManager( this.em ); { this.em.find( Place.class, this.valinorId ); FullTextQuery ftQuery = fem.createFullTextQuery( fem.getSearchFactory() .buildQueryBuilder() .forEntity( Place.class ) .get() .keyword() .onField( "name" ) .matching( "Valinor" ) .createQuery(), Place.class ); assertEquals( (Integer) this.valinorId, ((Place) ftQuery.getSingleResult()).getId() ); } }
@Test public void testGetSingleResultProjection() { FullTextEntityManager fem = this.searchFactory.getFullTextEntityManager( this.em ); { this.em.find( Place.class, this.valinorId ); FullTextQuery ftQuery = fem.createFullTextQuery( fem.getSearchFactory() .buildQueryBuilder() .forEntity( Place.class ) .get() .keyword() .onField( "name" ) .matching( "Valinor" ) .createQuery(), Place.class ).setProjection( "name" ); assertEquals( "Valinor", ((Object[]) ftQuery.getSingleResult())[0] ); } }
@Test public void testQueryPojectionJPA() { FullTextEntityManager fem = this.searchFactory.getFullTextEntityManager( this.em ); { this.em.find( Place.class, this.valinorId ); FullTextQuery ftQuery = fem.createFullTextQuery( fem.getSearchFactory() .buildQueryBuilder() .forEntity( Place.class ) .get() .keyword() .onField( "name" ) .matching( "Valinor" ) .createQuery(), Place.class ).setProjection( "name" ); assertEquals( "Valinor", ((Object[]) ftQuery.getResultList().get( 0 ))[0] ); } }
private void testFoundSorcererAndPlace(FullTextQuery ftQuery) { boolean[] found = new boolean[2]; ftQuery.getResultList().stream().forEach( (ent) -> { if ( ent instanceof Place ) { found[0] = true; } else if ( ent instanceof Sorcerer ) { found[1] = true; } else { throw new AssertionError(); } } ); for ( boolean fd : found ) { assertTrue( "did not find Sorcerer AND Place in Query", fd ); } }
@Test public void testManualIndexing() throws Exception { this.shouldFindAllGamesInIndex(); FullTextEntityManager fem = this.searchFactory.getFullTextEntityManager( this.em ); fem.beginSearchTransaction(); Game newGame = new Game( "Legend of Zelda" ); newGame.setId( -10L ); fem.index( newGame ); fem.commitSearchTransaction(); Sleep.sleep( MAX_SLEEP_TIME, () -> { FullTextQuery fullTextQuery = fem.createFullTextQuery( fem.getSearchFactory().buildQueryBuilder().forEntity( Game.class ).get().keyword().onField( "title" ).matching( "Legend of Zelda" ).createQuery(), Game.class ); // we can find it in the index even though it is not persisted in the database boolean val1 = 1 == fullTextQuery.getResultSize(); // but no result should be returned here: boolean val2 = 0 == fullTextQuery.getResultList().size(); return val1 && val2; }, 100, "" ); }
@Override @SuppressWarnings("unchecked") public SearchResult<Page> search(String term, int offset, int limit) { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( getEm() ); QueryBuilder queryBuilder = fullTextEm.getSearchFactory().buildQueryBuilder() .forEntity( Page.class ).get(); Query luceneQuery = queryBuilder.keyword() .onField( "title" ).boostedTo( 2.0f ) .andField( "content" ) .matching( term ) .createQuery(); Sort scoreSort = queryBuilder.sort().byScore().createSort(); FullTextQuery query = fullTextEm.createFullTextQuery( luceneQuery, Page.class ) .setFirstResult( offset ) .setMaxResults( limit ) .setSort( scoreSort ); return new SearchResult<>( query.getResultSize(), query.getResultList() ); }
@Test public void queryOnSingleField() { EntityManager em = emf.createEntityManager(); inTransaction( em, tx -> { FullTextEntityManager ftem = Search.getFullTextEntityManager( em ); QueryBuilder qb = ftem.getSearchFactory() .buildQueryBuilder() .forEntity( VideoGame.class ) .get(); FullTextQuery query = ftem.createFullTextQuery( qb.keyword().onField( "title" ).matching( "samurai" ).createQuery(), VideoGame.class ); List<VideoGame> videoGames = query.getResultList(); assertThat( videoGames ).onProperty( "title" ).containsExactly( "Revenge of the Samurai" ); } ); em.close(); }
@Test public void queryOnMultipleFields() { EntityManager em = emf.createEntityManager(); inTransaction( em, tx -> { FullTextEntityManager ftem = Search.getFullTextEntityManager( em ); QueryBuilder qb = ftem.getSearchFactory() .buildQueryBuilder() .forEntity( VideoGame.class ) .get(); FullTextQuery query = ftem.createFullTextQuery( qb.keyword().onFields( "title", "description").matching( "samurai" ).createQuery(), VideoGame.class ); List<VideoGame> videoGames = query.getResultList(); assertThat( videoGames ).onProperty( "title" ).containsExactly( "Revenge of the Samurai", "Tanaka's return" ); } ); em.close(); }