Let's say I have created a document like this:
PUT idx/type/1
{
"the_field": [1,2,3]
}
I can retrieve my document using GET /idx/type/1:
{
"_index": "idx",
"_type": "type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"the_field": [
1,
2,
3
]
}
}
Now, I want to check if the field "the_field" contains the value 2. I know I can use a term clause, but I need to check this using a filter script, so I tried:
POST /idx/typ/_search
{
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "doc['the_field'].values.contains(2)"
}
}
}
and get no results:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
To test if my mevl script syntax is right, I tried doing this:
POST /idx/type/_search
{
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "[1,2,3].contains(3)"
}
}
}
and get the right results:
{
"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
]
}
}
]
}
}
What am I doing wrong?
I think doc['the_field'].values should return [1,2,3], is not it? If so, my code should work.
Does anybody can help me? Thank you!
UPDATE
When I replace all the [1, 2, 3] in my code with ["a"," b", "c"], it works. Any idea?
POSTfor the searches?