Odd behavioir of range in elasticsearch restful query when I have more than on range in the query

  Kiến thức lập trình

I am using elasticsearch 8.8.0

I currently have this query

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "deliveryStatus": {
                            "gte": -2,
                            "lte": 9
                        }
                    }
                },
                {
                    "range": {
                        "cardDate": {
                            "gte": "2024-04-16",
                            "lte": "2024-04-16"
                        }
                    }
                }
            ]
        }
    }
}

which gives me this result

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 828,
            "relation": "eq"
        },
        "max_score": 2.0,
        "hits": [
            {
                "_index": "credit",
                "_id": "70230254011604",
                "_score": 2.0,
                "_source": {
                    "cardDate": "2024-04-16T00:00:00",
                    "deliveryStatus": "3"
                }
            }
        ]
    }
}

However, when I tried to increase the upper bound of deliveryStatus from 9 to 10 as shown in this query

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "deliveryStatus": {
                            "gte": -2,
                            "lte": 10
                        }
                    }
                },
                {
                    "range": {
                        "cardDate": {
                            "gte": "2024-04-16",
                            "lte": "2024-04-16"
                        }
                    }
                }
            ]
        }
    }
}

In my understanding, the result should remain the same – however when I hit it; this is the result that I got

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

Which is odd and not aligned with my current understanding. Out of curiosity, i tried to remove the cardDate to see if I got the same result or not as shown in this query

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "deliveryStatus": {
                            "gte": -2,
                            "lte": 10
                        }
                    }
                }
            ]
        }
    }
}

and I got this result

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "credit",
                "_id": "65391401441421011",
                "_score": 1.0,
                "_source": {
                    "cardDate": "2023-11-10T00:00:00"
                    "deliveryStatus": -1
                }
            }
        ]
    }
}

Can someone explain on why and how to fix it if I want the upper bound of the deliveryStatus is 13 (I have tried to change to 10 or above, it shows me an empty list)

LEAVE A COMMENT