【问题标题】:filter and return only specific fields in elasticsearch过滤并仅返回弹性搜索中的特定字段
【发布时间】:2021-05-18 15:01:55
【问题描述】:

我正在尝试创建一个可以从 Elasticsearch 中提取特定指标的请求,这样我就可以更快地管理数据。 我通过以下请求摆脱了元数据和不必要的数据:

get localhost:9200/metricbeat-7.12.0/_search?size=2000&pretty&filter_path=hits.hits._source
{
    "_source": ["@timestamp", "labels","prometheus"]
}

我得到了更接近我想要的东西。现在我想要一个额外的过滤器,我只得到指标“prometheus.metrics.windows_cpu_time_total”而不是其他指标。

   {
    "hits": {
        "hits": [
            {
            {
                "_source": {
                    "@timestamp": "2021-04-29T15:35:57.518Z",
                    "prometheus": {
                        "metrics": {
                            "windows_service_status": 0
                        },
                        "labels": {
                            "instance": "localhost:9182",
                            "name": "timebrokersvc",
                            "job": "prometheus",
                            "status": "lost comm"
                        }
                    }
                }
            },
            {
                "_source": {
                    "@timestamp": "2021-04-29T15:35:57.518Z",
                    "prometheus": {
                        "metrics": {
                            "windows_cpu_time_total": 29480.625
                        },
                        "labels": {
                            "mode": "idle",
                            "core": "0,0",
                            "instance": "localhost:9182",
                            "job": "prometheus"
                        }
                    }
                }
            }}]}}

我尝试了一个字段搜索,但似乎效果不佳。有人可以指出我的查询出了什么问题吗?

{
  "query": {
    "match_all": {}
  },
    "fields": [
    "prometheus.metrics", 
    {
        "field": "windows_cpu_time_total"
    }]
  }

提前谢谢你

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    @ESCoder 我不能再发表评论了。我会在这里回答你的问题。以下是包含 Prometheus 属性的映射概述。

    "prometheus": {
                    "properties": {
                        "*": {
                            "properties": {
                                "counter": {
                                    "type": "object"
                                },
                                "histogram": {
                                    "type": "object"
                                },
                                "rate": {
                                    "type": "object"
                                },
                                "value": {
                                    "type": "object"
                                }
                            }
                        "metrics": {
                                "properties": {
                            "*": {
                                "type": "object"
                            },
                            "windows_cpu_time_total": {
                                "type": "double"
                            },}}
    

    【讨论】:

      【解决方案2】:

      在elasticsearch 7.10 和7.11 版本中,fields 功能处于测试版,如官方documentataion 所述

      但在 elasticsearch 7.12 版中,fields 选项工作正常

      添加一个包含索引数据、搜索查询和搜索结果的工作示例

      索引数据:

      {
        "@timestamp": "2021-04-29T15:35:57.518Z",
        "prometheus": {
          "metrics": {
            "windows_cpu_time_total": 29480.625
          },
          "labels": {
            "mode": "idle",
            "core": "0,0",
            "instance": "localhost:9182",
            "job": "prometheus"
          }
        }
      }
      

      搜索查询:

      POST _search?size=2000&pretty&filter_path=hits.hits
      {
        "query": {
         "match_all": {}
        },
        "fields": [
          "prometheus.metrics.windows_cpu_time_total"
        ],
        "_source": false
      }
      

      搜索结果:

      {
        "hits": {
          "hits": [
            {
              "_index": "67588900",
              "_type": "_doc",
              "_id": "1",
              "_score": 1.0,
              "fields": {
                "prometheus.metrics.windows_cpu_time_total": [
                  29480.625
                ]
              }
            }
          ]
        }
      }
      

      这里是索引映射的概述

      "prometheus": {
                      "properties": {
                          "*": {
                              "properties": {
                                  "counter": {
                                      "type": "object"
                                  },
                                  "histogram": {
                                      "type": "object"
                                  },
                                  "rate": {
                                      "type": "object"
                                  },
                                  "value": {
                                      "type": "object"
                                  }
                              }
                          "metrics": {
                                  "properties": {
                              "*": {
                                  "type": "object"
                              },
                              "windows_cpu_time_total": {
                                  "type": "double"
                              },}}
      

      【讨论】:

      • @Sofiane 请仔细阅读答案,如果这能解决您的问题,请告诉我?您使用的是哪个版本的弹性搜索?
      • 嘿@ESCODER 我使用elasticsearch 7.12。我试过你上面写的,但我收到一个空结果“{}”。
      • @Sofiane 我还在 7.12 中测试了上述查询。你能分享你的索引映射吗?
      • @Sofiane 我需要查看prometheus.metrics.windows_cpu_time_total的映射
      • 我有以下映射。"prometheus": { "properties": { "metrics": { "properties": { "*": { "type": "object" }, "windows_cpu_time_total ": { "type": "double" }, }
      猜你喜欢
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多