Elasticsearch filter aggregations on minimal doc c

2019-04-06 18:29发布

I am really new to elasticsearch world.

Let's say I have a nested aggregation on two fields : field1 and field2 :

{
    ...
    aggs: {
        field1: {
            terms: {
                field: 'field1'
            },
            aggs: {
                field2: {
                    terms: {
                        field: 'field2'
                    }
                }
            }
        }
    }
}

This piece of code works perfectly and gives me something like this :

aggregations: {
    field1: {
        buckets: [{
            key: "foo",
            doc_count: 123456,
            field2: {
                buckets: [{
                    key: "bar",
                    doc_count: 34323
                },{
                    key: "baz",
                    doc_count: 10
                },{
                    key: "foobar",
                    doc_count: 36785
                },
                ...
                ]
        },{
            key: "fooOO",
            doc_count: 423424,
            field2: {
                buckets: [{
                    key: "bar",
                    doc_count: 35
                },{
                    key: "baz",
                    doc_count: 2435453
                },
                ...
                ]
        },
        ...
        ]
    }
}

Now, my need is to exclude all aggregation results where doc_count is less than 1000 for instance and get this instead :

aggregations: {
    field1: {
        buckets: [{
            key: "foo",
            doc_count: 123456,
            field2: {
                buckets: [{
                    key: "bar",
                    doc_count: 34323
                },{
                    key: "foobar",
                    doc_count: 36785
                },
                ...
                ]
        },{
            key: "fooOO",
            doc_count: 423424,
            field2: {
                buckets: [{
                    key: "baz",
                    doc_count: 2435453
                },
                ...
                ]
        },
        ...
        ]
    }
}

Is it possible to set this need in the query body ? or do I have to perform the filter in the caller layout (in javascript in my case)?

Thanks in advance

1条回答
做自己的国王
2楼-- · 2019-04-06 19:31

Next time, M'sieur Toph' : RTFM !!!

I feel really dumb: I found the anwser in the manual, 30 seconds after asking. I don't remove my question because, it can help, who knows...

Here is the anwser :

You can specify the min_doc_count property in the terms aggregation.

It gives you :

{
    ...
    aggs: {
        field1: {
            terms: {
                field: 'field1',
                min_doc_count: 1000
            },
            aggs: {
                field2: {
                    terms: {
                        field: 'field2',
                        min_doc_count: 1000
                    }
                }
            }
        }
    }
}

You also can specify a specific minimal count for each level of your aggregation.

What else ? :)

查看更多
登录 后发表回答