【问题标题】:Elasticsearch - parsing query that uses curl commandElasticsearch - 使用 curl 命令解析查询
【发布时间】:2021-08-23 15:54:38
【问题描述】:

我有一个从代码运行的查询(使用外部库),现在我尝试使用 curl 命令直接从终端运行它:

这是原始查询:

{
    "index": [
        "logstash-*2021.08.21*",
        "logstash-*2021.08.22*"
    ],
    "ignore_unavailable": true,
    "allow_no_indices": true,
    "type": "doc",
    "body": {
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "@timestamp": {
                                "gte": "now-24H/H"
                            }
                        }
                    }
                ]
            }
        }
    }
}

但是从 Linux 终端运行以下查询:

curl -u "*******:****************" -XGET "https://XXXXXXXXXXXXXXXXXXXXXXXXXX:9200/logstash- 
*/_search" 
-H 'Content-Type: application/json' 
-d '
 {
    "index": [
        "logstash-*2021.08.21*",
        "logstash-*2021.08.22*"
    ],
    "ignore_unavailable": true,
    "allow_no_indices": true,
    "type": "doc",
    "body": {
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "@timestamp": {
                                "gte": "now-24H/H"
                            }
                        }
                    }
                ]
            }
        }
    }
}
'

返回错误:

{"error":{"root_cause":[{"type":"parsing_exception","re​​ason":"未知 一个 START_ARRAY 的键 [index].","line":1,"col":10}],"type":"parsing_exception","re​​ason":"未知 [index] 中 START_ARRAY 的键。","line":1,"col":10},"status":4

值得注意的是,在没有附加参数的情况下运行以下查询,会返回正确的结果:

curl -u "elastic:****************" -XGET "https://XXXXXXXXXXXXXXXXXXXXXXXXXX:9200/logstash-*/_search"

我需要在终端查询中更改什么?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    发生错误是因为搜索 API 未指定 indices 请求正文选项。 也许您的外部库将请求包装到 Elasticsearch,因此您可以提供此选项。

    -XGET "https://XXX:9200/logstash-*/_search" 
    

    您已经指定要在与logstash-* 匹配的所有索引中搜索。

    查看此处的文档以了解所有可能的请求和 url 参数:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

    如果您需要搜索多个索引,您需要按照本指南中的说明进行操作:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multiple-indices.html

    但是由于您已经在 URL 中提供了索引模式,我不知道这是否仍然需要。

    还有:

    请求正文中不允许有 bodytype 字段。因此,在删除索引数组之后,这些将是 Elasticsearch 将抱怨的下一个错误。

    希望能帮到你。

    【讨论】:

      【解决方案2】:

      使用apt-get-install-skill 提示,我将查询更改为:

      curl -u "*******:****************" -XGET --globoff "https://XXXXXXXXXXXXXXXXXXXXXXXXXX:9200/logstash-*2021.08.21*,logstash-*2021.08.22*/_search?ignore_unavailable=true&allow_no_indices=true" -H 'Content-Type: application/json' -d'{"query":{"range":{"@timestamp":{"gte":"now-24H/H"}}}}'
      

      它有效。 谢谢

      【讨论】:

      • 很高兴能为您提供帮助。你介意接受我的回答吗?
      • 不,您对答案投了赞成票,但没有通过勾选标记将其标记为已接受 ..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 2023-01-15
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多