Typo tolerance on Filters

We have been investigated the docs in trying to find out the best approach/way to filter a search, but with keeping typo tolerance as an available option. The main motivation for wanting that is to (just in case) make sure things match even if the data isn’t exactly cleanest.

For instance, if I am searching for a city named Dallas … I want to limit my search to only find records in the city attribute that has Dallas, and ignore records that might match that on other fields. With a slight concern about data cleanliness, we still want to make sure that if records that store Dllas in the city attribute came across, that it would still be matched.

I know there is a restrictSearchableAttributes that could kind of give us a more specific query term on only certain attributes, but is there a way to sync multiple of these and pair them with each other. For instance:

{
  "query": "Dallas, Texas",
  "restrictSearchableAttributes": [
    "city",
    "state"
  ]
}

and

{
  "query": "Texas, Dallas",
  "restrictSearchableAttributes": [
    "city",
    "state"
  ]
}

Would ideally return different results. This is kind of a lackluster example, but the core of the problem we are trying to face. As well as, wanting the query to be a join of those search terms, so both Dallas and Texas being true.

One approach we have thought of is having a field that is only searchable with a join of those two things. So, the searchable attribute would be cityState: Dallas, TX.

Was hoping there was something we were overlooking in the docs that could maybe help here, thanks!

Hi,

If I understand your issue correctly, you want to be able to detect when a query matches a specific attribute (in your example, detecting that “Dallas” matches the “city” attribute) and restrict search to this attribute so records that contain “Dallas”, but in other attributes, don’t get returned. Is that right?

So for example, let’s say we have a concerts index, with bands and the cities in which they are going to play:

[
  {
    "band": "Texas",
    "city": "Portland",
    "state": "Orgeon"
  },
  {
    "band": "U2",
    "city": "Dallas",
    "state": "Txas"
  },
  {
    "band": "Architecture in Helsinki",
    "city": "Dallas",
    "state": "Texas"
  }
]

Here, we have “Texas” the state, and “Texas” the band. Also, we have a misspelled record with “Txas”. According to your use case, if we type “Texas”, we wouldn’t retrieve the record containing “Texas” the band playing in Portland, Oregon because we only want to get hits when “state” matches “Texas”.

In that case, you could use Query Rules: https://www.algolia.com/doc/guides/query-rules/query-rules-overview/

What you’d need to do is first, set “band”, “city” and “state” as searchable attributes, and “city” and “state” as attributes for faceting. Then, you would set a query rules so that when a query matches a state, you restrict searchable attributes on “city” and “state” only. This would look like this (JavaScript example):

index.saveRule({
  objectID: 'a-rule-id',
  condition: {
    pattern: '{facet:state}',
    anchoring: 'contains'
  },
  consequence: {
    params: {
      restrictSearchableAttributes: ['city','state']
    }
  }
});

This way, when typing “Texas”, you would only get results for the records that match “Texas” as a state.

Hope this helps! Don’t hesitate to tell me if I​​ can help you any further.

Thanks for your reply, Sarah!

Follow up question to that would be, in this example that you shared, what if we wanted users to be able to search not just for states, but to separately input a query for bands … so a query for bands could still be “Texas”, and not return states that match on Texas?

We would also like the ability to create joined queries, so searching and returning only bands that are in Texas, and are also named Texas.

I feel as though the query rule would always and only return us states that match the query but will ignore the possibility of also wanting to search for bands too.