Want to learn more about search in Drupal 7? Then check out our online video course called “Build Powerful Search Pages with Views and Search API in Drupal 7“.
The Search API ranges module allows you to easily create a search API page with a range facet. For example, you could create a search page where users can filter products based on price, by simply selecting a price range from a facet.
The Commerce Kickstart distribution uses this module for price filtering on their product search page. If you want to see the facet in action go to the Commerce Kickstart demo site and you should see the Price facet in the left sidebar.
In this tutorial we’ll setup a range facet (slider) for the “Comment count” property on content. This will allow users to filter content by the number of comments.
For this tutorial I’ll assume you have a basic search page already setup using Search API. If you have never used Search API before please read “Intro To Search API (Part 1) – How To Create Search Pages” and “Intro To Search API (Part 2) – Faceted Search” before you begin.
First, download and install Search API ranges.
If you use Drush, run the following command:
$ drush dl search_api_ranges $ drush en search_api_ranges
In this section, we’ll use the “Ranges: Min/Max UI Slider” facet on the “Comment count” property. This allows users to filter by the amount of comments on a piece of content.
First, we’ll have to add the “Comment count” property to our index. If this property is not indexed, then the facet will not appear.
1. Go to Configuration -> “Search API” and click on the index edit link, then fields.
In this example I’m using the standard “Default node index” index that Search API creates when you install the module.
2. Scroll down on the fields page and check the “Comment count” property checkbox, then click on “Save changes”.
3. Click on the Facets tab in the top right corner and check the “Comment count” checkbox and click on “Save changes”.
4. Click on the Status tab in the top right corner and make sure all your content has been indexed.
All we have done is add the “Comment count” property to our index and enabled the corresponding facet for that property.
5. Finally, go to Structure -> Blocks and assign “Facet API: Search service: Default node index : Comment count” to a region.
At this point you should have a search page with content and the facet should appear in a region. By default our facet is using the Links display widget, however we need to change the display widget to “Ranges: Min/Max UI Slider”.
1. Hover over the facet and click on the cogwheel then click on “Configure facet display”.
The cogwheel will only appear if you have the core Contextual links module installed. If you installed Drupal using the standard installation profile, then this module will be installed.
2. Select “Ranges: Min/Max UI Slider” from the “Display widget” drop-down and click on “Save and go back to search page”.
Once you’re back on the search page the “Comment count” facet should display a slider.
Search API ranges also offers two other facet widgets: “Ranges: Text links” and “Ranges: Drop down list”. If you want to use any those widgets instead, simply edit the facet and select one of them.
Here is what the “Ranges: Text links” facet looks like:
And here is what the “Ranges: Drop down list” facet looks like:
If you have any questions, please leave a comment.
14 thoughts on “Using Search API Ranges Module In Drupal 7”
Many thanks for your post.
Could you please help to integrate facet api for multi select checkbox.
I have never setup a multiselect facet. After a quick google search I found facetapi multiselect (http://drupal.org/project/facetapi_multiselect).
Hope this helps.
How can I change the label “ranges from 0 to99” into something more customised?
I have never had a need to change the label, but I would look at the two following options.
1. Override the “text-range” form element in the “search_api_ranges_block_slider_view_form” function.
$form['text-range'] = array(
'#markup' => '
'!field_name ranges from !prefix_' . $variables['range_field']
. '@from!suffix_' . $variables['range_field'] . ' to !prefix_'
. $variables['range_field'] . '@to!suffix_' . $variables['range_field'],
'!field_name' => t($variables['name']),
'@from' => number_format($variables['min'], 0),
'@to' => number_format($variables['max'], 0),
'!prefix_' . $variables['range_field'] => $variables['prefix'],
'!suffix_' . $variables['range_field'] => $variables['suffix'],
) . '
2. Or use String Overrides module (https://drupal.org/project/stringoverrides) to modify the string in the “text-range” form element.
I just stumbled in here by accident, and I totally need to configure a search function like this on an Ubercart installation!! Before I jump into it, will this work on an Ubercart site, do you know? Drupal 7 and Ubercart 3.x
I have never used this module to filter Ubercart products. That being said, this module is not tied to Drupal Commerce.
The module allows you to filter entities using decimal or integer entity properties. As long as Ubercart exposes product price as an entity property, it should work.
Try it out. 🙂
I should have time to check it out later this week. I’ll report back and let ya know!
Once we slide from right to left, and the page refreshes, the search range lost its original maximum value, and the slider is unable to slide back to the original maximum value. Is there a way to fix this problem.
for example, if we slide from 99 to 50, and the page refreshes, the maximum value for the slider become 50, rather than 99, so we are unable to slide to 99 again.
I don’t know why it’s doing that, try asking in the issue queue (https://drupal.org/project/search_api_ranges).
Thanks, found the discussion and patch here https://drupal.org/node/1989776
Hi Ivan, I was wondering if this widget can also be used on node post date to filter the nodes between a range. Since Search API does not support date filter. Can you tell something about it?
I’ve never used it for the post date but I found this issue: https://www.drupal.org/node/1920384
I don’t think the post date is supported.
what about drupal 8 (commerce v2) ??
No Drupal 8 version as of yet, https://www.drupal.org/node/2682951