我是休眠搜索的新手,并且遇到此问题,我想在休眠搜索中使用名字,中间名,姓氏,用户名以及合作伙伴ID来搜索特定合作社中的成员,搜索返回的成员匹配任何搜索字词都不会考虑合作社ID。
我们非常感谢您提供的有关上述问题的帮助,我仍然是休眠搜索的新手,这确实需要我的时间。
请参见下面的代码示例:
public List<MemberProfile> searchForMember(String memberSearchTerm, int cooperativeId, int pageNumber, int pageSize) throws Exception {
HibernateDataAccess dao = new HibernateDataAccess();
List<MemberProfile> members = new ArrayList<>();
try {
dao.startOperation();
Cooperative cooperative = (Cooperative) dao.searchObject(Cooperative.class, cooperativeId);
FullTextSession fullTextSession = Search.getFullTextSession(dao.getSession());
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(MemberProfile.class).get();
org.apache.lucene.search.Query cooperativeQuery = qb
.keyword()
.onField("cooperative.cooperativeName")
.matching(cooperative.getCooperativeName())
.createQuery();
org.apache.lucene.search.Query memberQuery = qb
.keyword()
.onFields("username", "firstName", "middleName",
"lastName", "phoneNumber", "emailAddress")
.matching(memberSearchTerm)
.createQuery();
org.apache.lucene.search.Query luceneQuery = qb.bool()
.must(cooperativeQuery)
.must(memberQuery)
.createQuery();
Query query = fullTextSession.createFullTextQuery(luceneQuery, MemberProfile.class)
.setFirstResult((pageNumber - 1) * pageSize)
.setMaxResults(pageSize);
members = query.getResultList();
dao.commit();
} catch (Exception ex) {
dao.rollback();
logger.error("error thrown - ", ex);
throw new Exception(ex);
} finally {
dao.closeSession();
}
return members;
}
答案 0 :(得分:1)
查询似乎正确。我最好的猜测是,合作社名称中有一些常用词(例如“ coop”或“ cooperative”),而对合作社名称的查询最终会匹配许多不同的合作社。
由于您将ID作为输入,因此最好直接在合作ID上运行查询。
首先,请确保在您的@IndexedEmbedded
中包括合作社ID:
public class MemberProfile {
// ...
@ManyToOne
@IndexedEmbedded(includeEmbeddedObjectId = true) // Change this
private Cooperative cooperative;
// ...
}
然后重新索引您的数据。
然后更改代码的这一部分:
Cooperative cooperative = (Cooperative) dao.searchObject(Cooperative.class, cooperativeId);
FullTextSession fullTextSession = Search.getFullTextSession(dao.getSession());
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(MemberProfile.class).get();
org.apache.lucene.search.Query cooperativeQuery = qb
.keyword()
.onField("cooperative.cooperativeName")
.matching(cooperative.getCooperativeName())
.createQuery();
对此:
FullTextSession fullTextSession = Search.getFullTextSession(dao.getSession());
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(MemberProfile.class).get();
org.apache.lucene.search.Query cooperativeQuery = qb
.keyword()
.onField("cooperative.id")
.matching(cooperativeId)
.createQuery();
答案 1 :(得分:0)
我终于找到了解决方案,我不得不在Cooperative实体上将Analyze.YES设置为Analyze.NO,并为我的实体重新编制索引,如下所示: 非常感谢@yrodiere,非常感谢您的帮助。
@索引 公共类合作社实现java.io.Serializable {
@DocumentId
private int id;
private Long version;
private CooperativeIndustry cooperativeIndustry;
private CurrencyType currencyType;
private FrequencyOfContribution frequencyOfContribution;
private State state;
private String cooperativeCode;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.NO)