我正在使用Elastic 5.5 无痛地过滤文档
问题
使用"无痛"查找具有strings
字段的文档。
预期结果
仅返回具有strings
字段的文档
实际结果
所有文件都已返回。
观察
返回所有文档,只要有一个包含strings
字段的文档即可。这可能是某种缓存问题
测试用例
固定装置
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"
]
}
}
]
}
}
您可能想尝试一下,尽管出于性能原因强烈建议过度使用无痛
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc[params.keypath].value != null",
"params": {
"keypath": "strings.keyword"
}
}
}
}
]
}
}
}
为什么要求无痛?这可以通过现有查询轻松完成
{
"query": {
"exists": {
"field": "strings"
}
}
}