go - 如何在Elasticsearch中基于输入字段获取字段的总和值(输入字段和总和输出字段不同)

标签 go elasticsearch

这是存在于 Elasticsearch 中的文档,并且希望输出基于字段的字段,在该字段中它返回上限值和中值的总和且大于零,上限值和中值的值必须大于> 0

         {
            "host_id": 1,
            "hostname": "Hostname1",
            "businesshierarchy": {
                "businessunit": "NON Unit",
                "Location":"Un",
                "Application":"App1"
            },
            "updatedts": 1601894092,
            "critical": 0,
            "high": 1,
            "medium": 1,
            "low": 0
        },
        {
            "host_id": 2,
            "hostname": "Hostname2",
            "businesshierarchy": {
                "businessunit": "One Unit",
                "Location":"Un",
                "Application":"App2"
            },
            "updatedts": 1601894092,
            "critical": 0,
            "high": 1,
            "medium": 2,
            "low": 0
        },
        {
            "host_id": 3,
            "hostname": "Hostname3",
            "businesshierarchy": {
                "businessunit": "NON Unit",
                "Location":"Uk",
                "Application":"App2"
            },
            "updatedts": 1601894092,
            "critical": 0,
            "high": 2,
            "medium": 2,
            "low": 0
        } 
是否有任何查询或方法可以像 Elasticsearch 那样获取输出?
  • 基于位置
    位置-联合国
    高-2
    中-3
    位置-英国
    高-2
    中-2
  • 基于应用程序
    应用程序-App1
    高-1
    中-1
    应用程序-App2
    高-3
    中-4
  • 或基于主机名
    主机名-主机名1
    高-1
    中-1
    主机名-主机名2
    高-1
    中-2
    主机名-主机名3
    高-2
    中-2

  • 对于业务单位也是如此。动态地传递的字段名称(如业务单位,主机名,应用程序,基于位置的名称)基于它想要获取计数高和中值,如上述输出。

    最佳答案

    添加带有索引映射,索引数据(与问题中给出的相同),搜索查询和搜索结果的工作示例
    索引映射:

    {
      "mappings": {
        "properties": {
          "hostname": {
            "type": "keyword"
          },
          "businesshierarchy": {
            "properties": {
              "Location": {
                "type": "keyword"
              },
              "Application": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    
    搜索查询:
    {
      "size": 0,
      "aggs": {
        "user": {
          "terms": {
            "field": "businesshierarchy.Location"
          },
          "aggs": {
            "top_user_hits": {
              "top_hits": {
                "_source": {
                  "includes": [
                    "high",
                    "medium"
                  ]
                }
              }
            },
            "high_sum": {
              "sum": {
                "field": "high"
              }
            },
            "medium_sum": {
              "sum": {
                "field": "medium"
              }
            }
          }
        }
      }
    }
    
    搜索结果:
    基于位置
    "aggregations": {
        "user": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "Un",
              "doc_count": 2,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 2,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "1",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 1
                      }
                    },
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 2
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 2.0       <-- note this
              },
              "medium_sum": {
                "value": 3.0
              }
            },
            {
              "key": "Uk",
              "doc_count": 1,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "3",
                      "_score": 1.0,
                      "_source": {
                        "high": 2,
                        "medium": 2
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 2.0                       <-- note this
              },
              "medium_sum": {
                "value": 2.0
              }
            }
          ]
        }
    
    对于基于应用程序的查询,请替换术语聚合,如下所示:
    "aggs": {
            "user": {
              "terms": {
                "field": "businesshierarchy.Application"
              },
    
    以下搜索结果将在那里:
     "aggregations": {
        "user": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "App2",
              "doc_count": 2,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 2,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "3",
                      "_score": 1.0,
                      "_source": {
                        "high": 2,
                        "medium": 2
                      }
                    },
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 2
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 3.0
              },
              "medium_sum": {
                "value": 4.0
              }
            },
            {
              "key": "App1",
              "doc_count": 1,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "1",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 1
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 1.0
              },
              "medium_sum": {
                "value": 1.0
              }
            }
          ]
        }
    
    对于基于主机名的查询,请替换术语聚合,如下所示:
    "aggs": {
        "user": {
          "terms": {
            "field": "hostname"
          },
    
    搜索结果将是:
    "aggregations": {
        "user": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "Hostname1",
              "doc_count": 1,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "1",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 1
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 1.0
              },
              "medium_sum": {
                "value": 1.0
              }
            },
            {
              "key": "Hostname2",
              "doc_count": 1,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 1.0,
                      "_source": {
                        "high": 1,
                        "medium": 2
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 1.0
              },
              "medium_sum": {
                "value": 2.0
              }
            },
            {
              "key": "Hostname3",
              "doc_count": 1,
              "top_user_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "stof_64218649",
                      "_type": "_doc",
                      "_id": "3",
                      "_score": 1.0,
                      "_source": {
                        "high": 2,
                        "medium": 2
                      }
                    }
                  ]
                }
              },
              "high_sum": {
                "value": 2.0
              },
              "medium_sum": {
                "value": 2.0
              }
            }
          ]
        }
    

    关于go - 如何在Elasticsearch中基于输入字段获取字段的总和值(输入字段和总和输出字段不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64218649/

    相关文章:

    memory-management - 1.5+ 中的 Golang 垃圾收集器改进是否会影响释放的内存量?

    go - client.Do错误获取状态码

    json - Logstash 删除类型并保留 _type

    elasticsearch - Elasticsearch:在查询包含 '#'之类的特殊字符的情况下进行精确搜索

    elasticsearch - 未能将映射放在索引上导致elasticsearch崩溃[5.4.1]

    hadoop - 什么是 ElasticSearch-Hadoop (es-hadoop) 及其相对于 HBase 的实时 Web 应用程序优势?

    google-app-engine - 将结构 slice 转换为空接口(interface) slice

    syntax - Golang 的多返回重载是 map 类型独有的吗?

    go - 如何从 IP 获取系统接口(interface)名称

    java - Elasticsearch 更新索引文档