gpt4 book ai didi

java - lucene/hibernate : @FullTextFilterDef not working with FilterFactory

转载 作者:行者123 更新时间:2023-12-01 10:00:30 26 4
gpt4 key购买 nike

我在 JUnit 测试下方构建了一个与过滤器相结合的简单全文搜索:

@Test
public void D_testFilterFactory() throws Exception {

// get the full text entity manager
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

// create the query using Hibernate Search query DSL
QueryBuilder queryBuilder = fullTextEntityManager
.getSearchFactory()
.buildQueryBuilder()
.forEntity(InspectionMaster.class)
.get();

// Build Query !
Query query = queryBuilder.keyword().wildcard().onField("itemDesc").matching("*").createQuery();

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, InspectionMaster.class);

// wrap Lucene query in an Hibernate Query object
logService.log("TEST : Test search on itemDesc = '*' return " + fullTextQuery.getResultSize() + " documents");

// test filter !
fullTextQuery.enableFullTextFilter("siteFilter").setParameter("siteID", "NEW");
logService.log("TEST : Test search on itemDesc = '*' with filter on site.siteId = 'NEW' return " + fullTextQuery.getResultSize() + " documents");

}

我的过滤器在实体中声明为:

@Entity
@Indexed
@FullTextFilterDefs( {
@FullTextFilterDef(name = "siteFilter", impl = siteFilterFactory.class)
})
@Table(name="LGIMAS")
public class InspectionMaster implements Serializable {

我还构建了一个filterFactory:

public class siteFilterFactory {
private String siteID;

/**
* injected parameter
*/
public void setSiteID(String siteID) {
this.siteID = siteID;
}

@Key
public FilterKey getKey() {
StandardFilterKey key = new StandardFilterKey();
key.addParameter( siteID );
return key;
}

@Factory
public Filter getFilter() {
Query query = new TermQuery( new Term("site.siteId", siteID ) );
return new CachingWrapperFilter(new QueryWrapperFilter(query));
}
}

当然,字段“site.siteId”是我的实体的嵌入式索引字段:

   @ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="SITID")
@IndexedEmbedded
private Site site;

我有以下返回:

*** LOG [Mon Apr 25 23:28:09 CEST 2016] TEST : Test search on itemDesc = '*' return 18 documents
*** LOG [Mon Apr 25 23:28:09 CEST 2016] TEST : Test search on itemDesc = '*' with filter on site.siteId = 'NEW' return 0 documents

与预期不同,我的表中有 18 个文档,其中两个文档的 site.siteId = 'NEW'。

我尝试仅使用 TermQuery,而不进行过滤: public void C_testFilter() 抛出异常 {

    // get the full text entity manager
Session hibernateSession = em.unwrap(Session.class);
FullTextSession fullTextSession = Search.getFullTextSession(hibernateSession);

BooleanQuery bq = new BooleanQuery();
TermQuery filterNEW = new TermQuery(new Term("site.siteId", "NEW"));
bq.add(filterNEW, BooleanClause.Occur.MUST);
Query q = new QueryParser(Version.LUCENE_36, "cs-method", new StandardAnalyzer(Version.LUCENE_36)).parse(bq.toString());

org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(q, InspectionMaster.class);
logService.log("TEST : Test filter on site.siteId = 'NEW' " + hibernateQuery.list().size() + " documents");

}

使用此代码,一切都按预期工作:

*** LOG [Mon Apr 25 23:42:49 CEST 2016] TEST : Test filter on site.siteId = 'NEW' 2 documents

非常欢迎任何帮助。谢谢!

最佳答案

所以我最终屈服于过滤器,不起作用,我也没有找到原因!

对于同样情况的人,请在下面找到我的完整搜索功能,它的工作方式就像一个魅力(但没有“过滤器”):

  public Page<InspectionMaster> search(String text, String filterStatus, String filterSite, Pageable pageable) {

// get the full text entity manager
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

// create the query using Hibernate Search query DSL
QueryBuilder queryBuilder = fullTextEntityManager
.getSearchFactory()
.buildQueryBuilder()
.forEntity(InspectionMaster.class)
.get();

// Add wildcard to always have a full search capability
text = "*" + text.toLowerCase() + "*";

// Prepare filter on Status and Site
Query filterStatusQuery;
if (filterStatus.isEmpty()) {
filterStatusQuery = queryBuilder.all().createQuery();
} else {
filterStatusQuery = queryBuilder.keyword().onField("status").matching(StatusType.fromDescription(filterStatus)).createQuery();
}
Query filterSiteQuery;
if (filterSite.isEmpty()) {
filterSiteQuery = queryBuilder.all().createQuery();
} else {
filterSiteQuery = queryBuilder.keyword().onField("site.siteId").matching(filterSite).createQuery();
}

// Prepare full text search
Query fullTextQuery = queryBuilder
.bool()
.should( queryBuilder.keyword().wildcard().onField("site.sitedescription").matching(text).createQuery() )
.should( queryBuilder.keyword().wildcard().onField("vendorNr").matching(text).createQuery() )
.should( queryBuilder.keyword().wildcard().onField("vendorDesc").matching(text).createQuery() )
.should( queryBuilder.keyword().wildcard().onField("itemNr").matching(text).createQuery() )
.should( queryBuilder.keyword().wildcard().onField("itemDesc").matching(text).createQuery() )
.should( queryBuilder.keyword().wildcard().onField("whmPickLocation").matching(text).createQuery() )
.createQuery();

// Build final Query !
Query finalQuery = queryBuilder
.bool()
.must( filterStatusQuery )
.must( filterSiteQuery )
.must( fullTextQuery )
.createQuery();

// wrap Lucene query in an Hibernate Query object
FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(finalQuery, InspectionMaster.class);

jpaQuery.setFirstResult(pageable.getOffset());
jpaQuery.setMaxResults(pageable.getPageSize());

int resultSize = jpaQuery.getResultSize();

// execute search and return results (sorted by relevance as default)
@SuppressWarnings("unchecked")
List<InspectionMaster> results = jpaQuery.getResultList();

return new PageImpl<>(results, pageable, resultSize);
}

这允许使用“3”个条件进行搜索:

  • 状态上有 1 个“必须”条件(如过滤器)
  • 网站上有 1 个“必须”条件(第二个过滤条件)
  • 对所有其他字段进行 1 次全文搜索

希望这对某人有帮助!

关于java - lucene/hibernate : @FullTextFilterDef not working with FilterFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com