Elasticsearch脚本查询主机名

发布于 2025-01-25 19:57:47 字数 824 浏览 4 评论 0原文

我希望创建一个脚本,该脚本将查询多个主机名,如果不在索引中,并提供了未找到的结果,并提供了在服务器上找到文档的主机和计数。到目前为止,我已经有效了,但是我不确定如何使其查询多个服务器并提供正确的结果。任何帮助将不胜感激。

GET /index1*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-7d",
              "lt": "now"
            }
          }
        },
        {
          "term": {
            "host.name": "server1"
          }
        }
      ]
    }
  },
  "aggregations": {
    "hosts": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "hostname": {
              "terms": {
                "field": "host.name"
              }
            }
          }
        ]
      }
    }
  },
  "size": 0

I am looking to create a script that will query multiple hostnames and provide a not found result if it is not in the index and provide the host and count of documents on the server if it is found. What I have so far seems to work, but I am unsure of how to make it query multiple servers and provide the correct result. Any help would be greatly appreciated.

GET /index1*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-7d",
              "lt": "now"
            }
          }
        },
        {
          "term": {
            "host.name": "server1"
          }
        }
      ]
    }
  },
  "aggregations": {
    "hosts": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "hostname": {
              "terms": {
                "field": "host.name"
              }
            }
          }
        ]
      }
    }
  },
  "size": 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2025-02-01 19:57:47

到目前为止的开端很棒!您可以简单地将术语查询到一个。另外,您需要利用缺少存储桶未找到结果的功能:

GET /index1*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-7d",
              "lt": "now"
            }
          }
        },
        {
          "terms": {
            "host.name": ["server1", "server2", "server3"]
          }
        }
      ]
    }
  },
  "aggregations": {
    "hosts": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "hostname": {
              "terms": {
                "field": "host.name",
                "missing_bucket": true,
                "missing_order": "last"
              }
            }
          }
        ]
      }
    }
  },
  "size": 0
}

在给定时间间隔期间具有文档的所有服务器都将有存储桶,其他所有服务器都将在“ null”存储桶中。

Great start so far! You can simply change the term query into a terms one. Also, you need to leverage the missing bucket feature for the not found result:

GET /index1*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-7d",
              "lt": "now"
            }
          }
        },
        {
          "terms": {
            "host.name": ["server1", "server2", "server3"]
          }
        }
      ]
    }
  },
  "aggregations": {
    "hosts": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "hostname": {
              "terms": {
                "field": "host.name",
                "missing_bucket": true,
                "missing_order": "last"
              }
            }
          }
        ]
      }
    }
  },
  "size": 0
}

All the servers which have documents during the given time interval will have buckets, all the others will be in the "null" bucket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文