【问题标题】:How to get only one field from elasticsearch in the output?如何从输出中的elasticsearch中仅获取一个字段?
【发布时间】:2021-04-23 01:20:22
【问题描述】:
{
     "took": 5,
   "timed_out": false,
   "_shards": {
       "total": 1,
       "successful": 1,
       "skipped": 0,
       "failed": 0
   },
   "hits": {
       "total": {
           "value": 1999,
           "relation": "eq"
       },
       "max_score": 1.0,
       "hits": [
           {
               "_index": "logstash-2021.01.13-000001",
               "_type": "_doc",
               "_id": "lVef-3YBI8ZVMz0vOphU",
               "_score": 1.0,
               "_source": {
                   "host": {
                       "name": "AAD-W1PF14DMMK"
                   },
                   "@timestamp": "2021-01-13T12:01:19.794Z",
                   "log": {
                       "file": {
                           "path": "C:\\elk\\test.log"
                       },
                       "offset": 158
                   },
                   "type": "test",
                   "tags": [
                       "beats_input_codec_plain_applied"
                   ],
                   "ecs": {
                       "version": "1.6.0"
                   },
                   "agent": {
                       "hostname": "AAD-W1PF14DMMK",
                       "type": "filebeat",
                       "name": "AAD-W1PF14DMMK",
                       "id": "4aa46436-264c-40ba-a24a-17af072c8363",
                       "version": "7.10.1",
                       "ephemeral_id": "18c7451e-78a8-4806-b43f-5ebae812b533"
                   },
                   "@version": "1",
                   "message": "2015-10-18 18:01:48,963 INFO [main] org.apache.hadoop.mapreduce.v2.app.MRAppMaster: Executing with tokens:"
               }
           },
}

我想得到类似的输出

{  "message": "2015-10-18 18:01:48,963 INFO [main] org.apache.hadoop.mapreduce.v2.app.MRAppMaster: Executing with tokens:" }

我尝试过使用

GET localhost:9200/_search?filter_path=hits.hits._source 
 {
    "_source": {
        "includes": ["message"]
    },
    "query": {
        "multi_match" : {
        "query": "ERROR",
        "fields": [ "message"] 
        }
    }
}

它有效,但警告它默认会被弃用。 警告:

#!弃用:此请求访问系统索引:[.apm-agent-configuration, .apm-custom-link, .async-search, .kibana_1, .kibana_task_manager_1],但在未来的主要版本中,将阻止直接访问系统索引默认情况下

什么是替代解决方案??

【问题讨论】:

    标签: elasticsearch logging kibana elastic-stack


    【解决方案1】:

    您的查询是正确的。该错误与您执行_search 请求的方式有关。

    而不是访问

    GET localhost:9200/_search...
    { ... }
    

    使用具体的索引名称——在你的情况下:

    GET localhost:9200/logstash-2021.01.13-000001/_search...
    { ... }
    

    提示:也支持通配符索引名称:

    GET localhost:9200/logstash-2021*/_search...
    { ... }
    

    多索引查询也是如此:

    GET localhost:9200/logstash-2021*,logstash-2020*/_search...
    { ... }
    

    【讨论】:

    • 嗨,但在这种情况下,我也会得到其他领域。请检查一次我期望的输出。我只想有 json 格式的“消息”而不是命中、标签、分数、代理等
    • 保留查询负载 -- 我只是在谈论您正在访问的 URL。
    • 哦,好吧,但这就是问题所在.. GET localhost:9200/_search?filter_path=hits.hits._source 当我使用查询 filter_path 时,它会显示弃用警告。还有其他选择吗?
    • 重新阅读答案 :) :9200/_search 已被弃用。使用:9200/your-index-name/_search。如果 URL 中的 port_search 参数之间没有任何内容,则您正在搜索所有索引(表)。
    • 哦,我的错!非常感谢,伙计!!它就像魅力一样!
    猜你喜欢
    • 2021-03-22
    • 2016-12-18
    • 1970-01-01
    • 2016-04-10
    • 2017-01-20
    • 1970-01-01
    • 2012-02-04
    • 2018-12-13
    • 2021-07-01
    相关资源
    最近更新 更多