0

I am trying to filter between two dates in ElasticSearch but I have the next error

This is the query

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "message": "start_time"
                    }
                },
                {
                    "range": {
                        "start_time": {
                            "gte": "09-02-2016",
                            "lte": "09-02-2016"
                        }
                    }
                }
            ]
        }
    }
}

and the error

{
      "error": {
               "root_cause": [
              {
                "type": "query_shard_exception",
                "reason": "failed to create query: For input string: \"09-02-2016\"",
                "index_uuid": "nLn_JFMDShW_PxAf7vgFyg",
                "index": "accidents"
              }
            ],
            "type": "search_phase_execution_exception",
            "reason": "all shards failed",
            "phase": "query",
            "grouped": true,
            "failed_shards": [
              {
                "shard": 0,
                "index": "accidents",
                "node": "x5unJW2PQsquLIpFsi9YQA",
                "reason": {
                  "type": "query_shard_exception",
                  "reason": "failed to create query: For input string: \"09-02-2016\"",
                  "index_uuid": "nLn_JFMDShW_PxAf7vgFyg",
                  "index": "accidents",
                  "caused_by": {
                    "type": "number_format_exception",
                    "reason": "For input string: \"09-02-2016\""
                  }
                }
              }
            ]
          },
          "status": 400
        }

This is the field to filter

enter image description here

And It's an example that how start_time is saved

enter image description here

I had understood that ElasticSearch converts from date to number to have better performance but maybe it's wrong

I would like to be sure about if I am making the query well or why I've this problem

1
  • can you please post the actual code you are using and not a screenshot, it's very hard to read and replicate otherwise Commented Nov 1, 2022 at 1:32

1 Answer 1

1

If you have configures the mapping correctly, you probably can use the format option to specify the date format. So the query looks something like the one below.

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "message": "start_time"
                    }
                },
                {
                    "range": {
                        "start_time": {
                            "gte": "09-02-2016",
                            "lte": "09-02-2016",
                            "format": "MM-dd-yyyy"
                        }
                    }
                }
            ]
        }
    }
}

Let's say you have an index like

{
  "mappings": {
    "properties": {
      "start_date": {
        "type": "date",
        "format": "date_optional_time"
      }
    }
  }
}

with data

{"index": {}}
{"start_date": "2022-10-10"}
{"index": {}}
{"start_date": "2022-10-11"}
{"index": {}}
{"start_date": "2022-10-12"}

You can search the index with the query below.

{
  "query": {
    "range": {
      "start_date": {
        "gte": "10-09-2022",
        "lte": "10-11-2022",
        "format": "MM-dd-yyyy"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.