我有如下es设置:
PUT /test
{
"mappings": {
"doc": {
"properties": {
"status": {
"type": "keyword"
},
"counting": {
"type": "integer"
},
"join": {
"type": "join",
"relations": {
"vsim": ["pool", "package"]
}
},
"poolId": {
"type": "keyword"
},
"packageId": {
"type": "keyword"
},
"countries": {
"type": "keyword"
},
"vId": {
"type": "keyword"
}
}
}
}}
然后添加数据:
// add vsim
PUT /test/doc/doc1
{"counting":6, "join": {"name": "vsim"}, "content": "1", "status": "disabled"}
PUT /test/doc/doc2
{"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "disabled"}
PUT /test/doc/doc3
{"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "enabled"}
// add package
PUT /test/doc/ner2?routing=doc2
{"join": {"name": "package", "parent": "doc2"}, "countries":["CN", "UK"]}
PUT test/doc/ner12?routing=doc1
{"join": {"name": "package", "parent": "doc1"}, "countries":["CN", "US"]}
PUT /test/doc/ner11?routing=doc1
{"join":{"name": "package", "parent": "doc1"}, "countries":["US", "KR"]}
PUT /test/doc/ner13?routing=doc3
{"join":{"name": "package", "parent": "doc3"}, "countries":["UK", "AU"]}
// add pool
PUT /test/doc/ner21?routing=doc1
{"join": {"name": "pool", "parent": "doc1"}, "poolId": "MER"}
PUT /test/doc/ner22?routing=doc2
{"join": {"name": "pool", "parent": "doc2"}, "poolId": "MER"}
PUT /test/doc/ner23?routing=doc2
{"join": {"name": "pool", "parent": "doc2"}, "poolId": "NER"}
然后我想通过status(vsim)、poolid(pool)和countries(package)对计数组进行计数,预期结果如下:
残疾人人数:3人
美国残疾人:3
已启用mr cn:1。。。等等。我是elasticsearch的新玩家,我学习了以下文档
https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html 以及https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html
但是仍然不知道如何实现这个聚合查询,请给我一些建议,谢谢!
1条答案
按热度按时间cedebl8k1#
如果我按照你的文档结构-你有相同级别的类型池和包(它们是同级的)-我就不能完全达到你的预期结果。我也非常怀疑那些类型的人是否可能是兄弟姐妹。
但是,仍然可以对doc(status)中的每个字段进行切片,然后使用如下查询分别按poolid和countries进行切片:
elasticsearch的响应如下(为了可读性,我省略了json的一些部分):