【问题标题】:Elasticsearch filter by matching query in all nested documentsElasticsearch 通过匹配所有嵌套文档中的查询进行过滤
【发布时间】:2021-08-18 10:50:12
【问题描述】:

我在按嵌套文档过滤弹性文档时遇到问题。

一般文档有一个嵌套资产列表,每个资产都有一个 teamId 列表

样本截断文件:

{
   "assets":[
      {
         "id":100,
         "teams":[
            1
         ]
      },
      {
         "id":101,
         "teams":[
            4,
            3
         ]
      }
   ]
}

预期结果是获得所有资产至少有一个匹配团队的根文档

我试过了:

{
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "nested": {
                                                "path": "assets",
                                                "query": {
                                                    "terms": {
                                                        "assets.teams": [
                                                            1
                                                        ]
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must_not": [
                                        {
                                            "nested": {
                                                "path": "assets",
                                                "query": {
                                                    "bool": {
                                                        "must": [
                                                            {
                                                                "exists": {
                                                                    "field": "assets"
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "size": 999
}

不幸的是,这个查询返回了文档。在这种情况下,如果查询包含 [1,3]、[1,4] 或 [1,3,4] 等 id,我确实希望它返回文档

提前致谢

【问题讨论】:

  • 您的查询应该有效。您能否提供更多详细信息,说明在哪个查询中遗漏了哪个文档。你也可以添加你的映射
  • 资产被标记为嵌套。使用此查询,我不希望从样本中获取文档,但我会在结果中得到它。我只想从查询数组中获取所有资产至少有一个匹配团队的文档
  • { "mappings":{ "properties":{ "assets":{ "type":"nested", "properties":{ "id":{ "type":"long" } , "团队":{ "type":"long" } } } } } }
  • 如果问题是您只需要匹配的嵌套文档,那么您需要将 inner_hits elastic.co/guide/en/elasticsearch/reference/6.8/… 添加到嵌套查询中。
  • 我需要根文档 :) 仅当所有嵌套文档都匹配查询时

标签: elasticsearch nested


【解决方案1】:

查找所有嵌套文档都包含任何给定术语的文档

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "assets",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "terms": {
                      "assets.teams": [
                        "1"
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
````

In the above, nested query returns documents where a nested document does not contain any of given term, then outer must_not excludes those documents.
In other words first find documents where a nested document doesnot contain given term and then exclude those documents.

If you want to include documents where teams field is not present use below

````
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "assets",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "assets.teams"
                    }
                  }
                ], 
                "must_not": [
                  {
                    "terms": {
                      "assets.teams": [
                        "1"
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

````

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 2018-02-15
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多