Reputation: 7089
I'm trying to query elasticsearch
using python requests
. Following this post, I'm using the following process:
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)
Unfortunately, the quotation marks in the query don't appear to be properly escaped for elasticsearch. I've also tried:
'\\"text in quotes\\"'
response = requests.get(url, data=json.dumps(params))
The equivalent curl, which works, looks as follows:
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,
}'
Upvotes: 2
Views: 3175
Reputation: 7089
Turns out that uri being used in the python example went to http
whereas the uri for the curl
example was https
. It works with the following changes:
'"text in quotes"'
response = requests.get(url, data=json.dumps(params))
I don't understand why it was partially working before, 100 hits were being returned.
Upvotes: 0
Reputation: 361
If the string is not the problem, be careful of character encoding. Try to manage it and use UTF-8.
Upvotes: 0
Reputation: 191728
In cURL, you are escaping the quotes. "\"text in quotes\""
, and this will become "text in quotes"
.
Your Python problem is that you don't need to escape anything if you use single quotes like you've done with r'\"text in quotes\"'
that will print \"text in quotes\"
because it is a raw string containing the slashes.
So, you have two options:
"\"text in quotes\""
'"text in quotes"'
Upvotes: 1