/** * 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(); }
public void index(Class<?> type) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); if (type == Article.class) { for (int i = 0; i < articleDao.count(); i += 20) { List<Article> articles = articleDao.findList(i, 20, null, null); for (Article article : articles) { fullTextEntityManager.index(article); } fullTextEntityManager.flushToIndexes(); fullTextEntityManager.clear(); articleDao.clear(); } } else if (type == Product.class) { for (int i = 0; i < productDao.count(); i += 20) { List<Product> products = productDao.findList(i, 20, null, null); for (Product product : products) { fullTextEntityManager.index(product); } fullTextEntityManager.flushToIndexes(); fullTextEntityManager.clear(); productDao.clear(); } } }
/** * Call at the startup of the server. * * - Enables Hibernate's FullTextEntityManager for Full-text searching. * - Initializes roles in database */ public void initialize() { logger.info("Linking entityManager."); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); logger.info(String.format("entityManager: %s linked.", entityManager)); try { logger.info("Beginning to index data."); fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("Index complete."); // Initialize user roles in database Role publicRole = createRole(1, "ROLE_PUBLIC"); roleRepository.save(publicRole); Role adminRole = createRole(2, "ROLE_ADMIN"); roleRepository.save(adminRole); }
@Override public List<Product> searchByText(String text) { Product productEntity = newInstance(); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em); QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder() .forEntity(productEntity.getClass()).get(); if (!Strings.isNullOrEmpty(text)) { Query query = qb.keyword().onField("name").andField("details").andField("technicalDetails").matching(text) .createQuery(); javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(query, productEntity.getClass()); List<Product> result = persistenceQuery.getResultList(); if (CollectionUtils.isNotEmpty(result)) { for (Product product : result) { loadImagesFromQuantities(product.getQuantities()); } } return result; } return Collections.emptyList(); }
@Override public Analyzer getAnalyzer(Class<?> entityType) { if (jpaPropertiesProvider.isHibernateSearchElasticSearchEnabled()) { ExtendedSearchIntegrator searchIntegrator = Search.getFullTextEntityManager(getEntityManager()) .getSearchFactory().unwrap(ExtendedSearchIntegrator.class); ScopedElasticsearchAnalyzer scopedAnalyzer = (ScopedElasticsearchAnalyzer) searchIntegrator.getAnalyzerReference(entityType).getAnalyzer(); try { // these properties are package protected ! ElasticsearchAnalyzer globalAnalyzer = (ElasticsearchAnalyzer) FieldUtils.getField(ScopedElasticsearchAnalyzer.class, "globalAnalyzer", true).get(scopedAnalyzer); @SuppressWarnings("unchecked") Map<String, ElasticsearchAnalyzer> analyzers = (Map<String, ElasticsearchAnalyzer>) FieldUtils.getField(ScopedElasticsearchAnalyzer.class, "scopedAnalyzers", true).get(scopedAnalyzer); Map<String, Analyzer> luceneAnalyzers = Maps.newHashMap(); for (Entry<String, ElasticsearchAnalyzer> analyzer : analyzers.entrySet()) { luceneAnalyzers.put(analyzer.getKey(), getAnalyzer(analyzer.getValue().getName(null))); } return new ScopedLuceneAnalyzer(getAnalyzer(globalAnalyzer.getName(null)) /* parameter is not used */, luceneAnalyzers); } catch (IllegalAccessException e) { throw new RuntimeException("illegal access on scoped analyzer", e); } } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class).getAnalyzer(entityType); } }
@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); }
@Test @Transactional public void searchAdherentSortOnName() throws Exception { // Given adherent.setNom("FERRÉ"); adherentRepository.saveAndFlush(adherent); final Adherent secondAdherent = new Adherent(); secondAdherent.setPrenom(DEFAULT_PRENOM); secondAdherent.setNom("BERRUBÉ"); adherentRepository.saveAndFlush(secondAdherent); final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.flushToIndexes(); // When restAdherentMockMvc.perform(get("/api/adherents/search?criteria=eRR&sort=nom").accept(TestUtil.APPLICATION_JSON_UTF8)).andExpect(status().isOk()) .andExpect(jsonPath("$.[*].nom").value(contains("BERRUBÉ", "FERRÉ"))); }
@Test public void test() { final String testValue = "test"; assertThat(entityWithFullTextRepository.findCount(findByValue(testValue))).isEqualTo(0); EntityWithFullText entityWithFullText = new EntityWithFullText(); entityWithFullText.setValue(testValue); entityWithFullText = entityWithFullTextRepository.save(entityWithFullText); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.index(entityWithFullText); fullTextEntityManager.flushToIndexes(); fullTextEntityManager.flush(); assertThat(entityWithFullTextRepository.find(findByValue(testValue))).containsExactly(entityWithFullText); }
@Test public void test() { final String testValue = "test"; assertThat(entityWithMultipleFullTextRepository.findCount(findByValue(testValue))).isEqualTo(0); EntityWithMultipleFullText entityWithMultipleFullText = new EntityWithMultipleFullText(); entityWithMultipleFullText.setValue1(testValue); entityWithMultipleFullText.setValue2("abcdef"); entityWithMultipleFullText = entityWithMultipleFullTextRepository.save(entityWithMultipleFullText); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.index(entityWithMultipleFullText); fullTextEntityManager.flushToIndexes(); fullTextEntityManager.flush(); assertThat(entityWithMultipleFullTextRepository.find(findByValue(testValue))).containsExactly(entityWithMultipleFullText); }
@Test public void deep_test() { final String testValue = "test"; assertThat(entityWithMultipleFullTextRepository.findCount(findByDeepValue(testValue))).isEqualTo(0); EntityWithMultipleFullText entityWithMultipleFullText = new EntityWithMultipleFullText(); entityWithMultipleFullText.setValue1("abcdef"); EntityWithFullText entityWithFullText = new EntityWithFullText(); entityWithFullText.setValue(testValue); entityWithMultipleFullText.setEntityWithFullText(entityWithFullText); entityWithMultipleFullText = entityWithMultipleFullTextRepository.save(entityWithMultipleFullText); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.index(entityWithMultipleFullText); fullTextEntityManager.flushToIndexes(); fullTextEntityManager.flush(); assertThat(entityWithMultipleFullTextRepository.find(findByDeepValue(testValue))).containsExactly(entityWithMultipleFullText); }
@Test public void test() { final Integer testValue = 1995; assertThat(entityWithFullTextIntegerRepository.findCount(findByValue(testValue))).isEqualTo(0); EntityWithFullTextInteger entityWithFullTextInteger = new EntityWithFullTextInteger(); entityWithFullTextInteger.setValue(testValue); entityWithFullTextInteger = entityWithFullTextIntegerRepository.save(entityWithFullTextInteger); EntityWithFullTextInteger another = new EntityWithFullTextInteger(); another.setValue(1994); entityWithFullTextIntegerRepository.save(another); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.index(entityWithFullTextInteger); fullTextEntityManager.flushToIndexes(); fullTextEntityManager.flush(); assertThat(entityWithFullTextIntegerRepository.find(findByValue(testValue))).containsExactly(entityWithFullTextInteger); }
@PostConstruct public void recreateIndex() throws Exception { log.debug("SearchConfiguration - creating full-text index"); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); Future<?> indexResult = fullTextEntityManager.createIndexer().start(); Futures.addCallback(JdkFutureAdapters.listenInPoolThread(indexResult), new FutureCallback<Object>() { @Override public void onSuccess(Object result) { log.debug("SearchConfiguration - finished creating full-text index"); } @Override public void onFailure(Throwable t) { log.error("SearchConfiguration - Failed creating full-text index", t); } }); }
@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(); }
@Test public void wildcardQuery() { 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().wildcard().onFields( "title", "description").matching( "sam*" ).createQuery(), VideoGame.class ); List<VideoGame> videoGames = query.getResultList(); assertThat( videoGames ).onProperty( "title" ).containsExactly( "Revenge of the Samurai", "Tanaka's return" ); } ); em.close(); }
@Test public void rangeQuery() { 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.range() .onField( "rating" ) .from( 2 ) .to( 9 ) .createQuery(), VideoGame.class ); List<VideoGame> videoGames = query.getResultList(); assertThat( videoGames ).onProperty( "title" ).containsOnly( "Revenge of the Samurai", "Ninja Castle" ); } ); em.close(); }
@Test public void queryString() { 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( ElasticsearchQueries.fromQueryString( "title:sam* OR description:sam*" ), VideoGame.class ); List<VideoGame> videoGames = query.getResultList(); assertThat( videoGames ).onProperty( "title" ).containsExactly( "Revenge of the Samurai", "Tanaka's return" ); } ); em.close(); }
/** * Return all associated money transfers with given expense report. * * @param expenseReport to look for in money transfers * @return collection of moneyTransfers associated with expenseReport or null. */ @Override public Collection<MoneyTransfer> get(ExpenseReport expenseReport) { if (expenseReport == null) { throw new IllegalArgumentException("expenseReport is null"); } FullTextEntityManager ftem = Search.getFullTextEntityManager(em); QueryBuilder queryBuilder = ftem.getSearchFactory().buildQueryBuilder().forEntity(MoneyTransfer.class).get(); Query query = queryBuilder.keyword().onField("report.id").matching(expenseReport.getId()).createQuery(); FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, MoneyTransfer.class); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); return fullTextQuery.getResultList(); }
@Override public Collection<ExpenseReport> getAllWithNoVerifierAssigned() { FullTextEntityManager ftem = Search.getFullTextEntityManager(em); QueryBuilder queryBuilder = ftem.getSearchFactory().buildQueryBuilder().forEntity(ExpenseReport.class).get(); Query allAssigned = queryBuilder.range().onField("verifier.id").above(0).createQuery(); // Query query = queryBuilder.bool().must(allAssigned).not().createQuery(); Query statusSubmitted = queryBuilder.keyword().onField("status").matching(ReportStatus.SUBMITTED).createQuery(); Query query = queryBuilder.bool() .must(allAssigned).not() .must(statusSubmitted) .createQuery(); FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, ExpenseReport.class); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); return fullTextQuery.getResultList(); }
@Override public Collection<ExpenseReport> getAllForSubmitterWithStatus(Person submitter, ReportStatus status) { FullTextEntityManager ftem = Search.getFullTextEntityManager(em); QueryBuilder queryBuilder = ftem.getSearchFactory().buildQueryBuilder().forEntity(ExpenseReport.class).get(); Query forPerson = queryBuilder.keyword().onField("submitter.id").matching(submitter.getId()).createQuery(); Query forStatus = queryBuilder.keyword().onField("status").matching(status).createQuery(); Query query = queryBuilder.bool() .must(forPerson) .must(forStatus) .createQuery(); FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, ExpenseReport.class); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); return fullTextQuery.getResultList(); }
@Override public Collection<ExpenseReport> getAllForVerifierWithStatus(Person verifier, ReportStatus status) { FullTextEntityManager ftem = Search.getFullTextEntityManager(em); QueryBuilder queryBuilder = ftem.getSearchFactory().buildQueryBuilder().forEntity(ExpenseReport.class).get(); Query forPerson = queryBuilder.keyword().onField("verifier.id").matching(verifier.getId()).createQuery(); Query forStatus = queryBuilder.keyword().onField("status").matching(status).createQuery(); Query query = queryBuilder.bool() .must(forPerson) .must(forStatus) .createQuery(); FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, ExpenseReport.class); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID); return fullTextQuery.getResultList(); }
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); }
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); }
@Override public List<Artifact> searchAutocomplete(String searchPattern, Integer limit, Integer offset) throws ServiceException { String[] searchFields = new String[] { Binding.artifact().artifactId().getPath(), Binding.artifact().group().groupId().getPath() }; QueryBuilder queryBuilder = Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().buildQueryBuilder() .forEntity(Artifact.class).get(); Query luceneQuery = queryBuilder.keyword().onField(Binding.artifact().deprecationStatus().getPath()).matching(ArtifactDeprecationStatus.NORMAL).createQuery(); List<SortField> sortFields = ImmutableList.<SortField>builder() .add(new SortField(Binding.artifact().group().getPath() + '.' + ArtifactGroup.GROUP_ID_SORT_FIELD_NAME, SortField.Type.STRING)) .add(new SortField(Artifact.ARTIFACT_ID_SORT_FIELD_NAME, SortField.Type.STRING)) .build(); Sort sort = new Sort(sortFields.toArray(new SortField[sortFields.size()])); return hibernateSearchService.searchAutocomplete(getObjectClass(), searchFields, searchPattern, luceneQuery, limit, offset, sort); }
@Override public List<Artifact> searchAutocompleteWithoutProject(String searchPattern, Integer limit, Integer offset) throws ServiceException { String[] searchFields = new String[] { Binding.artifact().artifactId().getPath(), Binding.artifact().group().groupId().getPath() }; QueryBuilder queryBuilder = Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().buildQueryBuilder() .forEntity(Artifact.class).get(); Query notDeprecatedQuery = queryBuilder.keyword().onField(Binding.artifact().deprecationStatus().getPath()).matching(ArtifactDeprecationStatus.NORMAL).createQuery(); Query withoutProjectQuery = queryBuilder.keyword().onField(Binding.artifact().project().getPath()).matching(null).createQuery(); BooleanJunction<?> booleanJunction = queryBuilder.bool() .must(notDeprecatedQuery) .must(withoutProjectQuery); List<SortField> sortFields = ImmutableList.<SortField>builder() .add(new SortField(Binding.artifact().group().getPath() + '.' + ArtifactGroup.GROUP_ID_SORT_FIELD_NAME, SortField.Type.STRING)) .add(new SortField(Artifact.ARTIFACT_ID_SORT_FIELD_NAME, SortField.Type.STRING)) .build(); Sort sort = new Sort(sortFields.toArray(new SortField[sortFields.size()])); return hibernateSearchService.searchAutocomplete(getObjectClass(), searchFields, searchPattern, booleanJunction.createQuery(), limit, offset, sort); }
@Override public void onApplicationEvent(final ApplicationReadyEvent event) { try { EntityManager entityManager = entityManagerFactory.createEntityManager(); try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer(CLASSES).startAndWait(); } finally { entityManager.close(); } } catch (InterruptedException e) { System.out.println("An error occurred trying to build the search index: " + e.toString()); } }
/** * Build lucene search index * * @param event application ready event */ @Override public void onApplicationEvent(final ApplicationReadyEvent event) { try { final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { logger.info("[BuildSearchIndex] [onApplicationEvent] Build Search Index Failure", e); } }
@Override public void buildIndexes() { try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { //System.out.println("Error creando la lista de indices: " + e.toString()); AitLogger.debug(logger,"Error creando la lista de indices: " + e.toString()); } }
public List <NewsItem> searchNewsItems (String query) { List <NewsItem> result = new ArrayList <NewsItem> (); EntityManager manager = emf.createEntityManager(); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(manager); try { // manager.getTransaction().begin(); QueryBuilder qb = fullTextEntityManager.getSearchFactory() .buildQueryBuilder().forEntity(NewsItem.class).get(); org.apache.lucene.search.Query luceneQuery = qb .keyword() .onFields("title", "subtitle", "article") .matching(query) .createQuery(); // wrap Lucene query in a javax.persistence.Query javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, NewsItem.class); // execute search result = jpaQuery.getResultList(); for (NewsItem item : result) { if (item.getImageUrl().isEmpty()) { item.setImageUrl(feeds.get(item.getTopic())[2]); } } // manager.getTransaction().commit(); } catch (Exception e) { e.printStackTrace (); } finally { manager.close(); } return result; }
public void purge(Class<?> type) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); if (type == Article.class) { fullTextEntityManager.purgeAll(Article.class); } else if (type == Product.class) { fullTextEntityManager.purgeAll(Product.class); } }
@SuppressWarnings("unchecked") @Transactional(readOnly = true) public Page<Article> search(String keyword, Pageable pageable) { if (StringUtils.isEmpty(keyword)) { return new Page<Article>(); } if (pageable == null) { pageable = new Pageable(); } try { String text = QueryParser.escape(keyword); QueryParser titleParser = new QueryParser(Version.LUCENE_35, "title", new IKAnalyzer()); titleParser.setDefaultOperator(QueryParser.AND_OPERATOR); Query titleQuery = titleParser.parse(text); FuzzyQuery titleFuzzyQuery = new FuzzyQuery(new Term("title", text), FUZZY_QUERY_MINIMUM_SIMILARITY); Query contentQuery = new TermQuery(new Term("content", text)); Query isPublicationQuery = new TermQuery(new Term("isPublication", "true")); BooleanQuery textQuery = new BooleanQuery(); BooleanQuery query = new BooleanQuery(); textQuery.add(titleQuery, Occur.SHOULD); textQuery.add(titleFuzzyQuery, Occur.SHOULD); textQuery.add(contentQuery, Occur.SHOULD); query.add(isPublicationQuery, Occur.MUST); query.add(textQuery, Occur.MUST); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Article.class); fullTextQuery.setSort(new Sort(new SortField[] { new SortField("isTop", SortField.STRING, true), new SortField(null, SortField.SCORE), new SortField("createDate", SortField.LONG, true) })); fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); fullTextQuery.setMaxResults(pageable.getPageSize()); return new Page<Article>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable); } catch (ParseException e) { e.printStackTrace(); } return new Page<Article>(); }
public void checkIndexOnStartup() { //log.info("Observed event {1} from Thread {0}", Thread.currentThread().getName(), App.INIT_SUCCESS); // See if we need to rebuild the index during startup ... FullTextEntityManager ftEm = Search.getFullTextEntityManager(entityManager); SearchFactory searchFactory = ftEm.getSearchFactory(); ReaderProvider readerProvider = searchFactory.getReaderProvider(); IndexReader reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]); int maxDoc = 0; try { maxDoc = reader.maxDoc(); } finally { readerProvider.closeReader(reader); } if (maxDoc == 0) { log.warn("No objects indexed ... rebuilding Lucene search index from database ..."); long _exit = 0L; long _entr = System.currentTimeMillis(); try { int docs = doRebuildIndex(); _exit = System.currentTimeMillis(); log.info("Took " + (_exit - _entr) + " (ms) to re-build the index containing " + docs + " documents."); } catch (Exception exc) { if (exc instanceof RuntimeException) { throw (RuntimeException) exc; } else { throw new RuntimeException(exc); } } // build the spell checker index off of the HS index. buildSpellCheckerIndex(searchFactory); } }
@Override @Transactional(readOnly = true) public List<PostEntity> searchByKeyword(final String keyword) { final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(PostEntity.class).get(); return fullTextEntityManager .createFullTextQuery(queryBuilder.simpleQueryString() .onFields("content") .withAndAsDefaultOperator() .matching(keyword) .createQuery(), PostEntity.class).getResultList(); }
@Override @Transactional(readOnly = true) public List<TweetEntity> searchByKeyword(final String keywords, final LocalDate from, final LocalDate to) { // 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(TweetEntity.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() .onFields("content") .matching(keywords) .createQuery() ); // And then 2 range queries if from and to are not null Optional.ofNullable(from) .map(f -> f.atStartOfDay(UTC)) // Must be a zoned date time to fit the field .map(f -> queryBuilder.range().onField("created_at").above(f).createQuery()) .ifPresent(q -> outer.must(q)); Optional.ofNullable(to) .map(f -> f.plusDays(1).atStartOfDay(UTC)) // Same here, but a day later .map(f -> queryBuilder.range().onField("created_at").below(f).excludeLimit().createQuery()) // which i exclude .ifPresent(q -> outer.must(q)); return fullTextEntityManager.createFullTextQuery(outer.createQuery(), TweetEntity.class).getResultList(); }
@Override @Transactional(readOnly = true) public List<TweetEntity> searchByQuery(final String query) { final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); List<TweetEntity> rv; try { final QueryParser queryParser = new QueryParser("content", fullTextEntityManager.getSearchFactory().getAnalyzer(TweetEntity.class)); rv = fullTextEntityManager.createFullTextQuery(queryParser.parse(query), TweetEntity.class).getResultList(); } catch (ParseException e) { log.error("Could not parse query", e); rv = new ArrayList<>(); } return rv; }
@Override public void compute() throws Exception { logInfo("Starting " + getName()); if (fullTextEntityManager == null) { fullTextEntityManager = Search.getFullTextEntityManager(manager); } computeLuceneIndexes(indexedObjects); // fullTextEntityManager.close(); logInfo("Finished " + getName()); }
@Override public void reset() throws Exception { if (fullTextEntityManager == null) { fullTextEntityManager = Search.getFullTextEntityManager(manager); } clearLuceneIndexes(); // fullTextEntityManager.close(); }
@Override public Analyzer getAnalyzer(String analyzerName) { if (jpaPropertiesProvider.isHibernateSearchElasticSearchEnabled()) { if ("default".equals(analyzerName)) { return PassThroughAnalyzer.INSTANCE; } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class) .getAnalyzer(CoreLuceneClientAnalyzersDefinitionProvider.ANALYZER_NAME_PREFIX + analyzerName); } } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class).getAnalyzer(analyzerName); } }
@Override public void reindexClasses(Class<?>... classes) throws ServiceException { try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); reindexClasses(fullTextEntityManager, getIndexedRootEntities(fullTextEntityManager.getSearchFactory(), classes.length > 0 ? classes : new Class<?>[] { Object.class })); } catch (RuntimeException | InterruptedException e) { throw new ServiceException(e); } }