Building a query
Before a search, a query must be built and ElasticSearch provides several ways to build these queries. In this recipe, we will see how to create a query object via QueryBuilder
and via simple strings.
Getting ready
You will need a working ElasticSearch cluster and a working copy of Maven. The code of this recipe is in chapter_10/nativeclient
in the code bundle available on Packt's website, and on GitHub (https://github.com/aparo/elasticsearch-cookbook-second-edition). The referred class is QueryCreation
.
How to do it...
To create a query, we will perform the following steps:
- There are several ways to define a query in ElasticSearch and they are interchangeable. Generally a query can be defined as a combination of the following components:
QueryBuilder
: This is a helper to build a query.XContentBuilder
: This is a helper to create JSON code. We have discussed it in the Managing mappings recipe in this chapter. The JSON code to be generated is similar to the previous REST,...