我不确定我是否正确理解了您的需求,但我建议您对日期字段使用“范围查询”。
下面的代码会返回你想要得到的结果。
{
"query": {
"range": {
"joiningDate": {
"gt": "2011-01-01",
"lt": "2012-01-01"
}
}
}
}'
希望对你有帮助。
编辑(搜索包含“13”本身的日期。)
我建议你使用 Elasticsearch 的“Multi field”功能。
这意味着您可以同时按两种不同的字段类型索引“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*"
}
}
}
}'