【发布时间】: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