Elasticsearch in Java

Tram Ho

1. Overview

In this article, we will dive into some key concepts related to full-text search engines, with a special focus on Elaticsearch.

Since this is a Java-driven article, we will not provide detailed step-by-step instructions on how to set up Elaticsearch and show how this program works, instead, we will target the client. Java, and how to use key features like index creation, delete, get and search.

2. Setup

To install Elaticsearch, please refer to the official setup guide.

The installation process is quite simple, just download the zip / tar package and run the elaticsearch script file (elSTERearch.bat for Windows users).

By default, Elaticsearch listens on port 9200 for HTTP queries. We can verify that it successfully launched by opening http: // localhost: 9200 / URL in your favorite browser:

3. Maven Configuration

Now that we have up and running the basic Elaticsearch cluster, let’s move straight to the Java client. First of all, we need to have the Maven dependency declared in pom.xml:

You can always check out the latest versions hosted by Maven Central with the link provided previously.

4. Java API

Before moving straight to using the main Java API features, we need to initialize the client application:

a. Indexing Documents

The prepareIndex () function allows to store an arbitrary JSON document and make it searchable:

When running the test, be sure to declare the path.home variable, otherwise, there will be the following exception:

After running the Maven command: mvn clean install -Des.path.home = C: elastic, the JSON document will be stored with everyone as an index.

Note that any JSON Java library can be used to create and process documents. If you are not familiar with any of these, you can use the Elaticsearch helper to create your own JSON documents.

b. Querying Indexed Documents

Now that we have a searchable index JSON document, we can proceed and search using prepareSearch ():

The results returned by the actionGet () method are called Entrances, each time accessing a JSON document that matches a search request.

In this case, the resulting list contains all data stored in the cluster. Note that in this example, we are using the FastJson library to convert JSON String to Java objects.

c. Retrieving and Deleting Documents

The prepareGet () and prepareDelete () methods allow you to receive or delete a JSON document from the cluster using its id:

The syntax is quite simple, you just need to specify the index and the type value along with the id of the object.

5. QueryBuilders Examples

The QueryBuilders class provides many static methods that are used as dynamic matches to find specific items in the cluster. While using the prepareSearch () method to search for specific JSON documents in the cluster, we can use query builders to customize the search results.

This is a list of the most common uses for the QueryBuilders API. The matchAllQuery () method returns a QueryBuilder object that matches all documents in the cluster:

RangeQuery () matches documents that have the value of a field within a certain range:

Provide the field name – for example, FullName and the corresponding value – for example, John Doe, The matchQuery () method matches all documents with the exact field value:

We can also use the multiMatchQuery () method to build a multi-field version of the matching query:

6. Conclusion

In this quick article, we looked at how to use ElasticSearch’s Java API to perform some common features related to full-text search engines.

Source: Baeldung.

Share the news now

Source : Viblo