Elasticsearch(6.5) HIgh level java rest client Delete an index by name is not working
Asked Answered
E

1

2

I can delete a document by passing the index name , type and id like this-

DeleteRequest deleteRequest = new DeleteRequest(data.getIndexName(),data.getType(),data.getUniqueId());

    DeleteResponse deleteResponse = client.delete(deleteRequest);

But when I am trying to delete an index by giving the index name only like below- (According to this document)

DeleteRequest deleteRequest = new DeleteRequest(allData.getIndexName());

    DeleteResponse deleteResponse = client.delete(deleteRequest);

getting-

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;2: id is missing;

I have tried another way like this (reference)

DeleteIndexRequest request = new DeleteIndexRequest(allData.getIndexName());
    AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);

getting:

java.lang.NoSuchMethodError: 
at org.elasticsearch.client.IndicesClient.delete(IndicesClient.java:93) ~[elasticsearch-rest-high-level-client-6.5.4.jar:6.4.3]

I am using this dependency:

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

mvn dependency:tree

[INFO] \- org.elasticsearch.client:elasticsearch-rest-high-level- 
client:jar:6.5.4:compile
[INFO]    +- org.elasticsearch:elasticsearch:jar:6.4.3:compile
[INFO]    |  +- org.elasticsearch:elasticsearch-core:jar:6.4.3:compile
[INFO]    |  +- org.elasticsearch:elasticsearch-secure- 
sm:jar:6.4.3:compile
[INFO]    |  +- org.elasticsearch:elasticsearch-x- 
content:jar:6.4.3:compile
[INFO]    |  |  +- com.fasterxml.jackson.dataformat:jackson- 
dataformat-smile:jar:2.9.8:compile
[INFO]    |  |  +- com.fasterxml.jackson.dataformat:jackson- 
dataformat-yaml:jar:2.9.8:compile
[INFO]    |  |  \- com.fasterxml.jackson.dataformat:jackson- 
dataformat-cbor:jar:2.9.8:compile
[INFO]    |  +- org.apache.lucene:lucene-core:jar:7.4.0:compile
[INFO]    |  +- org.apache.lucene:lucene-analyzers- 
common:jar:7.4.0:compile
[INFO]    |  +- org.apache.lucene:lucene-backward- 
codecs:jar:7.4.0:compile
[INFO]    |  +- org.apache.lucene:lucene-grouping:jar:7.4.0:compile
[INFO]    |  +- org.apache.lucene:lucene-highlighter:jar:7.4.0:compile
[INFO]    |  +- org.apache.lucene:lucene-join:jar:7.4.0:compile

Here is my pom.xml

<?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 
http://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.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>search-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>search-service</name>
<description>Demo project for search-service</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-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version><!--$NO-MVN-MAN-VER$-->
    </dependency> 
    <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
       <version>2.8.2</version><!--$NO-MVN-MAN-VER$-->
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.7.0</version><!--$NO-MVN-MAN-VER$-->

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

Expression answered 2/4, 2019 at 8:56 Comment(13)
The second way is the right one. Weird it throws that error, though, as version 6.5.4 did have the delete method already.Syndrome
Seems like correct version in not loading. Check if some some dependency is overriding the version.Poultryman
@NishantSaini I have added the dependency above which I have used. And its showing one warning-"Overriding managed version 6.4.3 for elasticsearch-rest-high-level-client" in pom.xml. Is this the reason why delete index is not workingExpression
In the dependency tree check what version of elastic search (org.elasticsearch) is being used. This should be 6.5.4 or above.Poultryman
@NishantSaini I have added the mvn dependency:tree don't know why it is showing 6.4.3? I have only the 6.5.4 dependency in my pom.xmlExpression
Some other dependency might be indirectly overriding this. Can you add pom content?Poultryman
@NishantSaini I have added the pom.xmlExpression
@Syndrome could you please check again I have added some more details may be you can find somethingExpression
@Bran: Just a hit and trial solution: add this property to the pom <elasticsearch.version>6.5.4</elasticsearch.version> and add the elastic dependency as <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>Poultryman
@Expression is this solved?Poultryman
@NishantSaini No I tried a lot of things but failed.so at last I use java low level rest client to achieve the requirement.Expression
@Expression Have you tried making changes as per my last message?Poultryman
@Expression Check my answer. I would suggest you not to go with low level rest client.Poultryman
P
5

Spring-boot v2.1.2 release has a property defined for elasticsearch version as

<elasticsearch.version>6.4.3</elasticsearch.version>

and the following dependecy

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>

This is the reason for this in dependency tree

org.elasticsearch:elasticsearch:jar:6.4.3:compile

To force load elasticsearch 6.5.4 add the following property to your pom which will override the property defined in spring-boot-dependencies pom

<properties>
    <elasticsearch.version>6.5.4</elasticsearch.version>
</properties>
<dependencies>
    ...
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    ...
</dependencies>
Poultryman answered 5/4, 2019 at 6:33 Comment(1)
Yes It does the job.ThanksExpression

© 2022 - 2024 — McMap. All rights reserved.