Skip to content

Faceted Search

Paris Holley edited this page Aug 13, 2013 · 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.

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) ?category=category1

Category must equal (category1) and contain (taxonomy1) ?category=category1&taxonomy=taxonomy1

Category must equal (category1) and contain both (taxonomy1 and taxonomy2) ?category=category1&taxonomy[and][]=taxonomy1&taxonomy[and][]=taxonomy2

Category must equal (category1) and contain either (taxonomy1 or taxonomy2) ?category=category1&taxonomy[or][]=taxonomy1&taxonomy[or][]=taxonomy2

Category can equal (category1 or category2) and contain either (taxonomy1 or taxonomy2) ?category['or'][]=category1&category['or'][]=category2&taxonomy[or][]=taxonomy1&taxonomy[or][]=taxonomy2

Show Facet Count

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 can access that facet information like so:

<?php $facets = elasticsearch\Template::facets(); ?>

<?php foreach(get_terms('term') as $term): ?>
    <?php echo $term->name; ?> (<?php echo $facets['term'][$term->slug] ?: 0; ?>)
<?php endforeach; ?>

Search Results

To enable this functionality, go to Admin > ElasticSearch > Wordpress Integration and "Enable Search". With this feature on, the default wordpress search will be replaced with ElasticSearch.

Category/Taxonomy Filtering

It is recommended that you set the action to / (assuming your search URL is the homepage) to prevent pagination problems. This example would work for any taxonomy type (include categories).

<form action="/" method="GET">
    <input type="hidden" name="q" value="<?php echo $_GET['q'];?>" />
    <input type="checkbox" name="category[or][]" value="sub-category1-slug" />SubCategory 1 
    <input type="checkbox" name="category[or][]" value="sub-category2-slug" />SubCategory 2
    <input type="submit" value="Filter" />
</form>

Category Listing

To enable this functionality, go to Admin > ElasticSearch > Wordpress Integration and "Enable Category Faceting". With this feature on, ElasticSearch will then be responsible for paginating and category filtering.

Child Category Filtering

If you are viewing a parent category you can allow the user to filter by a list of categories. Whether you use and or or is up to you. It is recommended that you set the action to the root/parent category permalink to prevent pagination problems.

<form action="/category/parent-category/" method="GET">
    <input type="checkbox" name="category[or][]" value="sub-category1-slug" />SubCategory 1 
    <input type="checkbox" name="category[or][]" value="sub-category2-slug" />SubCategory 2
    <input type="submit" value="Filter" />
</form>

Taxonomy Filtering

Here is an example of a form you can place in your theme to filter the current category. Whether you use and or or is up to you. It is recommended that you set the action to the category permalink to prevent pagination problems.

<form action="/category/my-category/" method="GET">
    <input type="checkbox" name="taxonomyname[and][]" value="taxonomy1-slug" />Taxonomy 1 
    <input type="checkbox" name="taxonomyname[and][]" value="taxonomy2-slug" />Taxonomy 2
    <input type="submit" value="Filter" />
</form>
Clone this wiki locally