[英]Delete documents from elasticsearch in Scala using RestClient
I am trying to delete documents from an index whose age(field of the index) is greater than 50. So basically im trying to write a range query.我正在尝试从年龄(索引的字段)大于 50 的索引中删除文档。所以基本上我正在尝试编写范围查询。 I have successfully connected to ElasticSearch from Scala and also i am able delete an entire index.我已经从 Scala 成功连接到 ElasticSearch 并且我还可以删除整个索引。 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.有人可以帮我写一个范围查询,以从 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.我在 Java 中看到了很多示例,但我需要 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)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.