Alright, let’s see how we can make our queries better. Before the solution, I’d like to dive a bit into aggregation to make the understanding clearer.
Pipeline Aggregations
By elastic.co definition:
Pipeline aggregations work on the outputs produced from other aggregations rather than from document sets, adding information to the output tree.
Using our last dataset, lets see how it works:
As you can see, group_by_color is inside of group_by_brand aggregation, which means that this query will give us the colors grouped by each brand, the result is:
This is one of the techniques that will help us improve the query of our last post, you will understand soon. Now, as I mentioned in the last post, lets see how we will map our index.
Index mapping
Cleanup
First, lets remove our last index:
The mapping
Index mapping is the way we tell elasticsearch how to index each field (keyword, date, etc…). Our index mapping will be it:
We have two properties: facet_name (brand, color and department) and facet_value (white, yellow, adventure, soccer, etc…).
Both of them has the type keyword. Terms aggregations must be a field of type keyword.
The "type": "nested" is very important here. Our documents will be inserted like this:
Without the type nested, ES would transform that document into this:
And then we would lose the relation between brand: ubest, color: green and `department: soccer. It would be possible, for example, to filter by facet_name: brand and facet_value: green, which doesn’t make much sense.
With nested type, the independence of the objects is maintained.
Populating / Querying
Lets populate our index for the new version:
And the query now is (using our knowledge of Pipeline Aggregations):
Here we used nothing but the pipeline aggregation, values is inside names, and we also declare the path because it is a nested type:
The response now is:
Conclusion
Now we solved our problems, if a new property must be added, it will just be a new object in array and the query won’t change. And now, it doesn’t matter if the products has the same properties or not, it will work, all we need to do is index using facet_name and facet_value.
We are good by now, but it is not enough, in the next chapter we will see how that query will be if the customer filters something. See you!
Lets see one way to use docker while developing one rails application with redis and mysql. Let’s try make the development process just like we did before co...
Leave a comment