# Building queries on the fly with java 1.7 api

**URL:** <https://discuss.elastic.co/t/building-queries-on-the-fly-with-java-1-7-api/35499>\
**Category:** Elasticsearch\
**Created:** [November 25, 2015, 12:09am UTC](https://discuss.elastic.co/t/building-queries-on-the-fly-with-java-1-7-api/35499 "2015-11-25T00:09:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![someProgrammerGuy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@someProgrammerGuy](https://discuss.elastic.co/u/someProgrammerGuy)\
**Post date:** [November 25, 2015, 12:09am UTC](https://discuss.elastic.co/t/building-queries-on-the-fly-with-java-1-7-api/35499/1 "2015-11-25T00:09:23Z")

</div>

Hi folks

I'd like to do something like this on the fly:

select \* from index where (fld1 \< 4) and (fld2 == "a") or ( (fld3 \< "b") and (fld3 \> "d"))

where I'm given the names of the fields and the values.

I _think_ I've got to use QueryFilters in some way but can't find any examples.

Can someone me out?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 25, 2015, 10:40am UTC](https://discuss.elastic.co/t/building-queries-on-the-fly-with-java-1-7-api/35499/2 "2015-11-25T10:40:52Z")

</div>

Hi,

if you don't need scoring of your results, you basically want to combine two [AndFilters](https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/and-filter.html) containing [RangeFilter](https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/range-filter.html) and maybe a [TermFilter](https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/term-filter.html) for the string equality. The you put those into an [OrFilter](https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/or-filter.html). Or simply use two BoolFilters (about some of the differences see this [old topic](https://discuss.elastic.co/t/advice-needed-for-searching-filters-vrs-queries/11141/2)  
The building blocks should all be here: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/query-dsl-filters.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/query-dsl-filters.html)

Most filters can be created using FilterBuilders, the methods you use to set the parameters largely correpond to the Query DSL. That said, as soon as you switch to ES 2.0, there are going to be changes regarding the use of filters, since they are merged with queries. There's some general overview [here](https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0).

---

<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:35pm UTC](https://discuss.elastic.co/t/building-queries-on-the-fly-with-java-1-7-api/35499/3 "2017-07-05T23:35:56Z")

</div>


