ElasticSearch does not run the range utils for date

收藏待读

ElasticSearch does not run the range utils for date

I am using ElasticSearch, and my query are not seeking the information that I want.

{
    "query":
    {
        "bool":
        {
            "must":
            [
                {
                    "range":
                    {
                       "date":
                       {
                           "gte": "2016-04-29 00:00:01",
                           "lte": "2016-04-29 23:59:59"
                       }
                    }
                }
            ]
        }
    }
}

The results is everything the date possible, so, is like if the query don’t exists.

POST /Test/date
{
     "date": "2016-04-28 13:43:14"
}

I various the day when I run, so I run also

POST /Test/date
{
     "date": "2016-04-29 13:43:14"
}

and

POST /Test/date
{
     "date": "2016-04-30 13:43:14"
}

Someone can help me?

This should work. You need a custom date format, basically, otherwise ES creates a string field, not a date one as it doesn’t recognize the format.

DELETE test

PUT /test
{
  "mappings": {
    "date": {
      "properties": {
        "date": {
          "type": "date",
          "format": "yyyy-mm-dd HH:mm:ss"
        }
      }
    }
  }
}

POST /test/date
{
  "date": "2016-04-28 13:43:14"
}
POST /test/date
{
  "date": "2016-04-29 13:43:14"
}
POST /test/date
{
  "date": "2016-04-30 13:43:14"
}

GET /test/date/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "gte": "2016-04-29 00:00:01",
              "lte": "2016-04-29 23:59:59"
            }
          }
        }
      ]
    }
  }
}

原文 : Hello, buddy!

相關閱讀

免责声明:本文内容来源于Hello, buddy!,已注明原文出处和链接,文章观点不代表立场,如若侵犯到您的权益,或涉不实谣言,敬请向我们提出检举。