Overview
Filter based facet requests provide the greatest flexibility in defining facet buckets. Filter-Based Facets allow using any query to be used to define a facet bucket. The bucket count is the number of documents that match both the query and the Filter-Based Facet bucket filter.
View incoming links.
Requesting Filter-Based Facet Requests
The following sections describe how to request a filter-based facet using the Java and HTTP/REST Query APIs.
Java Client API
Filter-Based Facets can be requested via the Java Client API using the FilterBasedFacetRequest . Note that multiple FilterBasedFacetRequest objects can be added to the QueryRequest to request multiple facets.
Syntax
Consult the javadoc page for FilterBasedFacetRequest .
Example (Java)
Using query filters allows explicitly defining the criteria for a bucket.
This example creates buckets for specific combinations of type
and collection
field values.
SearchClient client = ...; QueryRequest query = new QueryRequest("*:*"); // Add the FilterBasedFacetRequest to the query FilterBasedFacetRequest facet = new FilterBasedFacetRequest("combo"); facet.add("PDF Whitepapers", new QueryString("AND(type:pdf, collection:whitepapers)", QueryLanguages.ADVANCED) ); facet.add("PDF Designs", new QueryString("AND(type:pdf, collection:designs)", QueryLanguages.ADVANCED) ); facet.add("PDF Reports", new QueryString("AND(type:pdf, collection:reports)", QueryLanguages.ADVANCED) ); facet.add("Word Whitepapers", new QueryString("AND(type:word, collection:whitepapers)", QueryLanguages.ADVANCED) ); facet.add("Word Designs", new QueryString("AND(type:word, collection:designs)", QueryLanguages.ADVANCED) ); facet.add("Powerpoint Reports", new QueryString("AND(type:powerpoint, collection:reports)", QueryLanguages.ADVANCED) ); query.addFacet(facet); // Get the response QueryResponse response = client.search(query); // Show the buckets System.out.println("Buckets:"); for (FacetBucket bucket : response.getFacet("combo")) { System.out.printf("%s - %d\n", bucket.getLabel(), bucket.getCount()); }
Geographic Proximity Facet Example (Java)
A perfect example of using Filter-Based Facets is to generate a facet using the geographic search distance filters provides by AIE.
The example below return facet buckets for documents within 5 miles, within 10 miles and within 50 miles of a specified center point.
DistanceUnits distance = DistanceUnits.MILES; GeoUnits units = GeoUnits.DEGREES; String field = FieldNames.POSITION; double centerX = 70.0; double centerY = 70.0; SearchClient client = ...; QueryRequest query = new QueryRequest("*:*"); // Add the FilterBasedFacetRequest to the query FilterBasedFacetRequest facet = new FilterBasedFacetRequest("distance"); facet.add("< 5 miles", new GeoDistanceFilter(field, centerX, centerY, 0.0, 5.0, units, distance)); facet.add("5 - 10 miles", new GeoDistanceFilter(field, centerX, centerY, 5.0, 10.0, units, distance)); facet.add("10 - 50 miles", new GeoDistanceFilter(field, centerX, centerY, 10.0, 50.0, units, distance)); query.addFacet(facet); // Get the response QueryResponse response = client.search(query); // Show the buckets System.out.println("Distance:"); for (FacetBucket bucket : response.getFacet("distance")) { System.out.printf("%s - %d\n", bucket.getLabel(), bucket.getCount()); }
JSON REST API
See the REST API Tutorial for examples showing how to issue a JSON REST query.
See the REST Query API page for details of composing JSON REST queries.
{
"workflow": "search",
"query": "*:*",
"queryLanguage": "advanced",
"locale": "en",
"rows": 10,
"facets" : ["size(filter=small(\"size:>120\"))"],
"fields": [ ".id", "title", "text", "date" ]
}
XML REST API
Filter-Based Facets are not supported by the XML REST API.