我想基于两个脚本过滤器构建查询,但似乎无法使其正常工作。我尝试过使用嵌套(按照文档中的示例进行操作),但始终收到语法错误:
QueryParsingException[[my_index] [_na] filter malformed, no field after start_object
查询是:
{
"query": {
"filtered": {
"query":{
"query_string": {
"query": "things.type:1 AND things.status:1"
}
},
"filter": {
"nested": {
"path": "obj",
"_cache": true,
"filter": {
"bool": {
"must": [
{
"script": "doc['things.type'].values.size() == 1"
},
{
"script": "obj['other_things.key'].values.size() >= 1"
}
]
}
}
}
}
}
}
}
我可以从第一个脚本(
"script": "doc['things.type'].values.size() == 1"
)中提取记录,并在Python列表中进行迭代(使用pyelasticsearch
来执行这些查询),但是似乎 Elasticsearch 应该可以做到这一点。
最佳答案
您有两个嵌套的对象,因此需要两个单独的嵌套过滤器。每个嵌套过滤器都适用于单个文档,因此您不能在一个嵌套子句中访问不同文档。您可以根据需要设置任意数量的过滤器,并与和连接:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "things.type:1 AND things.status:1"
}
},
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "other_things",
"filter": {
"script": {
"script": "doc['other_things.key'].values.size() >= 1"
}
}
}
},
{
"nested": {
"path": "things",
"filter": {
"script": {
"script": "doc['things.type'].values.size() == 1"
}
}
}
}
]
}
}
}
}
}
关于elasticsearch - ElasticSearch:是否可以在同一查询中应用两个脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20052847/