0

So the problem is something like this I want to get posts that whose online field is greater than a time or the field does not exist and I only want to use these two clauses as a filtering mechanism i.e. they should not contribute any score towards the search and there are other clauses that do the actual searching.

Here is what I currently have

{
    "query": {
        "bool": {
            "should": [
                {"range" : {"online" : {"gte" : time}}},
                {"bool": {"must_not": {"exists": {"field": "online"}}}}
            ],
        }
    }
}

The problem is I think these are contributing to the score. If a doc where the online field is greater than time it gets a score of 1.0 and if there is a doc where the online field is not present it gets a score of 0.0

Nilan Saha
  • 191
  • 1
  • 9

1 Answers1

0

You can wrap your bool should query inside the bool filter clause. In the filter clause the scoring is ignored, so the score from the range or should will not contribute any score towards the search. Modify your query as

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "range": {
                "online": {
                  "gte": 6
                }
              }
            },
            {
              "bool": {
                "must_not": {
                  "exists": {
                    "field": "online"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
ESCoder
  • 15,431
  • 2
  • 19
  • 42