#community-help

Switching Service from Elasticsearch to Typesense

TLDR Sebastian inquires about recreating Elasticsearch Boolean Queries in Typesense. Jason explains that Typesense behaves as should by default and suggests configuring specific must keywords with double quotes.

Powered by Struct AI
Aug 09, 2023 (4 months ago)
Sebastian
Photo of md5-e96b2df5fb7daba1e4382044bc609218
Sebastian
04:12 PM
Hi! We want to switch a service that uses Elasticsearch Boolean Queries to Typesense. Is it possible to build something similar to the query below in Typesense? We can use filters for must and must_not sections.
What would be the best way to write should queries and to boost some elements of the query over others?
POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user.id" : "kimchy" },
        "boost": 20
      },
      "filter": {
        "term" : { "tags" : "production" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 },
          "boost":5
        }
      },
      "should" : [
        { "term" : { "tags" : "env1" } },
        { "term" : { "tags" : "deployed" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
05:52 PM
should is how Typesense behaves by default. It will first look for all terms in the query in the same record, if it doesn’t find enough records (threshold defined by drop_tokens_threshold), it will drop keywords until enough matches are found.
05:53
Jason
05:53 PM
But it’s not possible to specifically indicate which specific keywords are should. The dropping of keywords I mentioned above happens left to right, then right to left

You can configure which keywords should be must by surrounding them with double quotes
Sebastian
Photo of md5-e96b2df5fb7daba1e4382044bc609218
Sebastian
09:50 PM
Thank you Jason!
09:50
Sebastian
09:50 PM
Cc Rushil