Building a query
Before search, a query must be built, Elasticsearch provides several ways to build these queries. In this recipe, will see how to create a query object via QueryBuilder
and via simple strings.
Getting ready
You need an up-and-running Elasticsearch installation as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
A Maven tool, or an IDE that natively supports it for Java programming such as Eclipse or IntelliJ IDEA, must be installed.
The code for this recipe is in the chapter_14/nativeclient
directory and the referred class is QueryCreation
.
How to do it...
To create a query, we will perform the following steps:
We need to import the
QueryBuilders
:import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder;
We'll create a query using
QueryBuilder:
TermQueryBuilder filter...