amit_saxena
amit_saxena

Reputation: 7614

Elasticsearch filter for "field does not exist OR field is equal to true"

I have a search query of the following format:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "simple_query_string": {
              "query": "*",
              "fields": [
                "field1^10",
                "field2^3"
              ]
            }
          },
          "filter": [
            {
              "terms": {
                "field3": [
                  "val1",
                  "val2"
                ]
              }
            },
            {
              "terms": {
                "field4": [
                  "val4"
                ]
              }
            },
            {
              "term": {
                "field5": true
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "field1": {
              "scale": "31d"
            }
          },
          "weight": 5
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply"
    }
  },
  "size": 10
}

Now I want to modify the filter on field5 such that it filters values which either have field5 set as true or if they do not have field5 indexed. I want both kinds in the results and want to exclude only the ones which have field5 set to false.

I have tried various combinations of must_not with should inside filter but haven't been able to get the query working. Thanks for the help!

I am using elasticsearch version 6.5.1.

Upvotes: 0

Views: 680

Answers (1)

apt-get_install_skill
apt-get_install_skill

Reputation: 2908

Given the information you provided especially

I want both kinds in the results and want to exclude only the ones which have field5 set to false.

the query should look like this:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "simple_query_string": {
              "query": "*",
              "fields": [
                "field1^10",
                "field2^3"
              ]
            }
          },
          "must_not": [
            {
              "term": {
                "field5": {
                  "value": false
                }
              }
            }
          ], 
          "filter": [
            {
              "terms": {
                "field3": [
                  "val1",
                  "val2"
                ]
              }
            },
            {
              "terms": {
                "field4": [
                  "val4"
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "field1": {
              "scale": "31d"
            }
          },
          "weight": 5
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply"
    }
  },
  "size": 10
}

I added a must_not-clause to exclude all documents that have field5 equal to false.

Upvotes: 2

Related Questions