for (String facetField : facetFields) { FacetingRequest facetingRequest = queryBuilder.facet().name(facetField + FACET_NAME_SUFFIX).onField(facetField) .discrete().orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false).maxFacetCount(facetCount) .createFacetingRequest(); facetManager.enableFaceting(facetingRequest);
for (String facetField : facetFields) { FacetingRequest facetingRequest = queryBuilder.facet().name(facetField + FACET_NAME_SUFFIX).onField(facetField) .discrete().orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false).maxFacetCount(facetCount) .createFacetingRequest(); facetManager.enableFaceting(facetingRequest);
@Test public void testAlphabeticalSortOrder() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.FIELD_VALUE ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); for ( int i = 1; i < facetList.size() - 1; i++ ) { String previousFacetValue = facetList.get( i - 1 ).getValue(); String currentFacetValue = facetList.get( i ).getValue(); assertTrue( "Wrong alphabetical sort order", previousFacetValue.compareTo( currentFacetValue ) < 0 ); } }
@Test public void testRangeDefinitionSortOrderThrowsExceptionForDiscreteFaceting() { try { queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.RANGE_DEFINITION_ORDER ) .createFacetingRequest(); fail( "RANGE_DEFINITION_ODER not allowed on discrete faceting" ); } catch (SearchException e) { // success } }
@Test @TestForIssue(jiraKey = "HSEARCH-776") public void testMaxFacetCounts() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .maxFacetCount( 1 ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); assertEquals( "The number of facets should be restricted", 1, facetList.size() ); assertFacetCounts( facetList, new int[] { 5 } ); }
@Test public void testCountSortOrderAsc() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_ASC ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); assertFacetCounts( facetList, new int[] { 4, 4, 5 } ); }
@Test public void testCountSortOrderDesc() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); assertFacetCounts( facetList, new int[] { 5, 4, 4 } ); }
@Test public void testFacetEmbeddedAndCollections() throws Exception { FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( new MatchAllDocsQuery(), Book.class ); QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Book.class ).get(); FacetingRequest facetReq = builder.facet() .name( "someFacet" ) .onField( "authors.name_untokenized" ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .maxFacetCount( 10 ) .createFacetingRequest(); List<Facet> facets = fullTextQuery.getFacetManager().enableFaceting( facetReq ).getFacets( "someFacet" ); assertEquals( "There should be three facets", 3, facets.size() ); assertFacet( facets.get( 0 ), hugo, 3 ); assertFacet( facets.get( 1 ), moliere, 2 ); assertFacet( facets.get( 2 ), voltaire, 1 ); fullTextSession.close(); }
@Test public void testZeroCountsExcluded() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); assertFacetCounts( facetList, new int[] { 5, 4, 4 } ); }
@Test public void testZeroCountsIncluded() throws Exception { FacetingRequest request = queryBuilder( Car.class ).facet() .name( facetName ) .onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( true ) .createFacetingRequest(); FullTextQuery query = queryHondaWithFacet( request ); List<Facet> facetList = query.getFacetManager().getFacets( facetName ); assertFacetCounts( facetList, new int[] { 5, 4, 4, 0 } ); }
@Test public void stringCollection() throws Exception { StringCollectionFacetEntity entity = new StringCollectionFacetEntity( 1L ); helper.add( entity ); entity = new StringCollectionFacetEntity( 1L, "foo" ); helper.add( entity ); entity = new StringCollectionFacetEntity( 2L, "foo", "bar" ); helper.add( entity ); HSQuery hsQuery = sfHolder.getSearchFactory().createHSQuery( new MatchAllDocsQuery(), StringCollectionFacetEntity.class ); QueryBuilder builder = sfHolder.getSearchFactory().buildQueryBuilder().forEntity( StringCollectionFacetEntity.class ).get(); FacetingRequest facetReq = builder.facet() .name( "someFacet" ) .onField( "facet" ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .maxFacetCount( 10 ) .createFacetingRequest(); List<Facet> facets = hsQuery.getFacetManager().enableFaceting( facetReq ).getFacets( "someFacet" ); assertEquals( "There should be two facets", 2, facets.size() ); assertFacet( facets.get( 0 ), "foo", 2 ); assertFacet( facets.get( 1 ), "bar", 1 ); }
@Test public void stringMap() throws Exception { StringMapFacetEntity entity = new StringMapFacetEntity( 1L ); helper.add( entity ); entity = new StringMapFacetEntity( 1L, "foo" ); helper.add( entity ); entity = new StringMapFacetEntity( 2L, "foo", "bar" ); helper.add( entity ); HSQuery hsQuery = sfHolder.getSearchFactory().createHSQuery( new MatchAllDocsQuery(), StringMapFacetEntity.class ); QueryBuilder builder = sfHolder.getSearchFactory().buildQueryBuilder().forEntity( StringMapFacetEntity.class ).get(); FacetingRequest facetReq = builder.facet() .name( "someFacet" ) .onField( "facet" ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .maxFacetCount( 10 ) .createFacetingRequest(); List<Facet> facets = hsQuery.getFacetManager().enableFaceting( facetReq ).getFacets( "someFacet" ); assertEquals( "There should be two facets", 2, facets.size() ); assertFacet( facets.get( 0 ), "foo", 2 ); assertFacet( facets.get( 1 ), "bar", 1 ); }
.onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_ASC ) .includeZeroCounts( true ) .createFacetingRequest();
@Test public void stringArray() throws Exception { StringArrayFacetEntity entity = new StringArrayFacetEntity( 1L ); helper.add( entity ); entity = new StringArrayFacetEntity( 1L, "foo" ); helper.add( entity ); entity = new StringArrayFacetEntity( 2L, "foo", "bar" ); helper.add( entity ); HSQuery hsQuery = sfHolder.getSearchFactory().createHSQuery( new MatchAllDocsQuery(), StringArrayFacetEntity.class ); QueryBuilder builder = sfHolder.getSearchFactory().buildQueryBuilder().forEntity( StringArrayFacetEntity.class ).get(); FacetingRequest facetReq = builder.facet() .name( "someFacet" ) .onField( "facet" ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .maxFacetCount( 10 ) .createFacetingRequest(); List<Facet> facets = hsQuery.getFacetManager().enableFaceting( facetReq ).getFacets( "someFacet" ); assertEquals( "There should be two facets", 2, facets.size() ); assertFacet( facets.get( 0 ), "foo", 2 ); assertFacet( facets.get( 1 ), "bar", 1 ); }
.onField( "type" ) .discrete() .orderedBy( FacetSortOrder.COUNT_DESC ) .includeZeroCounts( false ) .createFacetingRequest();
.onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_ASC ) .includeZeroCounts( true ) .createFacetingRequest();
.onField( Car.CUBIC_CAPACITY_STRING_FACET_STRING_ENCODING ) .discrete() .orderedBy( FacetSortOrder.COUNT_ASC ) .includeZeroCounts( true ) .createFacetingRequest();