elasticsearch - ElasticSearch:是否可以在同一查询中应用两个脚本?

标签 elasticsearch

我想基于两个脚本过滤器构建查询,但似乎无法使其正常工作。我尝试过使用嵌套(按照文档中的示例进行操作),但始终收到语法错误:

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/

相关文章:

json - Elasticsearch中的删除字段

elasticsearch - Elasticsearch percolate函数中的搜索选项

mongodb - 从Elasticsearch河进口中排除mongodb字段

c# - Elastic Search .NET Core 对 Post 的低级别调用不成功

Spring 中的 ElasticSearch 与 @Query

elasticsearch - elasticsearch-certutil 创建的证书在生产中不可用?

elasticsearch - 使用单个标记的 Elasticsearch 匹配短语查询

elasticsearch - 如何在ElasticSearch或SOLR中为共享文件夹建模?

python elasticsearch索引大json时超时错误

search - 多字段,多词,不带query_string的匹配