【问题标题】:Is there any solution with elasticsearch parent-child join有没有弹性搜索父子加入的解决方案
【发布时间】:2019-01-02 23:25:26
【问题描述】:

我有一个如下的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"}

然后我想按状态(vsim)、poolId(池)和国家(包)来计算计数组,预期结果如下:

禁用-MER-CN:3

禁用-MER-US:3

启用-MR-CN: 1 ... 等等。 我是elasticsearch的新手,学习过文档之类的

https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html

但仍然不知道如何实现这个聚合查询,请给我一些建议,谢谢!

【问题讨论】:

    标签: elasticsearch parent-child aggregation


    【解决方案1】:

    如果我遵循您的文档结构-您的类型 poolpackage 在同一级别(它们是兄弟姐妹)-我无法完全实现你的预期结果。我也非常怀疑这些类型是否可能是兄弟姐妹。

    但是,仍然可以对文档中的每个字段进行切片(status),然后分别按 poolIdcountries 进行切片像这样的查询:

    {
      "aggs": {
        "status-aggs": {
          "terms": {
            "field": "status",
            "size": 10
          },
          "aggs": {
            "to-pool": {
              "children": {
                "type": "pool"
              },
              "aggs": {
                "top-poolid": {
                  "terms": {
                    "field": "poolId",
                    "size": 10
                  }
                }
              }
            },
            "to-package": {
              "children": {
                "type": "package"
              },
              "aggs": {
                "top-countries": {
                  "terms": {
                    "field": "countries",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
    

    来自 Elasticsearch 的响应是这样的(为了便于阅读,我省略了 json 的某些部分):

    {
      "status-aggs": {
        "buckets": [
          {
            "key": "disabled",
            "doc_count": 2,
            "to-pool": {
              "doc_count": 3,
              "top-poolid": {
                "buckets": [
                  {
                    "key": "MER",
                    "doc_count": 2
                  },
                  {
                    "key": "NER",
                    "doc_count": 1
                  }
                ]
              }
            },
            "to-package": {
              "doc_count": 3,
              "top-countries": {
                "buckets": [
                  {
                    "key": "CN",
                    "doc_count": 2
                  },
                  {
                    "key": "US",
                    "doc_count": 2
                  },
                  {
                    "key": "KR",
                    "doc_count": 1
                  },
                  {
                    "key": "UK",
                    "doc_count": 1
                  }
                ]
              }
            }
          },
          {
            "key": "enabled",
            "doc_count": 1,
            "to-pool": {
              "doc_count": 0,
              "top-poolid": {
                "buckets": []
              }
            },
            "to-package": {
              "doc_count": 1,
              "top-countries": {
                "buckets": [
                  {
                    "key": "AU",
                    "doc_count": 1
                  },
                  {
                    "key": "UK",
                    "doc_count": 1
                  }
                ]
              }
            }
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多