简体   繁体   English

Elasticsearch Search Query using curl and subprocess: Illegal Argument Exception

[英]Elasticsearch Search Query using curl and subprocess: Illegal Argument Exception

I am trying to run a curl command using the run command in the subprocess module in python.我正在尝试使用 python 中子进程模块中的运行命令运行 curl 命令。 But it is failing.但它失败了。 However, the curl command for my elasticsearch search query API is working fine-但是,我的 elasticsearch 搜索查询 API 的 curl 命令运行良好 -

curl -k XGET -H "Content-Type: application/json" -H "Authorization: ApiKey Q2txZ2FvSUJ6Nlcwa3pjbnh0NUM6enBidjdNLTdRQVNhc0tuTEk5UEZmZw==" 'https://internal-a02c1b85dade940ef871b0b6d1e65191-1270244841.us-west-2.elb.amazonaws.com:9200/_search?index=.fleet-enrollment-api-keys-7&size=20&pretty' -d '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}},"fields" : ["_id", "_index"]}'

This is the code that I am trying to run-这是我要运行的代码-

import json
from builtins import int
from pydantic import BaseModel, Field
from typing import List, Dict
from subprocess import PIPE, run

def elasticsearch_search_query(host: str, 
                               port: int, 
                               api_key: str, 
                               path: str, 
                               query: dict,
                               index: str,
                               size: int,
                               fields: List):

    es_path = host + ":" + str(port) + path +"?index="+index+"&"+str(size)+"&pretty"
    es_header = "Authorization: ApiKey" + " " + api_key
    es_dict = {"query": query, "fields": fields}
    es_json = json.dumps(es_dict)
    es_replaced = " "+"'"+ es_json.replace("'", "\"")+"'"

    result = run(["curl", "-k", "-XGET", "-H", "Content-Type: application/json", "-H",
                  es_header,
                  es_path,
                  "-d",
                  es_replaced]
                 # ,stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=False
                )
    print(es_path, es_header, es_replaced)
    print(result.stdout)


elasticsearch_search_query(host="https://internal-a02c1b85dade940ef871b0b6d1e65191-1270244841.us-west-2.elb.amazonaws.com:9200/_search", api_key="Q2txZ2FvSUJ6Nlcwa3pjbnh0NUM6enBidjdNLTdRQVNhc0tuTEk5UEZmZw==",
                           port=9200, size=5, query={"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, fields=["_id", "_index"], path="/_search",index=".fleet-enrollment-api-keys-7")

Here is the error-这是错误-

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "request [/_search:9200/_search] contains unrecognized parameter: [5]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "request [/_search:9200/_search] contains unrecognized parameter: [5]"
  },
  "status" : 400
}
https://internal-a02c1b85dade940ef871b0b6d1e65191-1270244841.us-west-2.elb.amazonaws.com:9200/_search:9200/_search?index=.fleet-enrollment-api-keys-7&5&pretty Authorization: ApiKey Q2txZ2FvSUJ6Nlcwa3pjbnh0NUM6enBidjdNLTdRQVNhc0tuTEk5UEZmZw== '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, "fields": ["_id", "_index"]}'
None

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   350  100   350    0     0  10294      0 --:--:-- --:--:-- --:--:-- 10294
curl: (3) nested brace in URL position 12:
'{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, "fields": ["_id", "_index"]}'

So parameter [5] is query but the same thing is working fine in the curl command but not here.所以参数 [5] 是查询,但同样的事情在 curl 命令中工作正常,但不是在这里。 Any suggestions on how I can pass the query parameter in the -d (data header) using JSON.关于如何使用 JSON 在 -d(数据头)中传递查询参数的任何建议。

FYI- query is the input parameter from the user.仅供参考-查询是来自用户的输入参数。

query and field dict parameter in the code- '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, "fields": ["_id", "_index"]}'代码中的查询字段字典参数 - '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, "fields": ["_id", "_index"]}'

query and field parameter in the curl command - '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}},"fields": ["_id", "_index"]}' curl 命令中的查询字段参数 - '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}},"fields": ["_id", "_index"]}'

There is absolutely no difference in both but it won't get parsed.两者绝对没有区别,但不会被解析。

UPDATE更新

Output of repr(result):代表的 Output(结果):

CompletedProcess(args=['curl', '-k', '-XGET', '-H', 'Content-Type: application/json', '-H', 'Authorization: ApiKey Q2txZ2FvSUJ6Nlcwa3pjbnh0NUM6enBidjdNLTdRQVNhc0tuTEk5UEZmZw==', 'https://internal-a02c1b85dade940ef871b0b6d1e65191-1270244841.us-west-2.elb.amazonaws.com:9200:9200/_search?index=.fleet-enrollment-api-keys-7&size=5&pretty', '-d', '{"query": {"match": {"_id": "c8a537a9-7f98-489b-8f8b-de2450c67ce6"}}, "fields": ["_id", "_index"]}'], returncode=3)

Since the error was由于错误是

contains unrecognized parameter: [5]包含无法识别的参数:[5]

The problem is not in the query but in the way you construct es_path .问题不在于查询,而在于您构造es_path的方式。 Instead of代替

&"+str(size)+

it should be它应该是

&size="+str(size)+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch Python客户端:创建新索引时为“ illegal_argument_exception” - Elasticsearch Python client: 'illegal_argument_exception' when creating a new index 在子过程curl中使用变量 - Using variable in subprocess curl 发生异常:非法参数 - 参数 2,预期对象(使用 win32com 制作 Photoshop 联系表) - Exception occured: Illegal Argument - Argument 2, Object Expected (Making a Photoshop contact sheet using win32com) 使用带有curl命令的子流程模块 - Using subprocess module with curl command 使用 Python 插入新文档 弹性客户端引发非法参数异常 - Insert new document using Python Elastic Client raises illegal_argument_exception 在 elasticsearch 查询中搜索字符串 - Search a string in a elasticsearch query 通过使用滚动ID的Elasticsearch查询返回未找到索引的异常 - Elasticsearch query by using scroll id returns index not found exception Elasticsearch Python API在一个简单查询上导致“ search()缺少1个必需的位置参数” - Elasticsearch Python API results in “search() missing 1 required positional argument” on a simple query Elasticsearch 使用 python 搜索 - Elasticsearch search using python RequestError:RequestError(400,search_phase_execution_exception未能为elasticsearch中的输入字符串创建查询 - RequestError: RequestError(400, search_phase_execution_exception failed to create query For input string in elasticsearch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM