Facets

Facets allow the user to easily refine the search results using the content of the index.


So that indexed properties can be used as facets, the attribute aggregatable must be set to true for the property in the CategoryDescriptor.

Important: Only string values are currently available to facets.

Facets are automatically displayed in the Mindbreeze client but can be deactivated with the option aggregations.


In the search service the same facets are available as in the client service. But you can also request any facets you want with addAggregation for a View.


…
View.newBuilder()
	.setId("View")
	.setCount(10)
	.addAggregation("name")
…


The facets are contained as a list of aggregation objects in the ResultSet.


<% 
for (Aggregation aggregation : resultSet.getAggregationList()) {				
		if (aggregation.getEntryCount() == 0) continue;
%>
<div class="facet">
		<div class="facetName">
<b><%="mes:date".equals(aggregation.getName())? "Datum" : aggregation.getName()%></b>
</div>
		<div class="facetEntries">
<% 
			for (Aggregation.Entry entry : aggregation.getEntryList()) {
				byte[] queryExpr = entry.getQueryExpr().toByteArray();
				String facetQuery = Base64.encodeBase64URLSafeString(queryExpr);
%>
		<div><a href="<%=Nav.self(request, "start", "0")%>&facet=<%=facetQuery%>"><%= entry.getName() %> (<%= entry.getCount() %>)</a></div>	
<% 	
			} 
%>
</div>
</div>
<% 
} 
%>


Every characteristic of the facets contains the value of the property, the number of the objects and a QueryExpr object for the restriction of the documents. You can use this QueryExpr object when searching to restrict the search results.


QueryExpr.And.Builder constraints = QueryExpr.And.newBuilder();
String[] facets = request.getParameterValues("facet");
if (facets != null) { 
	for (String facet : facets) {
		if (facet.isEmpty()) continue;
		QueryExpr queryExpr = QueryExpr.newBuilder().mergeFrom(Base64.decodeBase64(facet)).build();
		constraints.addExpr(queryExpr);		
	}
}
if (constraints.getExprCount() > 0) {
searchRequest.setQueryConstraints(QueryExpr.newBuilder()
.setKind(QueryExpr.Kind.EXPR_AND)
.setAndExpr(constraints)
);
}