Skip to content

Faceted Search

parisholley edited this page Sep 18, 2014 · 9 revisions

Any page that has ElasticSearch enabled (or if the API is called manually) can follow the recommendations on this page to enable faceted search. A basic implementation based on this document is provided out the box as widgets in the plugin.

Query Parameters

Though this plugin does not provide the UI or HTML to present faceting to the user, you can build this yourself while using the query API we have provided.

Boolean Search

ElasticSearch allows you to search for terms that should or must exist in the search results. This can be defined through use of nested array parameters like so:

Category must equal (category1) ?es[category][and][]=category1

Category must equal (category1) and contain (term1) ?es[category][and][]=category1&es[taxonomy][and][]=term1

Category must equal (category1) and contain both (term1 and term2) ?es[category][and][]=category1&es[taxonomy][and][]=term1&es[taxonomy][and][]=term1

Category must equal (category1) and contain either (term1 or term2) ?es[category][and][]=category1&es[taxonomy][or][]=term1&es[taxonomy][or][]=term2

Category can equal (category1 or category2) and contain either (term1 or term2) ?es[category][or][]=category1&es[category][or][]=category2&es[taxonomy][or][]=term1&es[taxonomy][or][]=term2

URL Based Implementation

One of the best features of faceting is it allows the user to know how many results are available if they decide to filter based on that value. If the page you are on has ElasticSearch enabled, you could create a widget that looks like this:

	$facets = elasticsearch\Faceting::all();

	foreach($facets as $type => $facet){
		echo '<h2>' . $type . '</h2>';

		echo '<ul>';

		foreach($facet['available'] as $option){
			$url = elasticsearch\Faceting::urlAdd(get_permalink(), $type, $option['slug']);

			echo '<li><a href="' . $url . '">' . $option['name'] . ' (' . $option['count'] . ')<a/></li>';
		}

		echo '</ul>';
	}
Clone this wiki locally