繁体   English   中英

ElasticSearch:从嵌套的聚合查询中访问外部文档字段

[英]ElasticSearch: Access outer document fields from within an nested aggregated query

我有以下映射:

{
    "dynamic": "strict",
    "properties": {
        "id": {
            "type": "string"
        },
        "title": {
            "type": "string"
        },
        "things": {
            "type": "nested",
            "properties": {
                "id": {
                    "type": "long"
                },
                "something": {
                    "type": "long"
                }
            }
        }
    }
}

我将文档插入如下(Python脚本):

body = {"id": 1, "title": "one", "things": [{"id": 1000, "something": 33}, {"id": 1001, "something": 34}, ]}
es.create(index_name, doc_type=doc_type, body=body, id=1)

body = {"id": 2, "title": "two", "things": [{"id": 1000, "something": 43}, {"id": 1001, "something": 44}, ]}
es.create(index_name, doc_type=doc_type, body=body, id=2)

body = {"id": 3, "title": "three", "things": [{"id": 1000, "something": 53}, {"id": 1001, "something": 54}, ]}
es.create(index_name, doc_type=doc_type, body=body, id=3)

我运行以下聚合查询:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "things": {
      "aggs": {
        "num_articles": {
          "terms": {
            "field": "things.id",
            "size": 0
          },
          "aggs": {
            "articles": {
              "top_hits": {
                "size": 50
              }
            }
          }
        }
      },
      "nested": {
        "path": "things"
      }
    }
  },
  "size": 0
}

(因此,我希望计数每个“事物”出现的次数,并针对每个事物列出出现每个事物的文章列表)

查询产生:

"key": 1000,
"doc_count": 3,
"articles": {
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [{
            "_index": "test",
            "_type": "article",
            "_id": "2",
            "_nested": {
                "field": "things",
                "offset": 0
            },
            "_score": 1,
            "_source": {
                "id": 1000,
                "something": 43
            }
        }, {
            "_index": "test",
            "_type": "article",
            "_id": "1",
            "_nested": {
                "field": "things",
                "offset": 0
            },
            "_score": 1,
            "_source": {
                "id": 1000,
                "something": 33
            }

.... (等等)

我想要的是让每个匹配项列出“外部”或顶级文档中的所有字段,例如ID和标题。

这实际上可能吗...如果是这样的话???

我不确定这是否是您要寻找的东西,但让我们尝试一下:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "nested_things": {
      "nested": {
        "path": "things"
      },
      "aggs": {
        "num_articles": {
          "terms": {
            "field": "things.id",
            "size": 0
          },
          "aggs": {
            "articles": {
              "top_hits": {
                "size": 50
              }
            },
            "reverse_things": {
              "reverse_nested": {},
              "aggs": {
                "title": {
                  "terms": {
                    "field": "title",
                    "size": 0
                  }
                },
                "id": {
                  "terms": {
                    "field": "id",
                    "size": 0
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

这将产生如下内容:

          "buckets": [
               {
                  "key": 1000,
                  "doc_count": 3,
                  "reverse_things": {
                     "doc_count": 3,
                     "id": {
                        "buckets": [
                           {
                              "key": "1",
                              "doc_count": 1
                           },
                           {
                              "key": "2",
                              "doc_count": 1
                           },
                           {
                              "key": "3",
                              "doc_count": 1
                           }
                        ]
                     },
                     "title": {
                        ...
                     }
                  },
                  "articles": {
                     "hits": {
                        "total": 3,
                        "max_score": 1,
                        "hits": [
                           {
                              "_index": "test",
                              "_type": "article",
                              "_id": "AVPOgQQjgDGxUAMojyuY",
                              "_nested": {
                                 "field": "things",
                                 "offset": 0
                              },
                              "_score": 1,
                              "_source": {
                                 "id": 1000,
                                 "something": 53
                              }
                           },
                           ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM