Search code examples
elasticsearchboolean

Elastic Search query boolean field


I have a query in elastic search where I check if a boolean field is true. I want to return all documents with this is_sellable field true.

What is the efficient way to query boolean field?

GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "is_sellable": true
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": {
              "query": "tomas 48 coming home",
              "operator": "OR",
              "fuzziness": "AUTO",
              "prefix_length": 3,
              "max_expansions": 50,
              "fuzzy_transpositions": true,
              "lenient": false,
              "zero_terms_query": "NONE",
              "auto_generate_synonyms_phrase_query": true,
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "Name": [
              "Tomas"
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "Year": [
              "2023"
            ],
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

Solution

  • As far as boolean field is concerned, your example looks good. As long as this field is mapped as boolean and is indexed (it is indexed by default) it should be fast. The should part of your query raises some questions though, but it is hard to suggest something without fully knowing your mapping and use case.