user2173353
user2173353

Reputation: 4660

OpenSearch range query not working correctly on dates?

I have created an index using the OpenSearch.Client library and I perform queries on it, again using OpenSearch.Client.

I have noticed that some range queries do not work as expected on date fields.

I try to index and send all dates in UTC, just to be sure, but it doesn't help.

For example, this is a query I send (taken from the logs and tested via OpenSearch Dashboards):

POST documents/_search
{
  "query": {
    "bool": {
        "adjust_pure_negative": true,
        "boost": 1.0,
        "must": [
            {
                "bool": {
                    "adjust_pure_negative": true,
                    "boost": 1.0,
                    "must": [
                        {
                            "range": {
                                "copies.creationDate": {
                                    "boost": 1.0,
                                    "from": "2023-09-06T21:00:00Z",
                                    "include_lower": true,
                                    "include_upper": true,
                                    "to": "2023-09-29T20:59:00Z"
                                }
                            }
                        }
                    ]
                }
            }
        ]
    }
  }
}

...and here is one of the hits I get back in return, having the date field set at 2023-09-05T07:59:53.0770000Z:

query results

What is going on here?

Both values seem to be in UTC (that's what I think the Z means), and the from in the query has a bigger value than the search result!

Increasing the query from value even more does eliminate the result, but I am not sure why it doesn't work as expected. Is there some timezone logic built into OpenSearch that screws things up?

This drives me insane.

Upvotes: 0

Views: 637

Answers (1)

glenacota
glenacota

Reputation: 2547

I see that copies is an array of objects. Could it be that the array in your response includes also dates satisfying the range query, other than the one in your screenshot? If that is the case, you should look for nested queries.

Upvotes: 1

Related Questions