# Python Script for missing filed

**URL:** <https://discuss.elastic.co/t/python-script-for-missing-filed/39911>\
**Category:** Elasticsearch\
**Created:** [January 22, 2016, 5:43pm UTC](https://discuss.elastic.co/t/python-script-for-missing-filed/39911 "2016-01-22T17:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivaprakash\_Shanmuga](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sivaprakash\_Shanmuga](https://discuss.elastic.co/u/Sivaprakash_Shanmuga)\
**Post date:** [January 22, 2016, 5:43pm UTC](https://discuss.elastic.co/t/python-script-for-missing-filed/39911/1 "2016-01-22T17:43:05Z")

</div>

Hi

I want to achive the following query in Python DSL

> {  
> "query": {  
> "filtered":  
> {  
> "filter":  
> {  
> "and":  
> [  
> { "terms": { "schoolID": 3456} },  
> { "bool": {  
> "must": [  
> { "term": { "facility": "1456" } },  
> { "term": { "facility": "1457" } }  
> ]  
> } },  
> { "bool": {  
> "should": [  
> { "range": {"fees": {"gte": min\_fees, "lte": max\_fees}} },  
> {"missing" : { "field" : "fees" }}  
> ]  
> } },  
> {"geo\_bounding\_box":  
> {  
> "location":  
> {  
> "top\_right":  
> {  
> "lat": X2,  
> "lon": y2  
> },  
> "bottom\_left":  
> {  
> "lat": x1,  
> "lon": y1  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }

What I have written in Python is

> for facility in facilities:  
> facility\_query.append({ "term": { "facility": int(facility) } })

> s = Search(using=es, index="School", doc\_type = "student")   
> .filter("term" , schoolID = schoolId )   
> .filter("geo\_bounding\_box", location = { "top\_right" : {"lat": x2, "lon": y2 },  
> "bottom\_left" : { "lat": x1, "lon": y1 }})   
> .query("range", fees = { "from": min\_fees, "to": max\_fees })

> if facility:  
> s = s.query("bool", must= facility\_query)

> response = s.execute()

Two things I couldnt achieve

1. In Price Range how to include missing field?
2. Facility field I have to pass multiple values inside "must" bool query.

What needs to be altered in Python code to achieve it?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [January 26, 2016, 8:38am UTC](https://discuss.elastic.co/t/python-script-for-missing-filed/39911/2 "2016-01-26T08:38:08Z")

</div>

Hi Sivaprakash,

this is really more of a Python / design question than related to Elasticsearch. The [Python Elasticsearch client](http://elasticsearch-py.readthedocs.org/en/master/index.html) just needs Python dicts for query documents and it seems to me you've built your own API on top of it which creates this data structure for you.

I'd build the query dict bottom up, so first build the individual clauses, like the school filter, the geo filter and so on, then connect them to via an `and` clause and build the overall query structure. This is something your `Search` class seems to be lacking yet.

You can look at the [builder pattern](https://en.wikipedia.org/wiki/Builder_pattern) and peek at the [QueryBuilders Java API](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-query-dsl.html) (and specifically the [bool query API](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html#java-query-dsl-bool-query)) for inspiration.

Daniel

---

<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 5, 2017, 11:21pm UTC](https://discuss.elastic.co/t/python-script-for-missing-filed/39911/3 "2017-07-05T23:21:41Z")

</div>


