Select index based on keywords

Is it possible to select an index based on keywords?

I have one ‘all products’ index with basic information such as name, brand, description. Then additional category indexes which contain data specific to that area. For example ‘clothes’ would have attributes for size, material, colour. Whilst ‘furniture’ would have different data such as room, dimensions, weight.

If a user searches for ‘green shirt’ I’d like to use the clothes index and show filters appropriate to the search. Likewise a search for ‘king size bed’ should use the furniture index.

Alternatively, should we be structuring indexes differently and put all the information in a single index?

Hi @x11, you can search multiple indices or you can use one index and put all your records in the same index and use a single search.

Thanks for reply but still not clear.

Let me try and rephrase.

I’m trying to understand whether category specific information, which only applies to a subset of products, should all be in the ‘main’ index or whether we need to create category specific indexes for this data.

We want to make sure that the filters and facets displayed to a user are relevant. What’s relevant depends on the search term. When searching for shoes the user should get filters for shoe size, it’s not the same dimension as the length / width / depth filters you might expect when searching for furniture.

This is why I’m trying to choose an index based on the search terms.

I don’t think multi index search is correct here, we don’t want to display the top clothing and top furniture results as seperate results. We do this for products + articles.

Hi there,

Algolia doesn’t dynamically match on an index or another based on the semantic content of a query. It’s designed to search in one or several indices and retrieve the best matches based on its ranking formula. When querying multiple indices, result sets aren’t aggregated, they remain independent.

In your case, you’d like to first do some sort of semantic match to decide which one of your indices you should query. This is possible, but this logic needs to happen on your end. For example, when the user types “green shirt”, you’d need to have logic on the front end that intercepts the query, parses it, and decides what’s the most appropriate index, then forward the query to said index. As you can guess, such logic can be complex and lead to false positives.

From what I understand of your use case, you’d like to dynamically suggest filters on the front end based on the query. So, for example, if I type “leather shoes”, you’d recognize that I’m looking for shoes and not only switch to the right Algolia index, but also display appropriate refinement lists. Again, such logic can be complex, and if the user types something that doesn’t match anything, you have a difficult corner case to manage.

Instead, I would recommend keeping a single index, and typing your records. Here’s what it could look like:

[
  {
    "name": "Sportswear Outdoors T-Shirt",
    "sizes": ["S", "M", "XL", "2XL"],
    "colors": ["cargo", "black"],
    "fabric": ["cotton", "rayon"],
    "type": "clothing"
  },
  {
    "name": "Caesar Wooden Bed Frame",
    "dimensions": {
      "length": 200,
      "width": 140
    },
    "materials": ["Mahogany", "Stainless steel"],
    "type": "furniture"
  }
]

This allows you to filter on type whenever you know a user is looking for a specific kind of item, but you can fall back to the entire index when you don’t know.

In terms of UI, there are many ways to filter on a specific category. You can do “smart” filtering based on the query, but again this can be hard to do and isn’t necessarily transparent for the user. One way you could implement it is to do two searches when user type a query: a normal one in the index, and a search for facet values for each tokenized term in the type attribute. Then, if you have matches, you could display the filter and let users select them or not.

Another, simpler approach which you can see on Amazon, they have a drop-down list next to the search box to select an existing category. You can use an InstantSearch refinementList to implement it.

1 Like

Thanks for your detailed response @sarah.dayan

That clarifies several things and puts on the right path.

Awesome! Happy coding :slightly_smiling_face: