【问题标题】:Match document if it contains multiple nested documents in elasticsearch如果在elasticsearch中包含多个嵌套文档,则匹配文档
【发布时间】:2016-12-01 09:25:49
【问题描述】:

我有一个包含嵌套文档数组的文档。如果文档包含所有指定的嵌套文档,我需要返回匹配项。

这里是映射的相关部分:

"element": {
  "dynamic": "false",
  "properties": {
    "tenantId": {
      "type": "string",
      "index": "not_analyzed"
    },
    "fqn": {
      "type": "string",
      "index": "not_analyzed"
    },
    "id": {
      "type": "string",
      "index": "not_analyzed"
    },
    "name": {
      "type": "string",
      "index": "not_analyzed"
    },
    "type": {
      "type": "string",
      "index": "not_analyzed"
    },
    "location": {
      "type": "string",
      "index": "not_analyzed"
    },
    "tags": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "string",
          "index": "not_analyzed"
        },
        "dataSourceId": {
          "type": "long",
          "index": "not_analyzed"
        },
        "name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "value": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

目标是能够返回包含所有标签列表的元素(尽管允许该元素包含超出搜索要求的其他标签)。

这是我目前所拥有的:

{
   "query": {
     "bool": {
       "filter": {
          "nested": {
             "path": "tags",
             "query": {
                "bool": {
                   "must": [
                      {
                        "bool": {
                          "must":{
                             "term": { "tags.name": "name1" },
                             "term": { "tags.value": "value1" }
                          }
                        }
                      },
                      {
                        "bool": {
                          "must":{
                             "term": { "tags.name": "name2" },
                             "term": { "tags.value": "value2" }
                          }
                        }
                      }
                   ]
                }
             }
          }
       }
    }
  }
}

这种方法的问题是它返回 0 个具有多个标签值的命中(它适用于单个值)。我相信这是因为查询要求标签具有多个名称和值才能匹配,这显然不可能发生。有谁知道如何查询包含所有标签列表的元素?

编辑:这是使用弹性搜索 5.0

【问题讨论】:

标签: elasticsearch


【解决方案1】:

我们想通了。答案是创建两个嵌套查询,而不是为同一个嵌套查询创建两个子句。

{
 "query":{
   "bool":{
     "must":[{
        "nested":{
           "path":"tags",
           "query":{
              "bool":{
                 "must":[
                    {"term":{"tags.name":"name1"}},
                    {"term":{"tags.value":"value1"}}
                 ]
              }
           }
        }
     },
     {
        "nested":{
           "path":"tags",
           "query":{
              "bool":{
                 "must":[
                    {"term":{"tags.name":"name2"}},
                    {"term":{"tags.value":"value2"}}
                 ]
              }
           }
        }
     }]
   }
 }
}

【讨论】:

  • 遇到了这个问题,你的回答让我很头疼,谢谢!
  • 有更好的方法吗?如果必须对嵌套属性应用 5-6 个条件,查询将变得巨大!
猜你喜欢
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多