Nov 1, 2018
Dru Sellers
•
Product
•
4
min read
Black Friday is coming up soon, and a lot of eCommerce companies are doing everything they can to prepare. Elasticsearch has some handy under-the-hood features that can improve how much money your search engine can generate for your site. Whether you’re using Elasticsearch for an eCommerce site or not, it never hurts to know different ways to better tune your search results.
Say a customer comes to your website searching for “gloves.” Your search engine diligently goes off and retrieves every document with the word “gloves” in it. You could easily imagine there would be a multitude of listings. To drive the point home, here is a hypothetical data set for this:
<pre><code>[{ "item_name": "Basic Gloves", ... various attributes "meta_sales_count": 90, "meta_margin": .20, "meta_age_days": 450 },{ "item_name": "Luxury Gloves", ... various attributes "meta_sales_count": 40, "meta_margin": .30, "meta_age_days": 2 }]</code></pre>
If you are a keen business person, you might notice to your surprise that the Luxury Gloves are ranking significantly lower than a group of Basic Gloves. But the margin on those Luxury Gloves is so much higher! How do I get the Luxury Gloves to show up higher in search results than the Basic Gloves?
Thankfully, in this example the developer has indexed into these documents a hidden (in that it’s not shown on the search results page) meta field that describes the item’s margin called “meta_margin”.
Elasticsearch makes it easy to alter the default scoring mechanism. By default Elasticsearch only scores your documents based on your query - basically, on how closely the word “gloves” matches the text in the document. In order to adjust the document scores based on the “meta_margin” field, we are going to use an Elasticsearch feature called boost. Using boost you can, at query time, change where a document will show up in the search results, by applying a positive “factor” to the field we can get higher margin products to sort to the top (If you use a negative number they will go down the search results page, and is often called a “bury”). It’s important to remember that with the great power to alter the search results comes greater responsibility to give our customers what they are actually looking for. Therefore, in practice, I will prefer this boost to be a relatively small number, my goal is that it becomes more of a tie-breaker, rather than something used to dramatically change results.
Example query using boost:
<pre><code>GET ~/_search { "query": { "function_score": { "query": { "match": { "item_name": { "query": "gloves" } } }, // https://www.elastic.co/guide/en/elasticsearch/guide/2.x/boosting-by-popularity.html#_modifier "field_value_factor": { "field": "meta_margin", "modifier": "log1p", "factor": 1.01 }, "boost_mode": "sum" } } } </code></pre>
As with any change to your query methodology, you will want to test the before and after results of this change and make sure that you aren’t impacting your search results in unexpected ways.
Schedule a free consultation to see how we can create a customized plan to meet your search needs.