Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

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

I'm working on aggregations in NEST, so far everything has worked well, but now when I try to access nested fields through .children the result is null, however the debugger is showing the data correctly.

DebuggerInfo

If I post this query through postman I get the following results:

{
    "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": []
            }
        }
    }
}

Is there a way to get inside the child "CollarSize" ? I've tried different combinations with .nested, .children, .terms, .filter however none of these seems to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get "CollarSize" terms and "banana" terms for each with

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}");
    }
}

which prints

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

Here's a full example, using InMemoryConnection to stub the response

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}");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...