如何在任何嵌套字段中搜索值?

wb1gzix0  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(399)

基本上,我必须在文档中的所有字段中找到匹配的值。
我尝试使用查询字符串,但不幸的是,它只在一级字段中搜索。嵌套字段值不是使用查询字符串获取的,或者可能是我使用它的方式错误。
我试过了

GET events/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "*",
            "query": "match"
          }
        }
      ]
    }
  }
}

我将如何查询这样一种方式,它检查所有字段,包括嵌套字段。
提前谢谢

30byixjq

30byixjq1#

始终可以有多个子句,一个用于第一级字段,另一个用于组合在or条件中的嵌套字段:

GET events/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "query_string": {
            "default_field": "*",
            "query": "match",
            "lenient": true
          }
        },
        {
          "nested": {
            "path": "nested_field",
            "query": {
              "query_string": {
                "default_field": "nested_field.*",
                "query": "match",
                "lenient": true
              }
            }
          }
        }
      ]
    }
  }
}

相关问题