【问题标题】:Elasticsearch query to search with mm-yyyy format on date fieldElasticsearch 查询以在日期字段上使用 mm-yyyy 格式进行搜索
【发布时间】:2017-09-25 22:19:02
【问题描述】:

我想在 yyyy-dd-mm 格式的日期字段上查询像 03-2015 这样的弹性搜索。

我试过这样,但没有成功。它没有给出任何错误,它返回0条记录

curl -XPOST "http://localhost:9200/myindex/mytype/_search?pretty" -d '{
"query": {
    "bool": {
        "must": [
            {
                "range": {
                    "deliverydate": {
                        "gte": "03-2015",
                        "lte": "03-2015",
                        "format": "mm-yyyy" 
                    }
                }
            }

        ]
    }
}

} '

我的示例文档是这样的

{
"took": 38,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 10,
    "max_score": 1,
    "hits": [
        {
            "_index": "myindex",
            "_type": "mytype",
            "_id": "33924",
            "_score": 1,
            "_source": {
                "id": 33924,
                "deliverydate": "2015-03-14",
                "name":"New test order"
            }
        }
        ]
    }

}

谁能帮我解决这个问题。这是对 elasticsearch 数据的有效搜索吗?

【问题讨论】:

    标签: elasticsearch date-format


    【解决方案1】:

    你的格式不正确(MM而不是mm),应该是

    curl -XPOST "http://localhost:9200/myindex/mytype/_search?pretty" -d '{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "deliverydate": {
                            "gte": "03-2015",
                            "lte": "04-2015",
                            "format": "MM-yyyy" 
                        }
                    }
                }
    
            ]
        }
    }}'
    

    【讨论】:

    • 没有。这不起作用..我们需要提供弹性搜索字段的格式还是需要提供搜索查询格式?
    • 你得到什么错误?请出示带有deliverydate 字段的示例文档。
    • 这是我的示例文档。 { "took": 38, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 10, "max_score ”:1,“命中”:[ {“_index”:“myindex”,“_type”:“mytype”,“_id”:“33924”,“_score”:1,“_source”:{“id”:33924 , "deliverydate": "2015-03-14", "name":"新测试订单"
    • 我没有收到任何错误..它返回 0 条记录
    • 好吧,如果您想检索 2015 年 3 月的所有记录,请查看我的更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多