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

时间:2016-03-31 14:10:12

标签: elasticsearch pyelasticsearch

我有以下映射:

{
    "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和标题。

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

1 个答案:

答案 0 :(得分:0)

我不确定这是不是你想要的,但让我们试一试:

{
  "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
                              }
                           },
                           ...