嵌套聚合结果为空,但调试器中有数据

Nest aggregation results are null however there are data in the debugger

我正在 NEST 中处理聚合,到目前为止一切正常,但现在当我尝试通过 .children 访问嵌套字段时,结果为空,但调试器正确显示数据。

如果我通过 postman post 这个查询,我会得到以下结果:

{
    "took": 50,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "filter#CollarSize": {
            "meta": {},
            "doc_count": 9,
            "nested#VariantsProperties": {
                "doc_count": 53,
                "sterms#CollarSize": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "CollarSize",
                            "doc_count": 39,
                            "sterms#banana": {
                                "doc_count_error_upper_bound": 0,
                                "sum_other_doc_count": 0,
                                "buckets": [
                                    {
                                        "key": "15",
                                        "doc_count": 7
                                    },
                                    {
                                        "key": "16",
                                        "doc_count": 7
                                    },
                                    {
                                        "key": "17",
                                        "doc_count": 6
                                    },
                                    {
                                        "key": "18",
                                        "doc_count": 6
                                    },
                                    {
                                        "key": "LAR",
                                        "doc_count": 2
                                    },
                                    {
                                        "key": "MED",
                                        "doc_count": 2
                                    },
                                    {
                                        "key": "SML",
                                        "doc_count": 2
                                    },
                                    {
                                        "key": "X.L",
                                        "doc_count": 2
                                    },
                                    {
                                        "key": "XXL",
                                        "doc_count": 2
                                    },
                                    {
                                        "key": "15.5",
                                        "doc_count": 1
                                    },
                                    {
                                        "key": "16.5",
                                        "doc_count": 1
                                    },
                                    {
                                        "key": "XXXL",
                                        "doc_count": 1
                                    }
                                ]
                            }
                        },
                        {
                            "key": "Colour",
                            "doc_count": 14,
                            "sterms#banana": {
                                "doc_count_error_upper_bound": 0,
                                "sum_other_doc_count": 0,
                                "buckets": [
                                    {
                                        "key": "Blue",
                                        "doc_count": 7
                                    },
                                    {
                                        "key": "White",
                                        "doc_count": 7
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "sterms#CollarSize": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": []
            }
        }
    }
}

有没有办法进入 child“CollarSize”?我尝试了 .nested, .children, .terms, .filter 的不同组合,但是其中 none 似乎有效。

您可以获得 "CollarSize" 个条款和 "banana" 个条款

var response = client.Search<object>(/** your query here **/);

var collarSizeSignificantTermsAgg = response.Aggregations.Filter("CollarSize").Nested("VariantsProperties").Terms("CollarSize");

foreach(var bucket in collarSizeSignificantTermsAgg.Buckets)
{
    Console.WriteLine(bucket.Key);
    
    var bananaSigTerms = bucket.Terms("banana");
    
    foreach(var subBucket in bananaSigTerms.Buckets)
    {
        Console.WriteLine($"key: {subBucket.Key}, doc_count: {subBucket.DocCount}");
    }
}

打印

CollarSize
key: 15, doc_count: 7
key: 16, doc_count: 7
key: 17, doc_count: 6
key: 18, doc_count: 6
key: LAR, doc_count: 2
key: MED, doc_count: 2
key: SML, doc_count: 2
key: X.L, doc_count: 2
key: XXL, doc_count: 2
key: 15.5, doc_count: 1
key: 16.5, doc_count: 1
key: XXXL, doc_count: 1
Colour
key: Blue, doc_count: 7
key: White, doc_count: 7

这是一个完整的例子,使用 InMemoryConnection 存根响应

private static void Main()
{
    var defaultIndex = "my_index";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var json = @"{
    ""took"": 50,

    ""timed_out"": false,
    ""_shards"": {
        ""total"": 1,
        ""successful"": 1,
        ""skipped"": 0,
        ""failed"": 0

    },
    ""hits"": {
        ""total"": {
            ""value"": 9,
            ""relation"": ""eq""

        },
        ""max_score"": null,
        ""hits"": []

    },
    ""aggregations"": {
        ""filter#CollarSize"": {
            ""meta"": { },
            ""doc_count"": 9,
            ""nested#VariantsProperties"": {
                ""doc_count"": 53,
                ""sterms#CollarSize"": {
                    ""doc_count_error_upper_bound"": 0,
                    ""sum_other_doc_count"": 0,
                    ""buckets"": [

                        {
                        ""key"": ""CollarSize"",
                            ""doc_count"": 39,
                            ""sterms#banana"": {
                            ""doc_count_error_upper_bound"": 0,
                                ""sum_other_doc_count"": 0,
                                ""buckets"": [

                                    {
                                ""key"": ""15"",
                                        ""doc_count"": 7

                                    },
                                    {
                                ""key"": ""16"",
                                        ""doc_count"": 7

                                    },
                                    {
                                ""key"": ""17"",
                                        ""doc_count"": 6

                                    },
                                    {
                                ""key"": ""18"",
                                        ""doc_count"": 6

                                    },
                                    {
                                ""key"": ""LAR"",
                                        ""doc_count"": 2

                                    },
                                    {
                                ""key"": ""MED"",
                                        ""doc_count"": 2

                                    },
                                    {
                                ""key"": ""SML"",
                                        ""doc_count"": 2

                                    },
                                    {
                                ""key"": ""X.L"",
                                        ""doc_count"": 2

                                    },
                                    {
                                ""key"": ""XXL"",
                                        ""doc_count"": 2

                                    },
                                    {
                                ""key"": ""15.5"",
                                        ""doc_count"": 1

                                    },
                                    {
                                ""key"": ""16.5"",
                                        ""doc_count"": 1

                                    },
                                    {
                                ""key"": ""XXXL"",
                                        ""doc_count"": 1

                                    }
                                ]
                            }
                    },
                        {
                        ""key"": ""Colour"",
                            ""doc_count"": 14,
                            ""sterms#banana"": {
                            ""doc_count_error_upper_bound"": 0,
                                ""sum_other_doc_count"": 0,
                                ""buckets"": [

                                    {
                                ""key"": ""Blue"",
                                        ""doc_count"": 7

                                    },
                                    {
                                ""key"": ""White"",
                                        ""doc_count"": 7

                                    }
                                ]
                            }
                    }
                    ]
                }
            },
            ""sterms#CollarSize"": {
                ""doc_count_error_upper_bound"": 0,
                ""sum_other_doc_count"": 0,
                ""buckets"": []

            }
        }
    }
}
";

    var settings = new ConnectionSettings(pool, new InMemoryConnection(Encoding.UTF8.GetBytes(json)))
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);
    var response = client.Search<object>(s => s);

    var collarSizeSignificantTermsAgg = response.Aggregations.Filter("CollarSize").Nested("VariantsProperties").Terms("CollarSize");

    foreach (var bucket in collarSizeSignificantTermsAgg.Buckets)
    {
        Console.WriteLine(bucket.Key);

        var bananaSigTerms = bucket.Terms("banana");

        foreach (var subBucket in bananaSigTerms.Buckets)
        {
            Console.WriteLine($"key: {subBucket.Key}, doc_count: {subBucket.DocCount}");
        }
    }
}