# Python client + Pyes queries

**URL:** <https://discuss.elastic.co/t/python-client-pyes-queries/7183>\
**Category:** Elasticsearch\
**Created:** [March 30, 2012, 4:25pm UTC](https://discuss.elastic.co/t/python-client-pyes-queries/7183 "2012-03-30T16:25:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santiago\_Basulto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santiago_basulto/32/2810_2.png) [@Santiago\_Basulto](https://discuss.elastic.co/u/Santiago_Basulto)\
**Post date:** [March 30, 2012, 4:25pm UTC](https://discuss.elastic.co/t/python-client-pyes-queries/7183/1 "2012-03-30T16:25:46Z")

</div>

Hello people,

i'm trying to figure out what's the best python client to use. I've started  
using pyes, but maybe you have another recommendation.

With pyes, i've a problem, I can't translate this query successfully (it's  
just trying to look for "EM" in the user\_type):

curl -XGET '[http://127.0.0.1:9200/test\_index/user\_type/\_search?pretty=1](http://127.0.0.1:9200/test_index/user_type/_search?pretty=1)' -d  
'{

```
"query" : {
    bool:
       { should: [
           { text: { "first_name": "EM" }},
           { text: { "first_name.partial": "EM" }}
         ]
    }
}

```

}'

I've done this according the 0.17v docs, but it seems like 0.17 is an unestable branch, and in 0.16 this doesn't work:

q1 = TextQuery("first\_name","EM")  
q2 = TextQuery("first\_name.partial","EM")

q = BoolQuery(should = [q1,q2])

conn.search(q,indices = "test\_index",doc\_types = "user\_type")

Any idea how can I translate the query into a python client?

Thanks!

---

<div class="post-metadata">

**Author:** ![Radu\_Gheorghe1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe1/32/2688_2.png) [@Radu\_Gheorghe1](https://discuss.elastic.co/u/Radu_Gheorghe1)\
**Post date:** [April 2, 2012, 6:18am UTC](https://discuss.elastic.co/t/python-client-pyes-queries/7183/2 "2012-04-02T06:18:53Z")

</div>

Hi Santiago,

I've just realized that we're not using pyes for queries, as I  
thought. So, even though it's not an answer to your question, below  
are the important bits of how we do it using requests and json:

# ==== import requests, json #where we run our queries url='[http://localhost:9200/](http://localhost:9200/)' #query here as a dictionary to\_search={ "size" : 5, "filter" : { "match\_all" : {} } } #load it to\_search = json.dumps(to\_search) #do the search result = requests.post(url + '\_search', to\_search) #now load the results results = json.loads(result.content) #here comes some (quite dirty) error handling try: #get the error code, if an error occurred print "ERROR: HTTP return code is %d." % results["status"] #get the error message as well print "Error message was: %s" % results["error"].split("; }{")[0] return None except: #get the number of hits. You can bet there's more interesting stuff, but this is all we use hits = results['hits']['total'] #then you have the actual results actual\_results = results['hits']['hits']

Hope this helps. I'm sure you could use this sort of stuff to do  
pretty much any kind of query.

Of course this method comes with it's own disadvantages compared to  
any kind of ES python client, but I enjoy a straightforward approach.  
Especially when I don't do a lot of fancy stuff.

Best regards,  
Radu

On Mar 30, 7:25 pm, Santiago Basulto [santiago.basu...@gmail.com](mailto:santiago.basu...@gmail.com)  
wrote:

> Hello people,
> 
> i'm trying to figure out what's the best python client to use. I've started  
> using pyes, but maybe you have another recommendation.
> 
> With pyes, i've a problem, I can't translate this query successfully (it's  
> just trying to look for "EM" in the user\_type):
> 
> curl -XGET '[http://127.0.0.1:9200/test\_index/user\_type/\_search?pretty=1'-d](http://127.0.0.1:9200/test_index/user_type/_search?pretty=1'-d)  
> '{
> 
> ```
> "query" : {
> bool:
> { should: [
> { text: { "first_name": "EM" }},
> { text: { "first_name.partial": "EM" }}
> ]
> }
> }
> 
> ```
> 
> }'
> 
> I've done this according the 0.17v docs, but it seems like 0.17 is an unestable branch, and in 0.16 this doesn't work:
> 
> q1 = TextQuery("first\_name","EM")  
> q2 = TextQuery("first\_name.partial","EM")
> 
> q = BoolQuery(should = [q1,q2])
> 
> conn.search(q,indices = "test\_index",doc\_types = "user\_type")
> 
> Any idea how can I translate the query into a python client?
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![Alberto\_Paro\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alberto_paro_2/32/1137_2.png) [@Alberto\_Paro\_2](https://discuss.elastic.co/u/Alberto_Paro_2)\
**Post date:** [April 3, 2012, 10:19pm UTC](https://discuss.elastic.co/t/python-client-pyes-queries/7183/3 "2012-04-03T22:19:18Z")

</div>

Your code should work.  
The query object should be passed in a Search object for customizing facet/size/highlighting.

I suggest you to use the latest github version and give a look to pyes/tests/test\_query.py for examples.

Hi,  
Alberto

Inviato da iPad

Il giorno 30/mar/2012, alle ore 18:25, Santiago Basulto [santiago.basulto@gmail.com](mailto:santiago.basulto@gmail.com) ha scritto:

> Hello people,
> 
> i'm trying to figure out what's the best python client to use. I've started using pyes, but maybe you have another recommendation.
> 
> With pyes, i've a problem, I can't translate this query successfully (it's just trying to look for "EM" in the user\_type):
> 
> curl -XGET '[http://127.0.0.1:9200/test\_index/user\_type/\_search?pretty=1](http://127.0.0.1:9200/test_index/user_type/_search?pretty=1)' -d '{  
> "query" : {  
> bool:  
> { should: [  
> { text: { "first\_name": "EM" }},  
> { text: { "first\_name.partial": "EM" }}  
> ]  
> }  
> }  
> }'
> 
> I've done this according the 0.17v docs, but it seems like 0.17 is an unestable branch, and in 0.16 this doesn't work:
> 
> q1 = TextQuery("first\_name","EM")  
> q2 = TextQuery("first\_name.partial","EM")
> 
> q = BoolQuery(should = [q1,q2])
> 
> conn.search(q,indices = "test\_index",doc\_types = "user\_type")
> 
> Any idea how can I translate the query into a python client?
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:33am UTC](https://discuss.elastic.co/t/python-client-pyes-queries/7183/4 "2017-07-06T03:33:46Z")

</div>


