【问题标题】:Elastic search bucket filtering弹性搜索桶过滤
【发布时间】:2019-01-31 16:00:48
【问题描述】:

我想从聚合存储桶中过滤数据。我写过这样的查询

{
    "size":0,
    "aggregations": {
        "latestNode": {
            "terms": {"field": "uri"},
            "aggregations": {
                "top_nodes": {
                    "top_hits": {
                        "sort": [
                            {
                                "timestamp": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                }
            }
        }
    }
}

在此之后我得到如下结果:

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 10,
        "successful": 10,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "latestNode": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "2FF70002AC01F743",
                    "doc_count": 2,
                    "top_nodes": {
                        "hits": {
                            "total": 2,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "VirtualDB",
                                    "_type": "doc",
                                    "_id": "E85FnWgBhR1fwKHvHfpI",
                                    "_score": null,
                                    "_source": {
                                        "uri": "2FF70002AC01F743",
                                        "status": "WARNING",
                                        "name": "Test_20k_11Test1",
                                        "timestamp": 1548664071116
                                    },
                                    "sort": [
                                        1548664071116
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "vDB1234",
                    "doc_count": 2,
                    "top_nodes": {
                        "hits": {
                            "total": 2,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "VirtualDB",
                                    "_type": "doc",
                                    "_id": "Es5EnWgBhR1fwKHvm_r5",
                                    "_score": null,
                                    "_source": {
                                        "uri": "vDB1234",
                                        "status": "OK",
                                        "name": "Test_20k_11Test",
                                        "timestamp": 15486640781116
                                    },
                                    "sort": [
                                        15486640781116
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "2FF70002AC01F744",
                    "doc_count": 1,
                    "top_nodes": {
                        "hits": {
                            "total": 1,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "VirtualDB",
                                    "_type": "doc",
                                    "_id": "Fc5GnWgBhR1fwKHvPvrm",
                                    "_score": null,
                                    "_source": {
                                        "uri": "2FF70002AC01F744",
                                        "status": "WARNING",
                                        "name": "Test_20k_11Test1",
                                        "timestamp": 1548664061116
                                    },
                                    "sort": [
                                        1548664061116
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "vDB1235",
                    "doc_count": 1,
                    "top_nodes": {
                        "hits": {
                            "total": 1,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "VirtualDB",
                                    "_type": "doc",
                                    "_id": "Ec5DnWgBhR1fwKHvb_oe",
                                    "_score": null,
                                    "_source": {
                                        "uri": "vDB1235",
                                        "status": "OK",
                                        "name": "Test_20k_11Test",
                                        "timestamp": 15486640751116
                                    },
                                    "sort": [
                                        15486640751116
                                    ]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

现在我想根据条件从存储桶中删除一些项目

如果 uri = vDB1234 并且 "status" = "OK","name" = "Test_20k_11Test" 然后删除

{
                                        "uri": "vDB1234",
                                        "status": "OK",
                                        "name": "Test_20k_11Test",
                                        "timestamp": 15486640781116
                                    }

在聚合存储桶中排名第二 提前感谢您的帮助

【问题讨论】:

  • 您找到解决方案了吗?

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

如果您想从整个结果集中删除它,您可以使用查询块。在这种情况下,首先会运行查询并对查询结果进行聚合。

{
"query": {
    "bool" : {
        "must": [
            // Your conditions here
        ]
    }
},
"size":0,
"aggregations": {
    "latestNode": {
        "terms": {"field": "uri"},
        "aggregations": {
            "top_nodes": {
                "top_hits": {
                    "sort": [
                        {
                            "timestamp": {
                                "order": "desc"
                            }
                        }
                    ],
                    "size" : 1
                }
            }
        }
    }
}

【讨论】:

  • 感谢您的快速回复,但我需要对聚合存储桶运行过滤器,只有我的要求是这样的
  • 您能详细解释一下具体的用例吗?也许它可以通过一些不同的方式来解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2015-11-20
相关资源
最近更新 更多