Search code examples
elasticsearchquerydslelasticsearch-5

bool filter query - custom logic


I am trying to implement a filter condition where i am reading a boolean flag = electric_flag

Below is the logic i am trying to implement

if electric_flag = False --> include records which have Flag = False
if electric_flag = True --> include records which have Flag = True or False

I tried below logic but it does not seem to work correctly

body = {
    "from": 0,
    "size": 5,
    "explain": False,
    "track_scores": True,
    "_source": [
        "name",
        "id",
        "electric_flag"
    ],
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "name.exact": {
                            "value": search_term,
                            "boost": 100
                        }
                    }
                },
                {
                    "match": {
                        "name": {
                            "query": search_term,
                            "operator": "and",
                            "boost": 80
                        }
                    }
                },
                {
                    "match_phrase": {
                        "name": {
                            "query": search_term,
                            "slop": 1,
                            "boost": 70
                        }
                    }
                },
                {
                    "match": {
                        "name": {
                            "query": search_term,
                            "boost": 10000
                        }
                    }
                },


            ],
            "minimum_should_match": 1,
            "filter": {
                "bool": {
                    "should": [
                        {
                            "term":{
                                "electric_car": False
                            }
                            
                        }
                    ],
                    "should": [
                        {
                            "term":{
                                "electric_car": {{ electric_flag }}
                            }
                            
                        }
                        
                    ],
                    
                }
            }
        }
    },
    "sort": [
        "_score"
    ]
}

In the first filter should --> i always set flag to False and in the second should i read the input flag that is passed to the query from the app. But it does now work as should does not satisfy my first use case. Is there an easy way to implement this conditional logic in the filter query?


Solution

  • You could use a simple script query:

    {
      "from": 0,
      "size": 5,
      "explain": False,
      "track_scores": True,
      "_source": [
        "name",
        "id",
        "electric_flag"
      ],
      "query": {
        "bool": {
          ...
          "filter": [
            {
              "script": {
                "script": {
                  "source": "!params.electric_flag ? doc['electric_car'].value == false : true",
                  "lang": "painless",
                  "params": {
                    "electric_flag": False
                  }
                }
              }
            }
          ]
        }
      },
      "sort": [
        "_score"
      ]
    }