Haystack EU 2023: Learnings and reflections from our team
If you have built search experiences, you know creating a great search experience is a never ending process: the data ...
Senior ML Engineer
If you have built search experiences, you know creating a great search experience is a never ending process: the data ...
Senior ML Engineer
Just as with a school kid who’s left unsupervised when their teacher steps outside to deal with a distraction ...
Search and Discovery writer
Back in May 2014, we added support for synonyms inside Algolia. We took our time to really nail the details ...
Technical Writer
You’re running an ecommerce site for an electronics retailer, and you’re seeing in your analytics that users keep ...
Technical Writer
What do OpenAI and DeepMind have in common? Give up? These innovative organizations both utilize technology known as transformer models ...
Sr. SEO Web Digital Marketing Manager
As a successful in-store boutique manager in 1994, you might have had your merchandisers adorn your street-facing storefront ...
Search and Discovery writer
At Algolia, our business is more than search and discovery, it’s the continuous improvement of site search. If you ...
JavaScript Library Developer
Analytics brings math and data into the otherwise very subjective world of ecommerce. It helps companies quantify how well their ...
Technical Writer
Amid all the momentous developments in the generative AI data space, are you a data scientist struggling to make sense ...
Sr. SEO Web Digital Marketing Manager
Fashion ideas for guest aunt informal summer wedding Funny movie to get my bored high-schoolers off their addictive gaming ...
Sr. SEO Web Digital Marketing Manager
Imagine you're visiting an online art gallery and a specific painting catches your eye. You'd like to find ...
Senior Software Engineer
At Algolia, our commitment to making a positive impact extends far beyond the digital landscape. We believe in the power ...
Senior Manager, People Success
In today’s post-pandemic-yet-still-super-competitive retail landscape, gaining, keeping, and converting ecommerce customers is no easy ...
Sr. SEO Web Digital Marketing Manager
There are few atmospheres as unique as that of a conference exhibit hall: the air always filled with an indescribable ...
Marketing Content Manager
To consider the question of what vectors are, it helps to be a mathematician, or at least someone who’s ...
Search and Discovery writer
My first foray into programming was writing Python on a Raspberry Pi to flicker some LED lights — it wasn’t ...
Technical Writer
How well do you know the world of modern ecommerce? With retail ecommerce sales having exceeded $5.7 trillion worldwide ...
Sr. SEO Web Digital Marketing Manager
In a world of artificial intelligence (AI), data serves as the foundation for machine learning (ML) models to identify trends ...
Director of AI Engineering
While answering Algolia forum posts last week, I did a deep dive on Optional Filters for Algolia Search. Similar to filters and facet filters, optional filters are applied at query-time, allowing you to use all sorts of contextual information to improve results. Unlike the other filters, optional filters don’t remove records from the result set. Instead, they allow you to say, “If records match this filter, then move them up or down the ranking,” without changing the number of records returned.
Some interesting use cases:
You can use filters and facet filters to reduce the number of records in the result set at query-time, then optional filters to manipulate the rankings for the remaining records. You can even apply filter scoring to control the order of the records further.
Here’s an example that applies facet filters for product_type
and price_range
to an index from a Shopify store. The code selects the objectID
of two records to promote, then injects them as optional filters into the query. If either of those products is part of the result set that matches the other criteria in the query, those products are pushed to the top of the ranking. The code uses filter scoring to ensure the featuredProduct
will always appear above the alternateProduct
.
import algoliasearch from 'algoliasearch'; const featuredProduct = '41469303161004'; const alternateProduct = '41469346644140'; const client = algoliasearch('H2M6B61JEG', 'b1bdfc3258823bb4468815a664dce649'); // Standard replica const index = client.initIndex('shopify_algolia_products_price_asc_standard'); // with params index.search(query, { facetFilters: [[ "product_type:HardGood", "price_range:75:100" ]], optionalFilters: [[ `objectID:${featuredProduct}<score=500>`, `objectID:${alternateProduct}<score=200>` ]], hitsPerPage: 50, }).then(({ hits }) => { console.log(hits.map(item => `- ${item.title} | ${item.product_type} | ${item.objectID} | ${item.price_range}`).join('\n')); });
Notice that this code uses a standard replica of the Shopify index sorted by price. Optional filters don’t play well with Virtual Replica indices since both re-rank records at query-time, leading to unpredictable results. If you plan to use optional filters, you should use a standard replica that applies ranking at index-time.
You can also use negative optional filters to push records down the ranking. For example, if you wanted to push posts written by the current user lower in the rankings but not remove them completely:
index.search('', { filters: `date_timestamp > ${Math.floor(d.setDate(d.getDate() - 7) / 1000)}`, optionalFilters: [ `author:-${user.name}` ], hitsPerPage: 50, }).then(({ hits }) => { console.log(hits}; });
Optional filters are a powerful tool to add to your ranking tool belt, but remember that any query-time calculations will impact search performance. For instance, you shouldn’t use filter scoring on searches that may return more than 100,000 results. Always try to move ranking criteria to index configuration when possible. Use optional filters only when you need to further tune your results at query-time using more real-time context.
Let me know if you find a great (or not so great) use case for optional filters in your search UI!
Powered by Algolia Recommend