Reader small image

You're reading from  Elasticsearch 8.x Cookbook - Fifth Edition

Product typeBook
Published inMay 2022
PublisherPackt
ISBN-139781801079815
Edition5th Edition
Right arrow
Author (1)
Alberto Paro
Alberto Paro
author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro

Right arrow

Chapter 13: Java Integration

Elasticsearch functionalities can be easily integrated into any Java application in a couple of ways, via a REST API or via native APIs. In Java, it's easy to call a REST HTTP interface with one of the many libraries available, such as the Apache HttpComponents client (see http://hc.apache.org/ for more information). In this field, there's no such thing as the most used library; typically, developers choose the library that best suits their preferences or one that they know very well. From Elasticsearch 6.x onward, Elastic has provided a battle low-/high-level HTTP for clients to use. In version 8.x, Elastic released a modern/functional/strongly typed client and in this chapter, we will mainly use this version for all the examples that are provided.

Each Java Virtual Machine (JVM) language can also use the native protocol to integrate Elasticsearch with their applications; however, we will not cover this because it has fallen out of use from...

Creating a standard Java HTTP client

An HTTP client is one of the easiest clients to create. It's very handy because it allows for the calling, not only of the internal methods as the native protocol does, but also of third-party calls implemented in plugins that can only be called via HTTP.

Getting ready

You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

To correctly execute the following commands, you will need an index populated with the ch04/populate_kibana.txt commands that are available in the online code.

A Maven tool or an Integrated Development Environment (IDE) that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed. Elasticsearch code is targeting Java 17, so it's best practice to have installed JDK 17 or above.

The code for this recipe is in the chapter_13/http_java_client directory...

Creating a low-level Elasticsearch client

There are two official Elasticsearch clients: the low-level one and the new typed one available from Elasticsearch 8.x (https://github.com/elastic/elasticsearch-java). The low-level one is used to communicate with Elasticsearch, and its main features are as follows:

  • Minimal dependencies
  • Load balancing across all available nodes
  • Failover in the case of node failures and upon specific response codes
  • Failed connection penalization (whether a failed node is retried depends on how many consecutive times it failed; the more failed attempts, the longer the client will wait before trying that same node again)
  • Persistent connections
  • Trace logging of requests and responses
  • Optional automatic discovery of cluster nodes

Getting ready

You need an up-and-running Elasticsearch installation, which can be obtained as described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

To...

Using the Elasticsearch official Java client

The official Java client is built on top of a low-level one and provides strong typed communication with Elasticsearch.

Initially released with Elasticsearch in the latest versions of 7.15 or above, this client is the official Java client for Elasticsearch 8.x. It provides many extra functionalities, such as the following:

  • Integration from application classes to JSON instances via an object mapper such as Jackson
  • Request/response marshaling/unmarshaling that provides stronger typed programming
  • Support for both synchronous and asynchronous calls
  • Use of fluent builders and functional patterns to allow writing concise yet readable code when creating complex nested structures
  • Built on top of previous low-level client

Getting ready

You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

To correctly execute...

Managing indices

In the previous recipe, we learned how to initialize a client to send calls to an Elasticsearch cluster. In this recipe, we will learn how to manage indices via client calls.

Getting ready

You need an up-and-running Elasticsearch installation, which we described how to get in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the ch13/elasticsearch-java-client directory and the referred class is IndicesOperations.

How to do it...

An Elasticsearch client maps all index operations under the indices object of the client, such as create, delete, exists, open, close, and optimize. The following steps retrieve a client and execute the main operations on the indices:

  1. First, we import the required classes, as shown in the following code:
    import...

Managing mappings

After creating an index, the next step is to add some mappings to it. We have already seen how to add a mapping via the REST API in Chapter 3, Basic Operations. In this recipe, we will look at how to manage mappings via a native client.

Getting ready

You need an up-and-running Elasticsearch installation, which we described how to get in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the ch13/elasticsearch-java-client directory and the referred class is MappingOperations.

How to do it...

In the following steps, we add a mapping to a myindex index via the native client:

  1. Import the required classes using the following code:
    import co.elastic.clients.elasticsearch.ElasticsearchClient;
    import java.io.IOException;
    import java.security.KeyManagementException...

Managing documents

The native APIs for managing documents (index, delete, and update) are the most important after the search APIs. In this recipe, we will learn how to use them. In the next recipe, we will proceed to bulk actions to improve performance.

Getting ready

You need an up-and-running Elasticsearch installation, which we described how to get in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool, or an IDE that natively supports it for Java programming such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the ch13/elasticsearch-java-client directory and the referred class is DocumentOperations.

How to do it...

For managing documents, we will perform the following steps:

  1. We'll need to import the required classes to execute all the document CRUD operations via the high-level client, as follows:
    import co.elastic.clients.elasticsearch.ElasticsearchClient...

Managing bulk actions

Executing automatic operations on items via a single call will often be the cause of a bottleneck if you need to index or delete thousands/millions of records. The best practice, in this case, is to execute a bulk action.

We have discussed bulk actions via the REST API in the Speeding up atomic operations (bulk) recipe in Chapter 3, Basic Operations.

Getting ready

You need an up-and-running Elasticsearch installation, which you can get using the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code of this recipe is in the ch13/elasticsearch-java-client directory and the referred class is BulkOperations.

How to do it...

To manage a bulk action, we will perform these steps:

  1. We'll need to import the required classes to execute bulk actions via the high...

Building a query

Before a search, a query must be built. Elasticsearch provides several ways to build these queries. In this recipe, we will learn how to create a query object via QueryBuilder and simple strings.

Getting ready

You need an up-and-running Elasticsearch installation, which you can get as described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the ch13/elasticsearch-java-client directory and the referred class is QueryCreation.

How to do it...

To create a query, we will perform the following steps:

  1. We need to import SearchRequest using the following code:
    import co.elastic.clients.elasticsearch.core.SearchRequest;
  2. Next, we'll create a query using SearchRequest, as follows:
    SearchRequest searchRequest = new SearchRequest.Builder...

Executing a search with aggregations

The previous recipe can be extended to support aggregations in order to retrieve analytics on indexed data.

Getting ready

You need an up-and-running Elasticsearch installation, which you can get as described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the ch13/elasticsearch-java-client directory, and the referred class is AggregationExample.

How to do it...

To execute a search with aggregations, we will perform the following steps:

  1. We need to import the necessary classes for the aggregations using the following code:
    import co.elastic.clients.elasticsearch.ElasticsearchClient;
    import co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate;
    import co.elastic.clients.elasticsearch._types.aggregations...

Integrating with DeepLearning4j

DeepLearning4J (DL4J) is one of the most used open source libraries in machine learning. It can be found at https://deeplearning4j.org/.

The best description for this library is available on its website, which says—Deeplearning4j is the first commercial-grade, open-source, distributed deep learning library written for Java and Scala. Integrated with Hadoop and Apache Spark, DL4J brings AI to business environments for use on distributed GPUs and CPUs.

In this recipe, we will see how it's possible to use Elasticsearch as a source for data to be trained in a machine learning algorithm.

Getting ready

You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.

A Maven tool or an IDE that natively supports Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.

The code for this recipe is in the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Elasticsearch 8.x Cookbook - Fifth Edition
Published in: May 2022Publisher: PacktISBN-13: 9781801079815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro