Search code examples
typesense

Typesense 'OR' query mode


Description

I have a case where I'd like to search for multiple query tokens in a single collection like:

let searchRequests = {
  'searches': [
    {
      'collection': 'products',
      'q': 'shoe hat dress perfume',
    }
  ]
}

each token contains results if I query them individually and also if I query two tokens like so: 'q': 'shoe hat',.

Is there a way to allow for more than two query items?

Expected Behavior

I expect to have results returned based on my query tokens 'shoe hat dress perfume', or in other words an OR query mode:

{
  "results": [
    {
      "facet_counts": [],
      "found": 100,
      "hits": [  
        ...
    } 
  ]
} 

Actual Behavior

The actual behavior is that nothing is found:

{
  "results": [
    {
      "facet_counts": [],
      "found": 0,
      "hits": [  
        ...
    } 
  ]
} 

Metadata

Typesense Version: 0.22.0


Solution

  • Typesense doesn't support strict ORs and currently have no plan to do so.

    To solve my problem I used filter_by instead like so:

    let searchRequests = {
      'searches': [
        {
          'collection': 'products',
          'q': '*',
          'filter_by': "category:["shoe", "hat", "dress", "perfume"]"
        }
      ]
    }
    
    

    This would return all products of category shoe/hat/dress/parfume.

    More details on usage of filter_by here: https://typesense.org/docs/0.19.0/api/documents.html#index-a-document