There are to libraries to do this pyes and pyelasticsearch. The pyelasticsearch website looks good, and pyes take an other approach but also is ok.
In the other hand this code works and it is very simple.
import urllib2 as urllib
import json
import pprint
query = {
"from":0,
"size":10,
"query":{
"field" : {
"name" : "david"
}
},
"sort":[
{"name":"asc"},
{"lastName":"asc"}
]
}
query = json.dumps(query)
response = urllib.urlopen(
'http://localhost:9200/users/users/_search',
query
)
result = json.loads( response.read() )
pprint.pprint(result)
So I'm thinking about use the simple code instead of learn the tricks of the libraries.
There is nothing wrong with your approach of using the REST API to interface with ElasticSearch.
Pyes and the other libraries provide a wrapper around the REST API so that you can write Python code as oppose to building the JSON queries yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With