Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using High-Level REST Client to Manage Elasticsearch

Tech May 16 1

This article demonstrates how to manage Elasticsearch using the High-Level REST Client. It covers connecting to a Elasticsearch cluster, creating indices, and checking their existence.

The following example shows how to establish a connection with basic authentication and perform operations like index creation:

import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.util.EntityUtils; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.client.*; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map;

public class ESTest {

public static void main(String[] args) throws IOException {
    String user = "admin";
    String pwd = "admin123";
    String host = "192.168.49.131";
    int portNum = 9200;

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(user, pwd));

    RestClientBuilder builder = RestClient.builder(new HttpHost(host, portNum));

    builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
            httpClientBuilder.disableAuthCaching();
            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }
    });
    RestClient restClient = builder.build();

    RestHighLevelClient highLevelClient = new RestHighLevelClient(builder);

    Request req = new Request(
            "GET",
            "_cat/indices?v");
    Response res = restClient.performRequest(req);
    HttpHost hostInfo = res.getHost();
    int statusCode = res.getStatusLine().getStatusCode();
    Header[] headers = res.getHeaders();
    String body = EntityUtils.toString(res.getEntity());

    String indexName = "es-demo-" + getCurrentDate();

    try {
        createIndex(highLevelClient, indexName);
    } catch (Exception e){
        e.printStackTrace();
    }


    restClient.close();
    highLevelClient.close();
}

/**
 * Checks if an index already exists
 *
 * @param client Elasticsearch client instance
 * @param indexName Name of the index to check
 * @return true if index exists, false otherwise
 * @throws Exception if operation fails
 */
public static boolean doesIndexExist(RestHighLevelClient client, String indexName) throws Exception {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    request.local(false);
    request.humanReadable(true);

    boolean exists = client.indices().exists(request);
    return exists;
}

/**
 * Creates an index with specified mappings
 *
 * @param client Elasticsearch client instance
 * @param indexName Name of the index to create
 * @param type Document type (deprecated in newer versions)
 * @param fields Field definitions for the index
 * @return true if index was created successfully
 * @throws Exception if operation fails
 */
public static boolean createIndex(RestHighLevelClient client, String indexName, String type,
                           Map<string object=""> fields) throws Exception {

    if (doesIndexExist(client, indexName)) {
        return true;
    }
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(Settings.builder().put("index.number_of_shards", 3)
            .put("index.number_of_replicas", 1));

    Map<string object=""> jsonMap = new HashMap<>();
    Map<string object=""> mapping = new HashMap<>();
    mapping.put("properties", fields);
    jsonMap.put(type, mapping);
    request.mapping(type, jsonMap);

    CreateIndexResponse createIndexResponse = client.indices().create(
            request, RequestOptions.DEFAULT);
    boolean acknowledged = createIndexResponse.isAcknowledged();
    return acknowledged;
}

/**
 * Creates an index using JSON string mapping
 *
 * @param client Elasticsearch client instance
 * @param indexName Name of the index to create
 * @return true if index was created successfully
 * @throws Exception if operation fails
 */
public static boolean createIndexWithJson(RestHighLevelClient client, String indexName)
                            throws Exception {
    if (doesIndexExist(client, indexName)) {
        return true;
    }

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(Settings.builder()
            .put("index.number_of_shards", 3)
            .put("index.number_of_replicas", 1)
    );
    request.mapping("_doc",
            "{\n" +
                    "  \"_doc\": {\n" +
                    "    \"properties\": {\n" +
                    "      \"message\": {\n" +
                    "        \"type\": \"text\"\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}",
            XContentType.JSON);
    CreateIndexResponse createIndexResponse = client.indices().create(
            request, RequestOptions.DEFAULT);
    boolean acknowledged = createIndexResponse.isAcknowledged();
    return acknowledged;
}

public static String getCurrentDate() {
    Date currentDate = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
    return formatter.format(currentDate);
}

}


</div>Maven dependencies required for this setup:

<div>```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-es</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.4.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.