Comprehensive Guide to Java API for Elasticsearch
Introduction to Elasticsearch
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data so you can discover the expected and uncover the unexpected.
Setting Up the Java Environment
Before interacting with Elasticsearch using Java, you need to set up your Java environment properly:
- Ensure you have Java Development Kit (JDK) installed.
- Set up Apache Maven or Gradle for dependency management.
- Add the Elasticsearch client dependency to your project.
For Maven, add the following dependency to your pom.xml
:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.10.0</version> </dependency>
Connecting to Elasticsearch Cluster
To interact with Elasticsearch, you need to establish a connection to your Elasticsearch cluster. Below is an example of how to create a RestHighLevelClient to connect to Elasticsearch:
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; public class ElasticsearchClient { public static void main(String[] args) { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); // Close the client connection try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
Creating an Index
Creating an index in Elasticsearch is similar to creating a table in a relational database. Here is an example of how to create an index:
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; public class CreateIndexExample { public static void main(String[] args) { try (RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")))) { CreateIndexRequest request = new CreateIndexRequest("my_index"); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println("Index created: " + createIndexResponse.index()); } catch (IOException e) { e.printStackTrace(); } } }
Adding a Document
Adding a document to an index is done via the IndexRequest
class. Here’s an example:
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class AddDocumentExample { public static void main(String[] args) { try (RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")))) { MapjsonMap = new HashMap<>(); jsonMap.put("user", "kimchy"); jsonMap.put("postDate", "2020-01-01"); jsonMap.put("message", "trying out Elasticsearch"); IndexRequest indexRequest = new IndexRequest("posts") .id("1").source(jsonMap, XContentType.JSON); IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("Document added with id: " + indexResponse.getId()); } catch (IOException e) { e.printStackTrace(); } } }
Searching for Documents
Once documents are added, you can search for them using the SearchRequest
class. Here’s an example:
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; public class SearchDocumentExample { public static void main(String[] args) { try (RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")))) { SearchRequest searchRequest = new SearchRequest("posts"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy")); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println("Search results: " + searchResponse.toString()); } catch (IOException e) { e.printStackTrace(); } } }
Conclusion
In this tutorial, we covered the basics of using the Java API to interact with Elasticsearch. We learned how to set up the environment, connect to an Elasticsearch cluster, create an index, add documents, and search for documents. With these basics, you can start building more complex Elasticsearch queries and operations to suit your needs.