【问题标题】:Querying Elasticsearch with escaped quotes in python requests在 python 请求中使用转义引号查询 Elasticsearch
【发布时间】:2016-03-23 03:26:57
【问题描述】:

我正在尝试使用 python requests 查询 elasticsearch。继this post之后,我正在使用以下流程:

params = {
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "query_string": {
                                "query": r'\"text in quotes\"'
                            }
                        }
                    ]
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "@timestamp": {
                                    "from": 1458581389860,
                                    "to": 1458494989000
                                }
                            }
                        }
                    ]
                }
            }
        }
    },
    "size": 100,
}
response = requests.get(url, params=params)

很遗憾,查询中的引号似乎没有为 elasticsearch 正确转义。我也试过:

  • '\\"text in quotes\\"'
  • response = requests.get(url, data=json.dumps(params))

有效的等效 curl 如下所示:

curl -XGET 'MYURL/_search?pretty' -d '{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "query_string": {
                                "query": "\"test in quotes\""
                            }
                        }
                    ]
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "@timestamp": {
                                    "from": 1458581389860,
                                    "to": 1458494989000
                                }
                            }
                        }
                    ]
                }
            }
        }
    },
    "size": 100,
}'

【问题讨论】:

  • '\\"text in quotes\\"'r'\"text in quotes\"' 相同
  • 我就是这么想的。 requests 可以在值传递后对字符串做些什么吗?
  • 可能会,但我不知道为什么会这样。你试过使用 python elasticsearch 库吗?
  • 另外,您是否阅读了您提供的链接上的答案? params 参数不适用于正在发送的数据。如果您尝试将数据发送到服务器,您应该专门使用 data 参数
  • 我正在尝试发送链接中概述的查询参数,这就是我使用params 参数的原因。我也尝试使用data参数通过上述dict的json转储发送查询,但无济于事。

标签: python elasticsearch python-requests


【解决方案1】:

在 cURL 中,您正在转义引号。 "\"text in quotes\"",这将变成"text in quotes"

您的 Python 问题是,如果您使用单引号,就像使用 r'\"text in quotes\"' 所做的那样,您不需要转义任何内容,这将打印 \"text in quotes\",因为它是包含斜杠的原始字符串。

所以,你有两个选择:

  1. 对 Python 字符串使用双引号并转义 "\"text in quotes\""
  2. 只需使用单引号和未转义的双引号'"text in quotes"'

【讨论】:

  • 这是我尝试的第一种方法。也许字符串不是问题。
【解决方案2】:

如果字符串不是问题,请注意字符编码。尝试管理它并使用 UTF-8。

【讨论】:

    【解决方案3】:

    事实证明,python 示例中使用的 uri 是 http,而 curl 示例的 uri 是 https。它适用于以下更改:

    • http -> https
    • 字符串为'"text in quotes"'
    • 将查询作为数据发送response = requests.get(url, data=json.dumps(params))

    我不明白为什么它之前部分工作,返回了 100 次点击。

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多