Executing a standard search
In the previous recipe, we saw how to build a query. In this recipe we can execute this query to retrieve some documents.
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 placed on Packt's website, and on GitHub (https://github.com/aparo/elasticsearch-cookbook-second-edition). The referred class is QueryExample
.
How to do it...
To execute a standard query, we will perform the following steps:
- After having created a query, it is enough to use the
prepareQuery
call in order to execute it and pass it your query object. Here is a complete example:import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchHit; import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*...