ElasticSearch中的不同值,在{“key”:“xyz”,“文档计数”:一个

rekjcdws  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(72)

我正在使用Oython(版本3.6)进行ElasticSearch(版本7.16)
我在ElasticSearch中有以下行:

{"owner": "john", "database": "postgres", "table": "sales_tab"},
{"owner": "hannah", "database": "mongodb", "table": "dept_tab"},
{"owner": "peter", "database": "mysql", "table": "new_tab"},
{"owner": "jim", "database": "postgres", "table": "cust_tab"},
{"owner": "lima", "database": "postgres", "table": "sales_tab"},
{"owner": "tory", "database": "oracle", "table": "store_tab"},
{"owner": "kane", "database": "mysql", "table": "trasit_tab"},
{"owner": "roma", "database": "mongodb", "table": "common_tab"},
{"owner": "ashley", "database": "mongodb", "table": "common_tab"},

用下面的查询:

{
    "size": 0,
    "aggs": {
        "table_grouped": {
          "terms": {
            "field": "table",
            "size": 100000
          }
        }
      }
}

我得到了不同的表值,如下所示:

{..., 'aggregations': {'table_grouped': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 
'buckets': [{'key': 'sales_tab', 'doc_count': 3}, {'key': 'dept_tab', 'doc_count': 1}, 
{'key': 'new_tab', 'doc_count': 1}, {'key': 'cust_tab', 'doc_count': 1}, 
{'key': 'store_tab', 'doc_count': 1}, {'key': 'trasit_tab', 'doc_count': 1}, 
{'key': 'common_tab', 'doc_count': 2}]}}}

但我真正想要的是:

{..., 'aggregations': {'table_grouped': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 
'buckets': [{'key': 'sales_tab', 'doc_count': 2, "database": "postgres"}, {'key': 'dept_tab', 
'doc_count': 1, "database": "mongodb"}, {'key': 'new_tab', 'doc_count': 1, 
"database": "mysql"}, {'key': 'cust_tab', 'doc_count': 1, "database": "postgres"}, 
{'key': 'store_tab', 'doc_count': 1, "database": "oracle"}, {'key': 'trasit_tab', 'doc_count': 1, "database": "mysql"}, 
{'key': 'common_tab', 'doc_count': 2, "database": "mongodb"}}]}}}

我想知道这个表来自哪个数据库,而不仅仅是{'key': 'sales_tab', 'doc_count': 2}这样的额外键:value ofdatabase{'key': 'sales_tab', 'doc_count': 2, "database": "postgres"} value in bucket result或任何其他解决方案,该解决方案将给出不同的表及其来源的数据库。
我如何实现它?

bis0qfac

bis0qfac1#

您可以使用子聚合来获取数据库名称,如下所示:

{
  "size": 0,
  "aggs": {
    "table_grouped": {
      "terms": {
        "field": "table",
        "size": 10
      },
      "aggs": {
        "database": {
          "terms": {
            "field": "database",
            "size": 10
          }
        }
      }
    }
  }
}

这将生成如下所示的响应:

"aggregations": {
    "table_grouped": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "common_tab",
          "doc_count": 2,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "mongodb",
                "doc_count": 2
              }
            ]
          }
        },
        {
          "key": "sales_tab",
          "doc_count": 2,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "postgres",
                "doc_count": 2
              }
            ]
          }
        },
        {
          "key": "cust_tab",
          "doc_count": 1,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "postgres",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "dept_tab",
          "doc_count": 1,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "mongodb",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "new_tab",
          "doc_count": 1,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "mysql",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "store_tab",
          "doc_count": 1,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "oracle",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "trasit_tab",
          "doc_count": 1,
          "database": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "mysql",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }

相关问题