I'm using painless to filter documents with Elastic 5.5
Using "painless", find documents with strings
field.
Only documents with strings
field are returned
All documents are returned.
All documents are returned, as long as there's a document with strings
field. This could be a caching issue of some sort.
PUT /test_idx
POST /test_idx/t/1
{
"strings": ["hello", "world"]
}
POST /test_idx/t/2
{
"numbers": [1, 2, 3]
}
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc.containsKey(params.keypath)",
"params": {"keypath": "strings"}
}
}
}
]
}
}
}
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "test_idx",
"_type": "t",
"_id": "2",
"_score": 0,
"_source": {
"numbers": [
1,
2,
3
]
}
},
{
"_index": "test_idx",
"_type": "t",
"_id": "1",
"_score": 0,
"_source": {
"strings": [
"hello",
"world"
]
}
}
]
}
}
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "test_idx",
"_type": "t",
"_id": "1",
"_score": 0,
"_source": {
"strings": [
"hello",
"world"
]
}
}
]
}
}
You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc[params.keypath].value != null",
"params": {
"keypath": "strings.keyword"
}
}
}
}
]
}
}
}