无痛:检查单个文档是否包含密钥
painless: check if an individual document contains a key
我正在使用 painless 通过 Elastic 5.5
过滤文档
问题
使用 "painless",查找具有 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"
]
}
}
]
}
}
为什么你要求无痛这样做?这可以通过 exists query
轻松完成
{
"query": {
"exists": {
"field": "strings"
}
}
}
您可能想试试这个,尽管出于性能原因强烈建议不要过度使用 painless
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc[params.keypath].value != null",
"params": {
"keypath": "strings.keyword"
}
}
}
}
]
}
}
}
我正在使用 painless 通过 Elastic 5.5
过滤文档问题
使用 "painless",查找具有 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"
]
}
}
]
}
}
为什么你要求无痛这样做?这可以通过 exists query
轻松完成{
"query": {
"exists": {
"field": "strings"
}
}
}
您可能想试试这个,尽管出于性能原因强烈建议不要过度使用 painless
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc[params.keypath].value != null",
"params": {
"keypath": "strings.keyword"
}
}
}
}
]
}
}
}