无论才能、知识多么卓著,如果缺乏热情,则无异纸上画饼充饥,无补于事。

请问下es可以在date字段上查询月日吗

Elasticsearch | 作者 mcs41531 | 发布于2018年03月03日 | 阅读数:6148

下单时间字段类型是date,想查询每年11月11日下单的记录?
有什么办法可以zh直接查
已邀请:

eric930721

赞同来自:

可以查,意思就是每个月第十一天,可以用 script inline

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

可以实现,已经验证过:POST index_*/_search
{
  "size":100,
  "_source": {
    "includes": [
      "title",
      "create_time"
    ]
  },
    "query": {
        "bool" : {
            "must" : [{
                "script" : {
                    "script" : {
                        "inline": "doc['create_time'].getDate().getMonthOfYear() == 11",
                        "lang": "painless"
        
                    }
                }
            },
            {
                "script" : {
                    "script" : {
                        "inline": "doc['create_time'].getDate().getDayOfMonth() == 11",
                        "lang": "painless"
        
                    }
                }
            }
            ]
        }
    }
}

fish_to_sky

赞同来自:

我按照你的写法,报错。 es用的7版本,字段存在 也有值
POST yimi_crm_label_test/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"birthday": {
"value": "2019-10-16"
    }
}}]}}}

查询结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "yimi_crm_label_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "A",
"_source" : {
"birthday" : "2019-10-16"
}}]}}
我就加了一条数据

接下来是按照你的方法,查月份是10
POST yimi_crm_label_test/_search
{
"query":{
"bool": {
"must": [
{
"script":{
"script": {
"source": "doc['birthday'].getDate().getMonthOfYear() == 10"
}}}]}}}
就会报错:
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"doc['birthday'].getDate().getMonthOfYear() == 10",
" ^---- HERE"
],
"script": "doc['birthday'].getDate().getMonthOfYear() == 10",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "yimi_crm_label_test",
"node": "Tb8HxaWtQFyQDSpULGxhyw",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"doc['birthday'].getDate().getMonthOfYear() == 10",
" ^---- HERE"
],
"script": "doc['birthday'].getDate().getMonthOfYear() == 10",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.index.fielddata.ScriptDocValues.Dates, getDate/0] not found"
}
}
}
]
},
"status": 400
}

要回复问题请先登录注册