Multiple query terms for exact matching or fuzzy search

filters
filtering

#1

Need to query multiple exact matches… For example get hits that contain

Jon Snow OR Bran Stark OR …

So they don’t match like Jon slips on Snow or Stark hires Bran and George

And multiple fuzzy terms…

Google search would look something like this…

Daneris Targeryon OR Khal Drago OR "Jon Snow" OR "Bran Stark"

So first two matches can be fuzzy (wrong spelling etc) and other two need to be exact…

What would be the best performing way to do this…? :sweat_smile:

We could have several exact match terms… Is there a limit we should be aware of…?


#2

The first thing I need to mention is that Algolia has not specifically been designed for the use-case you’re trying to achieve. It’s really meant for realtime, as-you-type search, not complete queries with boolean filters.
It’s definitely likely you’re going to face some limitations as you try to use it this way.

That being said, there are multiple tools provided by Algolia which can help you achieve what you want.

Basically, what you’re looking for could likely be achieve with a combination of:

That being said, the real place where you can use actual boolean logic is filters.
If possible, I would encourage you to use those instead:

About limits, there’s no definite rule. It all depends on the size of your dataset and complexity of your queries.
Your queries will likely not be rejected, but might start giving non-exhaustive results.
My personal rule of thumb though is that 100 different words / filters is starting to be too much, and 1000 definitely is.
If you also use distinct, this number is way lower, as it’s very computationally expensive.


#3

Hi Jerska,

Thanks :slight_smile:
Was wondering if we need to wrap optional keywords in double quotes as well for exact terms matches…?

So if we wanna match "Jon Snow" or "Bran Stark", should the optionalWords look like…

"Jon Snow" "Bran Stark"

OR

Jon Snow Bran Stark

Wanted to know coz optionalWords have a limit of 512 characters it seems… So if we don’t need those double quotes, we would like to strip them of to save a few chars :wink:


#4

Hi,

If I use these parameters…

{
	"hitsPerPage": 1000,
	"advancedSyntax": true,
	"query": "\"Jon Snow\" \"Bran Stark\" ",
	"optionalWords": "\"Jon Snow\" \"Bran Stark\" "
}

It matches Arya Stark and Ramsay Snow and Jon Doe :flushed:

We need exact phrases to be exact even if they are optional… How do we achieve that…?


#5

Hi @shramee.srivastav, unfortunately optionalWords is not impacted by advancedSyntax. advancedSyntax only has an impact on the query parameter, not on optionalWords.

As you discovered and pointed out by @Jerska, you are currently facing Algolia limitations on what you are trying to achieve.

What you are trying to do is usually better answered by a database query than by Algolia. Right now you want to search for “Jon Snow” OR “Bran Stark”, without any typo and most probably in specific fields. While Algolia was built to specifically handle typo tolerance and search all fields at once.

Your best options now are:

  • Use your own database to do such a query
  • If Jon Snow and Bran Stark are part of an “actor” attribute, then use faceting and the filters option to achieve a query that will look like this: filters: actor:“Jon Snow” OR actor:“Bran Stark”

I hope that you’ll be able to use Algolia for other use cases still.