0

I am trying to delete documents from an index whose age(field of the index) is greater than 50. So basically i m trying to write a range query. I have successfully connected to ElasticSearch from Scala and also i am able delete an entire index. But i am not able to write a range query. Can someone please help me to write a range query for deletion of documents from an index in scala. Below is my code snippet to delete an entire index. I have seen many examples in Java but I NEED THE SOLUTION IN SCALA.

import com.sksamuel.elastic4s.delete.DeleteByQueryRequest
import org.apache.http.HttpHost
import org.apache.http.auth.{AuthScope, Credentials, UsernamePasswordCredentials}
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder

System.setProperty("javax.net.ssl.trustStore", "path of certificate")
System.setProperty("javax.net.ssl.trustStorePassword", "password")

val  credentials = new UsernamePasswordCredentials("username", "password");
    val credentialsProvider:CredentialsProvider  = new BasicCredentialsProvider
    credentialsProvider.setCredentials(AuthScope.ANY, credentials)

val client = RestClient.builder(new HttpHost("host", 9200,"https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
      override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
    }).build

// Delete entire index
val request = new Request("DELETE", "/products")
val response = client.performRequest(request)

1 Answer 1

1

I found the solution. Below is my code snippet.

import org.apache.http.HttpHost
import org.apache.http.auth.{AuthScope, Credentials, UsernamePasswordCredentials}
import org.elasticsearch.client._
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
import org.elasticsearch.index.query.{QueryBuilders, RangeQueryBuilder}
import org.elasticsearch.index.reindex.DeleteByQueryRequest

System.setProperty("javax.net.ssl.trustStore", "Certificate path")
System.setProperty("javax.net.ssl.trustStorePassword", "password")

val credentials = new UsernamePasswordCredentials("username", "password");
val credentialsProvider:CredentialsProvider  = new BasicCredentialsProvider
credentialsProvider.setCredentials(AuthScope.ANY, credentials)

val builder = RestClient.builder(new HttpHost("host name", 9200,"https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
      override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
    })

var client = new RestHighLevelClient(builder)

val queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("age").gte("50"))

val deleteRequest = new DeleteByQueryRequest("index_name").setQuery(queryBuilder)

client.deleteByQuery(deleteRequest, RequestOptions.DEFAULT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.