Here is the code snippet:
@Entity @Table(name = "BusinessCategory") public class BusinessCategory { @Field(analyze = Analyze.NO) private String name; }
@Entity @Table(name = "BusinessAccount") @Indexed public class BusinessAccount {
@Field @Column(name = "Title", length = 100, nullable = false, unique = true) private String title;
@IndexedEmbedded @ManyToOne @JoinColumn(name = "BusinessCategoryID") private BusinessCategory businessCategory;
}
public class AccountRepository {
public SearchResult findAccounts(String searchText) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
// Create index for the first time /* try { fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { e.printStackTrace(); }*/
QueryBuilder builder = (QueryBuilder) fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(BusinessAccount.class).get(); org.apache.lucene.search.Query query = builder.keyword().onFields("title").matching(searchText).createQuery();
// making faceting request on field businessCategory.name FacetContext facetContext = builder.facet(); FacetFieldContext facetFieldContext = facetContext.name("BusinessCategoryFacet"); FacetContinuationContext continuationContext = facetFieldContext.onField("businessCategory.name"); DiscreteFacetContext discreteFacetContext = continuationContext.discrete(); FacetParameterContext facetParameterContext = discreteFacetContext.orderedBy(FacetSortOrder.COUNT_DESC); facetParameterContext.includeZeroCounts(Boolean.FALSE); FacetingRequest categoryFacetingRequest = facetParameterContext.createFacetingRequest();
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, BusinessAccount.class); FacetManager facetManager = persistenceQuery.getFacetManager(); facetManager.enableFaceting(categoryFacetingRequest);
List<BusinessAccount> accounts = persistenceQuery.getResultList(); int size = persistenceQuery.getResultSize();
List<Facet> facets = facetManager.getFacets("BusinessCategoryFacet"); }
}
Facets have old count which was at the time of indexing data. means first time. after new record insert count should be updated. But the search results are fine. i can see new record in search results.
|