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

时间:2020-10-06 02:44:47

标签: go 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
        } 

是否有任何查询或方法来获取弹性搜索中的输出?

  1. 基于位置

    位置-联合国 高-2 中-3

    位置-英国 高-2 中-2

  2. 基于应用

    应用程序-App1 高-1 中-1

    应用程序-App2 高-3 中-4

  3. 或基于主机名

    主机名-主机名1 高-1 中-1

    主机名-主机名2 高-1 中-2

    主机名-主机名3 高-2 中-2

同样适用于业务部门。像业务单位,主机名,应用程序,基于位置的动态传递的字段名称一样,要获取该字段名称的高中值,就像上面的输出一样。

2 个答案:

答案 0 :(得分:1)

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

索引映射:

{
  "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
          }
        }
      ]
    }

答案 1 :(得分:0)

我们可以使用此查询获取例外结果

  {
          "query": {
            "bool": {
              "filter": [
                {
                  "bool": {
                    "should": [
                      {
                        "range": {
                          "medium": {
                            "gt": 0
                          }
                        }
                      },
                      {
                        "range": {
                          "high": {
                            "gt": 0
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "fieldnames": {
              "terms": {
                "field": "hostname.keyword"
              },
              "aggs": {
                "medium": {
                  "sum": {
                    "field": "medium"
                  }
                },
                "high": {
                  "sum": {
                    "field": "high"
                  }
                }
              }
            }
          },
          "size": 0
        }

搜索结果如下

"aggregations": {
        "fieldnames": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "ALL Unit",
                    "doc_count": 1,
                    "high": {
                        "value": 0.0
                    },
                    "medium": {
                        "value": 7.0
                    }
                },
                {
                    "key": "Latest Unit",
                    "doc_count": 1,
                    "high": {
                        "value": 0.0
                    },
                    "medium": {
                        "value": 5.0
                    }
                },
                {
                    "key": "NO Unit",
                    "doc_count": 1,
                    "high": {
                        "value": 1.0
                    },
                    "medium": {
                        "value": 1.0
                    }
                }
            ]
        }
    }

如果我们需要位置和应用程序的结果,只需更改 位置

"aggs": {
                "fieldnames": {
                  "terms": {
                    "field": "businesshierarchy.Application.keyword"
                  }

用于申请

"aggs": {
                    "fieldnames": {
                      "terms": {
                        "field": "businesshierarchy.Location.keyword"
                      }

如果映射是这样的

{
  "mappings": {
    "properties": {
      "hostname": {
        "type": "keyword"
      },
      "businesshierarchy": {
        "properties": {
          "Location": {
            "type": "keyword"
          },
          "Application": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

无需将.keyword添加到

"terms": {
             "field": "businesshierarchy.Location"
           }
相关问题