汇总Elasticsearch嵌套数据类型

时间:2019-07-12 12:51:39

标签: elasticsearch

假设我们有许多这样的文件:

{
    "article" : [
      {
        "id" : 1
        "title" : "An article title",
        "author" : [
            {
                "id": "AUTHOR1"
                "firstname" : "John",
                "surname": "Doe"
            },
            {
                "id": "AUTHOR2"
                "firstname" : "Harry",
                "surname" : "Schmoe"
            }
        ]
      }
    },
    {
        "id" : 2
        "title" : "Another article title",
        "author" : [
            {
                "id": "AUTHOR2"
                "firstname" : "Harry",
                "surname" : "Schmoe"
            }
        ]
      }
}

author被映射为嵌套嵌套。我可以使用以下查询汇总作者:

{
    "aggs": {
        "unique_authors": {
            "nested": {
                "path": "authors"
            },
            "aggs": {
                "author_id": {
                    "terms": {
                        "field": "authors.id"
                    }
                }
            }
        }
    },
    "size": 0,
    "query": {
        "match_all": {}
    }
}

但这只会返回带有作者ID的存储桶。我想避免不得不在数据库中查询其他作者的详细信息。添加额外的术语汇总将在单独的存储桶中返回数据,但这将无法将名称链接到作者ID。我想要的是这样的:

{
    ...
    "aggregations" : {
        "unique_authors" : {
            "doc_count_error_upper_bound": 0, 
            "sum_other_doc_count": 0, 
            "buckets" : [ 
                {
                    "key" : "AUTHOR1",
                    "firstname" : "John",
                    "surname": "Doe"
                    "doc_count" : 1
                },
                {
                    "key" : "AUTHOR2",
                    "firstname" : "Harry",
                    "surname" : "Schmoe"
                    "doc_count" : 1
                }
            ]
        }
    }
}

如何在同一存储桶中检索相应的名字和姓氏,以便将它们链接到作者ID?

0 个答案:

没有答案