如何获取与hibernate搜索方面相关的对象

时间:2017-10-13 05:44:17

标签: hibernate hibernate-search facet faceted-search

我正在关注关于分区的hibernate-search文档:https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting

我能够生成我的方面,例如我有我的authorNames + count:

name1 = 100
name2 = 200
and so on..

但我的问题是我如何查询Author对象,如果我想显示它的其他一些细节。

以我现在的例子为例:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();

org.apache.lucene.search.Query luceneQuery = qb.all().createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);

FacetingRequest authorFacet = qb.facet().name("authorFacetRequest").onField("authors.name_facet").discrete()
        .orderedBy(FacetSortOrder.FIELD_VALUE).includeZeroCounts(false).createFacetingRequest();

// retrieve facet manager and apply faceting request
FacetManager facetManager = fullTextQuery.getFacetManager();
facetManager.enableFaceting(authorFacet);

// retrieve the faceting results
List<Facet> facets = facetManager.getFacets("authorFacetRequest");
facets.forEach(p -> log.info(p.getValue() + " - " + p.getCount()));

但是在GUI中我想创建一个id = author.id的链接。但是使用facet无法使用author.id。所以我想要的是:

List<facetName (authorName), count, referenceId>

实现这一目标的最简单有效的方法是什么?有没有办法在没有多个查询的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以定位作者标识符而不是作者名称。

写一个课程来保存你的成绩:

placeholder.InnerHtml = File.ReadAllText(Server.MapPath("~/path_to_SVG_file.svg"));

将Field and Facet添加到作者ID:

public class EntityFacet<T> implements Facet {
  private final Facet delegate;
  private final T entity;
  public EntityFacet(Facet delegate, T entity) {
    // ...
  }

  // delegate all Facet methods to the delegate

  public T getEntity() {
    return entity;
  }
}

(如果您在@Indexed @Entity public class Author { @Id // Discrete faceting only works on text fields, so we don't use the default bridge @Field(name = "id_for_facet", analyze = Analyze.NO, bridge = @Bridge(impl = org.hibernate.search.bridge.builtin.IntegerBridge)) @Facet(name = "id_facet", forField = "id_for_facet") private Integer id; // ... } 课程@IndexedEmbedded中有任何限制,例如Book,请确保更新它们以包含此新方面

如果您只需要名称和ID,那么您可以对这两个方面进行两次查询并完成。

如果您需要更多信息,请更改代码以定位ID方面:

includePaths

最后,做一些后期处理:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();

org.apache.lucene.search.Query luceneQuery = qb.all().createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);

FacetingRequest authorFacet = qb.facet().name("authorFacetRequest").onField("authors.id_facet").discrete()
        .includeZeroCounts(false).createFacetingRequest();

// retrieve facet manager and apply faceting request
FacetManager facetManager = fullTextQuery.getFacetManager();
facetManager.enableFaceting(authorFacet);

// retrieve the faceting results
List<Facet> facets = facetManager.getFacets("authorFacetRequest");

我没有尝试运行代码,但应该是它,给出或者采取一些语法错误。

当然,它有点过于复杂,特别是ID解析的东西。但是我不认为你可以做得更好,直到我们改进分面(这应该发生在搜索6中)。