Chapter 14. Java Integration

In this chapter, we will cover the following recipes:

  • Creating a standard Java HTTP client
  • Creating an HTTP Elasticsearch client
  • Creating a native client
  • Managing indices with the native client
  • Managing mappings
  • Managing documents
  • Managing bulk actions
  • Building a query
  • Executing a standard search
  • Executing a search with aggregations
  • Executing a scroll search

Introduction

Elasticsearch functionalities can be easily integrated in any Java application in several ways, both via a REST API and native ones.

In Java it's easy to call a REST HTTP interface with one of the many of libraries available, such as the Apache HttpComponents client http://hc.apache.org/. In this field there's no such thing as the most used library; typically, developers choose the library that best suits their preferences or that they know very well.

Each JVM language can also use the native protocol (discussed in C) to integrate Elasticsearch with their applications.

Chapter 1, Getting Started is one of the faster protocols available to communicate with Elasticsearch due to many factors such as its binary nature, its fast native serializer or deserializer of the data, its asynchronous approach to communicating, and the hop reduction (native client nodes are able to communicate directly with the node that contains the data without executing the double hop needed in REST calls).

The main disadvantage of using native protocol is that it evolves during the development stage of the Elasticsearch life cycle and there is no guarantee of compatibility between versions.

For example, if a field of a request or a response changes, its binary serialization changes, generating incompatibilities between the client and server with different versions.

The Elasticsearch community tries not to make changes often, but in every version, some parts of Elasticsearch are improved and these changes often modify the native API call signature, thus breaking the applications.

It is recommended to use the REST API when integrating with Elasticsearch as it is much more stable across versions.

In Elasticsearch 5.x a new special REST client was delivered by the Elasticsearch team to use REST on Java. This client is managed by them and will receive more improvements in future.

In this chapter, we will see how to initialize different clients and how to execute the commands that we have seen in the previous chapters. We will not cover every call in depth, as we have already described for the REST API ones.

Elasticsearch uses the native protocol and API internally, so these are the most tested ones compared to REST calls due to unit and integration tests available in the Elasticsearch code base.

The official documentation for the native Java API is available at http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/ but it doesn't cover all the API calls.

In many of the recipes in this chapter, the same code is executed mainly with the native client because the REST one is too-low level, and its use is like plain CURL calls as there are no helpers for now (apart from the connection management). It's up to the user to choose the best solution that covers their need.

If you want a complete suite of examples, they are available in the src/test directory of the source code base of Elasticsearch.

As we have already discussed in Chapter 1, Getting Started, the Elasticsearch community recommends using the REST APIs when integrating as they are more stable between releases and well-documented.

All the code presented in these recipes is available in the book code repository and can be built with Maven.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.210