【问题标题】:Search for Month and Day in elastic search在弹性搜索中搜索月份和日期
【发布时间】:2023-03-03 17:06:02
【问题描述】:

弹性搜索新手,请帮忙。
我的日期映射如下

{'mappings': {'properties':{
            'Reference': {'fields': {'keyword': {'ignore_above': 100,'type': 'keyword'}},'type': 'text'},
            'refdate':{'type': 'date','format':'yyyy-MM-dd'}}}

如何搜索 'Month' 和 'day' ,例如搜索 refdate 为 02-21 的所有 'Reference'(2 月 22 日) , 与年份无关)

尝试了以下查询,但出现错误

"query": {
    "bool" : {
        "filter" : {
            "script" : {
                "script" : {
                    "source": "doc['refdate'].date.getMonthValue() == 02 && doc['refdate'].date.getDayOfMonth() == 21",
                    "lang": "painless"
                }
            }
        }
    }
}

RequestError: RequestError(400, 'search_phase_execution_exception', 'runtime error')

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    以下查询正在运行

    qry = {"query": {
        "bool" : {
            "filter" : {
                "script" : {
                    "script" : {
                        "source": "doc['refdate'].value.getMonthValue() == 02 && doc['refdate'].value.getDayOfMonth() == 21",
                        "lang": "painless"
                    }
                }
            }
        }
    }}
    
    es.search(index='indexName', body=qry, size=100)
    

    如文档中所述

    long 和 date 类型的字段具有 getDate()getDates() 方法(用于多值字段)来获取具有当前 doc 值的特定于日期的辅助方法的对象。在 5.3.0 中,日期字段已更改为在调用 doc["myfield"].value 时直接公开相同的日期对象,并且不推荐使用日期对象的 getter 方法。这些方法现在已被删除(从 7.0 开始)。相反,在日期字段上使用.value,或使用Instance.ofEpochMillis(doc["myfield"].value) 将长字段显式解析为日期对象。

    参考here如何提取日、年、月等。

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多