Luke
Luke

Reputation: 7089

Querying Elasticsearch with escaped quotes in python requests

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:

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

Answers (3)

Luke
Luke

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:

  • http -> https
  • the string as '"text in quotes"'
  • sent the query as data 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

Adrien Chaussende
Adrien Chaussende

Reputation: 361

If the string is not the problem, be careful of character encoding. Try to manage it and use UTF-8.

Upvotes: 0

OneCricketeer
OneCricketeer

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:

  1. Use double quotes for the Python string and escape "\"text in quotes\""
  2. Just use single quotes with un-escaped double quotes '"text in quotes"'

Upvotes: 1

Related Questions