Reg: Build complex query using Boolean Operators

Hi Team,

We are doing some R&D around building queries without keys. Is this possible in algolia?

Example:
a user-enters query like this (“sam” AND false AND NOT currently_working). Is there a way that we can transform this query(key mapped) to the below query using any algolia features?

(first_name:“sam” AND disabled:false) AND NOT work_status:“currently_working”

Any helpful docs or examples will be helpful. Happy to share more details.

Thanks,

What could be an interesting approach here is to consider two parts of the query, the first being the query itself, that’s everything until the first occurrence of an operator (AND, as query isn’t optional), and adding that to the query parameter, and everything else directly to the filters parameter without transforming it. This would have the least code required and also the least chance of bugs cropping up in the transformation of the search query into filters.

What we see is that filters done exclusively via the search bar are very undiscoverable though, and prefer to recommend a filtering UI (using for example InstantSearch) so that you can easily see all options.

Thanks for the quick response and suggestions. Can you please provide some examples for the second part of the query?

some query AND brand:Algolia OR price > 10 can be split in: some query and brand:Algolia OR price > 10, which you can then use in search:

search({ query: `some query`, filters: `brand:Algolia OR price > 10`})

Assuming your frontend is in js, you can split like this: const [query, filters] = originalQuery.split(/\sAND\s(.+)/i). This split also works for queries with just a string some query or queries with empty quotes or nothing for the query "" AND someFilter:true or AND someFilter:true

1 Like