假设我创建了一个这样的文档:
PUT idx/type/1
{
"the_field": [1,2,3]
}
我可以使用 GET /idx/type/1 检索我的文档:
{
"_index": "idx",
"_type": "type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"the_field": [
1,
2,
3
]
}
}
现在,我想检查字段“the_field”是否包含值 2。
我知道我可以使用术语子句,但我需要使用过滤器脚本检查这一点,所以我尝试了:
POST /idx/typ/_search
{
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "doc['the_field'].values.contains(2)"
}
}
}
并没有得到结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
为了测试我的 mevl 脚本语法是否正确,我尝试这样做:
POST /idx/type/_search
{
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "[1,2,3].contains(3)"
}
}
}
并得到正确的结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "idx",
"_type": "type",
"_id": "1",
"_score": 1,
"_source": {
"the_field": [
1,
2,
3
]
}
}
]
}
}
我究竟做错了什么?
我认为 doc['the_field'].values 应该返回 [1,2,3],不是吗?如果是这样,我的代码应该可以工作。
有人可以帮助我吗?
谢谢你!
UPDATE
当我将代码中的所有 [1, 2, 3] 替换为 ["a"," b", "c"] 时,它就起作用了。任何想法?