本文整理汇总了Java中org.hibernate.search.jpa.FullTextEntityManager类的典型用法代码示例。如果您正苦于以下问题:Java FullTextEntityManager类的具体用法?Java FullTextEntityManager怎么用?Java FullTextEntityManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FullTextEntityManager类属于org.hibernate.search.jpa包,在下文中一共展示了FullTextEntityManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: search
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
/**
* 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();
}
示例2: getFTSQuery
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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;
}
示例3: search
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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);
}
示例4: searchSubscriptions
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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)));
}
示例5: getFTSWildcardQuery
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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;
}
示例6: index
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
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();
}
}
}
示例7: initialize
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
/**
* 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);
}
示例8: search
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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;
}
示例9: searchByText
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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();
}
示例10: reindexClasses
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
protected void reindexClasses(FullTextEntityManager fullTextEntityManager, Set<Class<?>> entityClasses)
throws InterruptedException {
int batchSize = propertyService.get(HIBERNATE_SEARCH_REINDEX_BATCH_SIZE);
int loadThreads = propertyService.get(HIBERNATE_SEARCH_REINDEX_LOAD_THREADS);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Targets for indexing job: {}", entityClasses);
}
for (Class<?> clazz : entityClasses) {
ProgressMonitor progressMonitor = new ProgressMonitor();
Thread t = new Thread(progressMonitor);
LOGGER.info(String.format("Reindexing %1$s.", clazz));
t.start();
MassIndexer indexer = fullTextEntityManager.createIndexer(clazz);
indexer.batchSizeToLoadObjects(batchSize)
.threadsToLoadObjects(loadThreads)
.cacheMode(CacheMode.NORMAL)
.progressMonitor(progressMonitor)
.startAndWait();
progressMonitor.stop();
t.interrupt();
LOGGER.info(String.format("Reindexing %1$s done.", clazz));
}
}
示例11: processKeywords
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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);
}
示例12: executeSearchQuery
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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();
}
示例13: createFullTextQuery
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
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;
}
示例14: find
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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();
}
示例15: findId
import org.hibernate.search.jpa.FullTextEntityManager; //导入依赖的package包/类
@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;
}