2017-10-13 19 views
0

최대 절전 관련 객체받는 방법 : 내가 가지고 예를 들어 내면을 생성 할 수 있었다 https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting내가에서 패 시팅 (faceting)에 대해 최대 절전 모드 검색 문서를 다음하고 검색면

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

하지만 ID = author.id와 링크를 만들려는 GUI에서

. 그러나 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 만 있으면 두 패싯에 대해 두 가지 쿼리를 수행하고이를 수행합니다.

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

내가 실행하려고하지 않았다 :

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 패싯을 대상으로 코드를 변경 코드,하지만 그게 있어야, 줄거나 몇 가지 구문 오류가 있습니다.

허락을 받으면, 특히 자신의 이익을 위해 너무 복잡합니다. 특히 ID 파싱 관련 항목입니다. 하지만 패셔닝을 개선 할 때까지는 더 잘 할 수 있다고 생각하지 않습니다 (검색 6에서 발생해야 함).