[hibernate-dev] [HSearch] Facet drilldown

Hardy Ferentschik hibernate at ferentschik.de
Fri Mar 11 14:08:15 EST 2011


Hi,

sorry for the following lengthy code example, but it is hard to  
demonstrate this much shorter.
The example shows how you can enable multiple facets and then continuously  
restrict the query result
by chaining facets into a FacetFilter (which delegates to a ChainedFilter  
underneath). WDYT?
If you want to see all the code or even get the feature branch -  
https://github.com/hferentschik/hibernate-search/blob/HSEARCH-706


	public void testMultipleFacetDrillDown() throws Exception {
		final String ccsFacetName = "ccs";
		final String ccsFacetFieldName = "cubicCapacity";
		FacetRequest ccsFacetRequest = queryBuilder( Car.class ).facet()
				.name( ccsFacetName )
				.onField( ccsFacetFieldName )
				.discrete()
				.createFacet();

		final String colorFacetName = "color";
		final String colorFacetFieldName = "color";
		FacetRequest colorFacetRequest = queryBuilder( Car.class ).facet()
				.name( colorFacetName )
				.onField( colorFacetFieldName )
				.discrete()
				.createFacet();

		FullTextQuery query = createMatchAllQuery( Car.class );
		query.enableFacet( colorFacetRequest );
		query.enableFacet( ccsFacetRequest );
		assertEquals( "Wrong number of query matches", 50, query.getResultSize()  
);

		List<Facet> colorFacetList = getFacetListForFacet( query, colorFacetName  
);
		assertFacetCounts( colorFacetList, new int[] { 12, 12, 12, 12, 2 } );

		List<Facet> ccsFacetList = getFacetListForFacet( query, ccsFacetName );
		assertFacetCounts( ccsFacetList, new int[] { 17, 16, 16, 1 } );

		FacetFilter facetFilter = new FacetFilter();
		query.setFilter( facetFilter );

		facetFilter.addFacet( colorFacetList.get( 0 ) );
		colorFacetList = getFacetListForFacet( query, colorFacetName );
		assertFacetCounts( colorFacetList, new int[] { 12, 0, 0, 0, 0 } );

		ccsFacetList = getFacetListForFacet( query, ccsFacetName );
		assertFacetCounts( ccsFacetList, new int[] { 4, 4, 4, 0 } );

		facetFilter.addFacet( ccsFacetList.get( 0 ) );
		// needs to set the filter explicitly atm, because I need the query  
state to reset
		query.setFilter( facetFilter );
		colorFacetList = getFacetListForFacet( query, colorFacetName );
		assertFacetCounts( colorFacetList, new int[] { 4, 0, 0, 0, 0 } );

		ccsFacetList = getFacetListForFacet( query, ccsFacetName );
		assertFacetCounts( ccsFacetList, new int[] { 4, 0, 0, 0 } );
	}

I like the idea of using Lucene Filters to implement the drilldown for. It  
seems the most natural and the original query stays
untouched.

--Hardy




More information about the hibernate-dev mailing list