2017-10-13 70 views
0

我下面講的小面休眠搜索文檔相關對象:我有https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting如何獲得冬眠搜索方面

我是能夠產生我的面,例如我authorNames +計數:

name1 = 100 
name2 = 200 
and so on.. 

但我的問題是如何查詢作者對象,如果我想顯示它的一些其他細節。

由例如我現在去:

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())); 

但我想創建一個ID = author.id鏈接的GUI。但是author.id不能使用facet。所以我想要的是:

List<facetName (authorName), count, referenceId> 

什麼是最簡單和有效的方式來實現呢?有沒有辦法做到這一點沒有多個查詢?

回答

1

您可以定位作者標識符而不是作者姓名。

寫一個類來保存結果:

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; 
    } 
} 

添加一個字段和方面筆者ID:

@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; 

    // ... 
} 

(如果你在Book已經制定任何限制你的@IndexedEmbedded類,如includePaths,請確保將它們更新爲包含此新面)

如果您只需要名稱和ID,則您c在兩個方面做兩個查詢並且完成它。

如果您需要更多的信息,然後更改您的代碼爲目標的ID方面:

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"); 

最後,做一些後期處理:

List<Integer> authorIds = facets.map(f -> { 
      try { 
      return Integer.parseInt(f.getValue()); 
      } 
      catch (NumberFormatException e) { 
      throw new RuntimeException("Unexpected author ID format", e); 
      } 
     }) 
     .collect(Collectors.asList()); 
List<Author> authors = fullTextEntityManager.unwrap(Session.class) 
    .byId(Author.class) 
    .multiLoad(authorIds); 
List<EntityFacet<Author>> entityFacets = new ArrayList<>(facets.size()); 
for (int i = 0; i < facets.size()) { 
    entityFacets.add(new EntityFacet(facets.get(i), authors.get(i))); 
} 
Collator nameSort = new Collator(); 
nameSort.setStrength(Collator.PRIMARY); 
Collections.sort(entityFacets, Comparator.comparing(f -> f.getEntity().getName(), nameSort)); 
entityFacets.forEach(p -> log.info(p.getEntity() + " - " + p.getCount())); 

我沒有試運行代碼,但這應該是,給出或帶來一些語法錯誤。

當然,它有點太複雜了,尤其是ID解析的東西。但是我認爲在我們改進分面之前,你可以做得更好(這應該發生在搜索6中)。