Filtering nested objects and year range

date
categories
filtersearch
range

#1

I have a pretty complex business requirement that I have been trying to solve for some time now to no avail as of yet. The basic requirement is a product mapping to vendor specific information. I have products that need to be mapped to OEM vehicles.

There is a ProductCombo model that has a type/category, many products, and many vehicle configurations. Each vehicle configuration has a beginning year, ending year, and has many makes and has many models. An example of two combinations is below:

products: ['123','456','789'],
config: [
    {
        min_year: '2018',
        max_year:'2020',
        makes: ['Acura','Honda'],
        models: ['Accord','Odyssey','RLX']
    },
    {
        min_year: '2019',
        max_year:'2020',
        makes: ['Acura'],
        models: ['RDX']
    }
]

products: ['321','654'],
config: [
    {
        min_year: '2014',
        max_year:'2017',
        makes: ['Honda'],
        models: ['Civic']
    },
    {
        min_year: '2004',
        max_year:'2010',
        makes: ['Ford'],
        models: ['F-150','F-250','F-350']
    }
]

A user will search the system by year. make, and model:
2016 Honda Civic

which should/would return the products '321' and '654'

or 2019 Acura RLX would return product #'s 123,456, and 789

while 2020 Ford F-150 would return no results.

I have looked into filtering strings or numeric values as well as facets, but I am fairly new to Algolia and am just not sure how to implement this behavior.

Any feedback or advice is greatly appreciated and thank you in advance!


#2

Hi Larry,

Thanks for your question!

Based on your search UI, you could implement this in two different ways:

1. A single search box, where users would type things like “2016 Honda Civic” and this would automatically refine on year, make and model.

You can use query rules to dynamically apply filter based on the user’s input. There are different strategies you can take depending on the outpur you are looking for, but they are all explained here: https://www.algolia.com/doc/guides/managing-results/optimize-search-results/detecting-intent/#autodynamic-filtering-based-on-the-query

The plus plan allow up to 20 query rules.

2. A multi select UI where the customer would first select the year, then select the make, then select the model.

This can be achieved through faceting. A facet is an attribute that can be used for navigation, to build a refinement list that the user can click on to filter results.

Attributes that will be used for faceting have to be set first at indexing time, you can configure the index with something like:

$index->setSettings('attributesForFaceting' => ['year', 'make', 'model'])

Then you can use refinement widgets from InstantSearch.js to display refinement lists in the UI to ease the browsing of your catalog.

You can use the configure widget to create a custom widget that would fit more precisely the specific filters you want to apply.

The first solution will require significantly more work than the second one, especially if you want to enable some kind of auto-completion.

I hope this helps, let us know if you have any more questions.


#3

Hi Marie,

Thanks for the quick reply. While the long term goal will be to implement an autocomplete search box, we are using a dependent drop down setup like you mentioned in the 2nd option. I have setup facets on the index from the Algolia UI:

I think my issue is not knowing how to setup the query on the backend because I am fairly new to Algolia. I have tried things like:

$index->search('ford f-150')->where('config.min_year', '<=', 2018) but this does not seem to work. I have also tried passing in options to my search() method:

$index->search('ford f-150', ['facetFilter'=>'config.min_year <= 2018']);

Neither of this are returning results. I know I must be missing something…


#4

Hi Larry,

Your second attempt should work, but it appears there is a typo for facetFilters (with an s)

Here is the documentation for the PHP search method:

Along with the various API Query parameters that are available:

Let me know if you have further questions.
Best regards,