|
| 1 | +# Dynamic Faceting using Query Rules |
| 2 | + |
| 3 | +Prerequisites: Algolia, react-instantsearch, Query Rules, facets |
| 4 | + |
| 5 | +## The goal |
| 6 | + |
| 7 | +We want to add facets only when they are relevant to the search, when there's a manually manageable number of facets to add. |
| 8 | + |
| 9 | +We want to create a interface like [this one](https://preview.algolia.com/dynamic-faceting/) (which is using InstantSearch.js). The code for that interface is [here](https://github.com/algolia/demo-dynamic-faceting). |
| 10 | + |
| 11 | +## The strategy |
| 12 | + |
| 13 | +1. add all the possible facets to your indexing configuration as regular |
| 14 | +2. add query rules for the situations in which you want certain facets to show up |
| 15 | +3. render the facets decided by the query rule |
| 16 | + |
| 17 | +## Adding facets |
| 18 | + |
| 19 | +This is exactly like normally, but this time you also keep track of which facets are possible. Here I've started from the "ecommerce" dataset provided, and indexed it like in [indexing.js](./indexing/index.js) (run `yarn index` to index to your app). |
| 20 | + |
| 21 | +then we decide which queries (no typo-tolerance) you want these facets to show up. For example: |
| 22 | + |
| 23 | +```js |
| 24 | +const dynamicFacets = [ |
| 25 | + { |
| 26 | + query: 'iphone', |
| 27 | + facets: [ |
| 28 | + { attribute: 'category', widgetName: 'RefinementList' }, |
| 29 | + { attribute: 'type', widgetName: 'Menu' }, |
| 30 | + ], |
| 31 | + objectID: 'dynamic_query_iphone', |
| 32 | + }, |
| 33 | + { |
| 34 | + query: 'smartphone', |
| 35 | + facets: [ |
| 36 | + { attribute: 'brand', widgetName: 'Menu' }, |
| 37 | + { attribute: 'price', widgetName: 'RangeInput' }, |
| 38 | + ], |
| 39 | + objectID: 'dynamic_query_smartphone', |
| 40 | + }, |
| 41 | +]; |
| 42 | +``` |
| 43 | + |
| 44 | +## Adding query rules |
| 45 | + |
| 46 | +Next we need to transform that list into query rules and add them to the index. |
| 47 | + |
| 48 | +```js |
| 49 | +const algoliasearch = require('algoliasearch'); |
| 50 | +const client = algoliasearch('your_app_id', 'your_admin_api_key'); |
| 51 | +const index = client.initIndex('your_index'); |
| 52 | + |
| 53 | +index |
| 54 | + .batchRules( |
| 55 | + dynamicFacets.map(({ objectID, query, facets }) => ({ |
| 56 | + condition: { |
| 57 | + pattern: query, |
| 58 | + anchoring: 'contains', |
| 59 | + }, |
| 60 | + consequence: { |
| 61 | + userData: { |
| 62 | + type: 'dynamic_facets', |
| 63 | + facets, |
| 64 | + }, |
| 65 | + }, |
| 66 | + objectID, |
| 67 | + })) |
| 68 | + ) |
| 69 | + .then(console.log); |
| 70 | +``` |
| 71 | + |
| 72 | +These query rules can also be added via the dashboard. make sure to choose the same options as the ones added here. |
| 73 | + |
| 74 | +## Dynamically displaying facets |
| 75 | + |
| 76 | +In our query rules we made four possible dynamic facets: categories, type, brand, price. We now want to display these in a React InstantSearch app. First let's create a start: |
| 77 | + |
| 78 | +```jsx |
| 79 | +import React from 'react'; |
| 80 | +import ReactDOM from 'react-dom'; |
| 81 | +import { |
| 82 | + InstantSearch, |
| 83 | + Hits, |
| 84 | + SearchBox, |
| 85 | + Pagination, |
| 86 | +} from 'react-instantsearch-dom'; |
| 87 | + |
| 88 | +const App = () => ( |
| 89 | + <InstantSearch |
| 90 | + appId="your_app_id" |
| 91 | + apiKey="your_search_api_key" |
| 92 | + indexName="your_index" |
| 93 | + > |
| 94 | + <SearchBox /> |
| 95 | + <div |
| 96 | + style={{ |
| 97 | + display: 'grid', |
| 98 | + gridTemplateColumns: '20% 75%', |
| 99 | + gridGap: '5%', |
| 100 | + }} |
| 101 | + > |
| 102 | + <div>We will add dynamic facets here</div> |
| 103 | + <Hits /> |
| 104 | + </div> |
| 105 | + <div style={{ textAlign: 'center' }}> |
| 106 | + <Pagination /> |
| 107 | + </div> |
| 108 | + </InstantSearch> |
| 109 | +); |
| 110 | + |
| 111 | +ReactDOM.render(<App />, document.getElementById('root')); |
| 112 | +``` |
| 113 | + |
| 114 | +Now we want to create the dynamic facets. We will make a wrapper component which will read the facets to apply from the query rule state: |
| 115 | + |
| 116 | +```js |
| 117 | +import { Component } from 'react'; |
| 118 | +import PropTypes from 'prop-types'; |
| 119 | +import { |
| 120 | + connectStateResults, |
| 121 | + RefinementList, |
| 122 | + Menu, |
| 123 | + RangeInput, |
| 124 | +} from 'react-instantsearch-dom'; |
| 125 | + |
| 126 | +const DynamicWidgets = { |
| 127 | + RefinementList, |
| 128 | + Menu, |
| 129 | + RangeInput, |
| 130 | +}; |
| 131 | + |
| 132 | +class DynamicFacets extends Component { |
| 133 | + static propTypes = { |
| 134 | + limit: PropTypes.number, |
| 135 | + searchState: PropTypes.object, |
| 136 | + searchResults: PropTypes.object, |
| 137 | + }; |
| 138 | + |
| 139 | + static defaultProps = { |
| 140 | + limit: 10, |
| 141 | + }; |
| 142 | + |
| 143 | + render() { |
| 144 | + if (this.props.searchResults && this.props.searchResults.userData) { |
| 145 | + // get the data returned by the query rule |
| 146 | + const uniques = this.props.searchResults.userData |
| 147 | + // only get the dynamic facet type |
| 148 | + .filter(({ type }) => type === 'dynamic_facets') |
| 149 | + // only add one widget per attribute |
| 150 | + .reduce((acc, { facets }) => { |
| 151 | + facets.forEach(({ attribute, widgetName }) => |
| 152 | + acc.set(attribute, widgetName) |
| 153 | + ); |
| 154 | + return acc; |
| 155 | + }, new Map()); |
| 156 | + |
| 157 | + const facets = [...uniques] |
| 158 | + // limit it |
| 159 | + .slice(0, this.props.limit) |
| 160 | + // turn the name of the widget into its value |
| 161 | + .map(([attribute, widgetName]) => ({ |
| 162 | + attribute, |
| 163 | + widgetName, |
| 164 | + Widget: DynamicWidgets[widgetName], |
| 165 | + })); |
| 166 | + |
| 167 | + if (facets.length > 0) { |
| 168 | + return this.props.children({ facets }); |
| 169 | + } |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +export default connectStateResults(DynamicFacets); |
| 176 | +``` |
| 177 | + |
| 178 | +This component essentially gives us access to the dynamic facets applying here. We can now make use of these facets and render them where we used to have `<div>We will add dynamic facets here</div>`. |
| 179 | + |
| 180 | +```jsx |
| 181 | +<DynamicFacets> |
| 182 | + {({ facets }) => |
| 183 | + facets.map(({ attribute, widgetName }) => ( |
| 184 | + <pre key={attribute}> |
| 185 | + {JSON.stringify({ attribute, widgetName }, null, 2)} |
| 186 | + </pre> |
| 187 | + )) |
| 188 | + } |
| 189 | +</DynamicFacets> |
| 190 | +``` |
| 191 | + |
| 192 | +So what we have now is a component which tells us which widgets to render with which attribute. The next step is to actually render them: |
| 193 | + |
| 194 | +```jsx |
| 195 | +<DynamicFacets> |
| 196 | + {({ facets }) => |
| 197 | + facets.map(({ attribute, Widget }) => ( |
| 198 | + <Panel header={attribute} key={attribute}> |
| 199 | + <Widget attribute={attribute} key={attribute} /> |
| 200 | + </Panel> |
| 201 | + )) |
| 202 | + } |
| 203 | +</DynamicFacets> |
| 204 | +``` |
| 205 | + |
| 206 | +This gives us a final result of: |
| 207 | + |
| 208 | +[![these widgets ]()]() |
0 commit comments