gpt4 book ai didi

elasticsearch - 如何在日期字段上执行通配符搜索?

转载 作者:行者123 更新时间:2023-12-02 22:51:34 25 4
gpt4 key购买 nike

我有一个字段,包含带有映射的2011-10-20之类的值:

"joiningDate": { "type": "date", "format": "dateOptionalTime" }

以下查询以 SearchPhaseExecutionException 结尾。
"wildcard" : { "ingestionDate" : "2011*" }

好像 ES(v1.1)并没有提供那么多的狂喜。 This post提出了脚本的想法( Not Acceptable 答案甚至更多)。我会尝试的,只是问是否有人做过?

期望
搜索字符串 13应该与 joiningDate字段具有值的所有文档匹配:
2011-10-13
2013-01-11
2100-13-02

最佳答案

我不确定我是否正确理解您的需求,但建议您在日期字段中使用“范围查询”。
下面的代码将返回您想要获得的结果。

{
"query": {
"range": {
"joiningDate": {
"gt": "2011-01-01",
"lt": "2012-01-01"
}
}
}
}'

希望对您有所帮助。

编辑(搜索日期本身包含“13”。)

我建议您使用Elasticsearch的“ 多字段”功能。
这意味着您可以同时按两个不同的字段类型索引“joiningDate”字段。
请查看并尝试以下示例代码。

创建索引
curl -XPUT 'localhost:9200/blacksmith'

定义映射,其中“joiningDate”字段的类型为“multi_field”。
curl -XPUT 'localhost:9200/blacksmith/my_type/_mapping' -d '{ 
"my_type" : {
"properties" : {
"joiningDate" : {
"type": "multi_field",
"fields" : {
"joiningDate" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"verbatim" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
}'

为4个文档建立索引(3个文档包含“13”)
curl -s -XPOST 'localhost:9200/blacksmith/my_type/1' -d '{ "joiningDate": "2011-10-13" }'
curl -s -XPOST 'localhost:9200/blacksmith/my_type/2' -d '{ "joiningDate": "2013-01-11" }'
curl -s -XPOST 'localhost:9200/blacksmith/my_type/3' -d '{ "joiningDate": "2130-12-02" }'
curl -s -XPOST 'localhost:9200/blacksmith/my_type/4' -d '{ "joiningDate": "2014-12-02" }' # no 13

请尝试对“joiningDate.verbatim”字段而不是“joiningDate”字段进行通配符查询。
curl -XGET 'localhost:9200/blacksmith/my_type/_search?pretty' -d '{
"query": {
"wildcard": {
"joiningDate.verbatim": {
"wildcard": "*13*"
}
}
}
}'

关于elasticsearch - 如何在日期字段上执行通配符搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com