Chapter 10. Java Integration

In this chapter, we will cover the following topics:

  • Creating an HTTP client
  • Creating a native client
  • Managing indices with the native client
  • Managing mappings
  • Managing documents
  • Managing bulk action
  • Creating a query
  • Executing a standard search
  • Executing a facet search
  • Executing a scroll/scan search

Introduction

ElasticSearch functionalities can be easily integrated in every Java application in several ways, both via REST API then native ones.

With the use of Java, it's easy to call a REST HTTP interface with one of the many libraries available, such as Apache HTTPComponents Client (http://hc.apache.org/). In this field, there is no library which is used the most; typically developers choose the library that best suits their taste or that they know very well.

Every JVM language can also use the Native protocol to integrate ElasticSearch in their products. The Native protocol, discussed in Chapter 1, Getting Started, is one of the fastest protocols available to communicate with ElasticSearch due to many factors, such as its binary nature, the fast native serializer/deserializer of the data, the asynchronous approach for communicating and the hop reduction (native client is able to communicate directly with the node that contains the data without executing a double hop needed in REST calls).

The main disadvantage of using Native protocol is that it evolves during the life of ElasticSearch and there is no guarantee of compatibility between versions. For example, if a field of a request or a response changes, their binary serialization changes, generating incompatibilities between client and server with different versions.

The ElasticSearch community tries not to change 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.

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 go in to depth of every call as we have already described for the REST 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 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.

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 when integrating with ElasticSearch as they are more stable between versions and well documented.

All the code presented in these recipes is available in the book's code repository available on Packt's website 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.138.61.208