Search requests using Java

While it's easy to write a JSON query and directly use it with the Python client, using Java client requires a bit of expertise to create queries using Elasticsearch Java APIs.

In Java, there is the QueryBuilder class that helps you in constructing queries. Once the queries are created, you can execute that query with the client's prepareSearch method.

First of all, you need the following imports in your code:

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

Then you can start building queries and executing them:

QueryBuilder query = QueryBuilders.termQuery("screen_name", "d_bharvi"); SearchResponse response = client.prepareSearch() ...

Get Elasticsearch Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.