Building queries on the fly with java 1.7 api


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?

(Christoph) #2


if you don't need scoring of your results, you basically want to combine two AndFilters containing RangeFilter and maybe a TermFilter for the string equality. The you put those into an OrFilter. Or simply use two BoolFilters (about some of the differences see this old topic
The building blocks should all be here:

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.

(system) #3