2017-10-13 6 views
0

を休止状態に関連するオブジェクトを取得する方法:私が持っている例えば、私は私のファセットを生成することができたhttps://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-facetingIからファセッティングについてハイバネート-検索文書を、次の午前検索ファセット

を、私authorNames +回数:

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とのリンクを作成したいです。しかし、author.idはファセットを使って利用できません。だから私は何を望みますか:

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だけが必要な場合は、 2つのファセット上で2つのクエリを実行し、それを使って実行します。

あなたはより多くの情報が必要な場合は、その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で起こるはずだ)。

関連する問題