【发布时间】:2019-10-07 23:53:16
【问题描述】:
我需要搜索在 elasticsearch 上编入索引的多个文档。搜索有效,但我需要知道返回搜索的对象类型。
公共列表搜索(字符串术语){
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder authorQB = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Author.class).get();
QueryBuilder postQB = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Post.class).get();
QueryBuilder commentQB = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Comment.class).get();
Query authorLQ = authorQB
.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1)
.onFields(AUTHOR_FIELDS).matching(terms)
.createQuery();
Query postLQ = postQB
.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1)
.onFields(POST_FIELDS).matching(terms)
.createQuery();
Query commentLQ = commentQB
.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1)
.onFields(COMMENT_FIELDS).matching(terms)
.createQuery();
Query luceneQuery = authorQB.bool()
.should(authorLQ)
.should(postLQ)
.should(commentLQ)
.createQuery();
javax.persistence.Query jpaQuery = fullTextEntityManager.
createFullTextQuery(luceneQuery, Author.class, Post.class, Comment.class);
List<Object> result; // need to know object type
try {
result = jpaQuery.getResultList();
} catch (NoResultException nre) {
throw new NoResultException("The search for " + terms + " did not get any results");
}
return result;
}
这给了我所有对象的列表,但我需要确切知道它是什么类型(作者、帖子或评论)。可以的,谢谢。
【问题讨论】:
标签: elasticsearch lucene hibernate-search