Reputation: 115
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)
Upvotes: 0
Views: 702
Reputation: 115
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)
Upvotes: 1