# Advice on building a query dynamically

**URL:** https://discuss.elastic.co/t/advice-on-building-a-query-dynamically/5666
**Category:** Elasticsearch
**Created:** [October 24, 2011, 9:29am UTC](https://discuss.elastic.co/t/advice-on-building-a-query-dynamically/5666 "2011-10-24T09:29:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jondow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondow/32/2111_2.png) [@Jondow](https://discuss.elastic.co/u/Jondow)
#### Post date: [October 24, 2011, 9:29am UTC](https://discuss.elastic.co/t/advice-on-building-a-query-dynamically/5666/1 "2011-10-24T09:29:15Z")

</div>

Hi all,

I'm looking for some advice on how to build a query dynamically. So  
what I mean by that is that our web application interface allows the  
user to create a filter of the information in our database, which is  
effectively an ES query on the indexed data from the database.

Doing this dynamically using the filter criteria that are available to  
the user is conceptually simple but I'm not sure how to convert that  
to an ES query. We have two options:

1. Use the Lucene query syntax e.g. hostname:Blah - the problem here  
is determining all the options for Lucene query syntax ... the docs  
page here [http://lucene.apache.org/java/3\_4\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html)  
doesn't provide clues as to how to accomplish the many options made  
available via the ES Query DSL.

2. Use the ES DSL. Here the documentation is good, but how to combine  
multiple criteria in a single DSL document becomes more of a  
challenge.

Has anybody had to solve this before? Does anybody have any good  
suggestions here?

Thanks in advance,  
Darryl Pentz

---

<div class="post-metadata">

### Author: ![Jussi\_Arpalahti](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@Jussi\_Arpalahti](https://discuss.elastic.co/u/Jussi_Arpalahti)
#### Post date: [October 25, 2011, 6:39pm UTC](https://discuss.elastic.co/t/advice-on-building-a-query-dynamically/5666/2 "2011-10-25T18:39:12Z")

</div>

On 24 October 2011 12:29, Jondow [djpentz@gmail.com](mailto:djpentz@gmail.com) wrote:

> Hi all,
> 
> I'm looking for some advice on how to build a query dynamically. So  
> what I mean by that is that our web application interface allows the  
> user to create a filter of the information in our database, which is  
> effectively an ES query on the indexed data from the database.
> 
> Doing this dynamically using the filter criteria that are available to  
> the user is conceptually simple but I'm not sure how to convert that  
> to an ES query. We have two options:
> 
> 1. Use the Lucene query syntax e.g. hostname:Blah - the problem here  
> is determining all the options for Lucene query syntax ... the docs  
> page here [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_4_0/queryparsersyntax.html)  
> doesn't provide clues as to how to accomplish the many options made  
> available via the ES Query DSL.
> 
> 2. Use the ES DSL. Here the documentation is good, but how to combine  
> multiple criteria in a single DSL document becomes more of a  
> challenge.
> 
> Has anybody had to solve this before? Does anybody have any good  
> suggestions here?
> 
> Hi.

Since I used pyes (python Elasticsearch module) this might not be relevant  
for you, but this is what I did:

params =  
{  
"searches" : [  
{"TermQuery" :  
[  
["\_type", "doctype"],  
["field", "content"]  
]  
}]  
}

search\_args =   
for search in params["searches"]:  
for term, arg in search.get("TermQuery", ):  
search\_args.append(pyes.TermQuery(term, arg))  
query = pyes.BoolQuery(must=search\_args)

Since pyes takes care of generating Query DSL from Query objects, I can  
build the query from user input or (like in this case) parameter dictionary.  
If you need to stick to javascript, perhaps you need some kind of JSON  
templating instead of Query class instances..

Best,  
Jussi

---

<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:51am UTC](https://discuss.elastic.co/t/advice-on-building-a-query-dynamically/5666/3 "2017-07-06T03:51:00Z")

</div>


