简体   繁体   English

使用 RestClient 从 Scala 中的 elasticsearch 中删除文档

[英]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.

相关问题 使用RestClient获取DELETE和SCROLLAPI的Elasticsearch客户端 - Obtain the elasticsearch client using RestClient for DELETE and SCROLLAPI 使用Java RestClient API从Elastic Search处理多个文档 - Handling multiple documents from Elastic Search using Java RestClient API 使用 Jest 在 Elasticsearch 中删除没有父级的子文档 - Delete child documents without parent in Elasticsearch using Jest 使用 ElasticsearchOperations 从索引中删除所有文档 - Delete all documents from index using ElasticsearchOperations 使用 GluonhqConnect.Provider.RestClient 查询 ElasticSearch 正文 - Query with ElasticSearch body using GluonhqConnect.Provider.RestClient 如何使用Spring Data从Elasticsearch中读取文档? - How to read documents from elasticsearch using Spring Data? 从 ElasticSearch 中的索引中删除多个文档,其值为项目之一 - 7.8 版 - Delete multiple documents from an index in ElasticSearch with value of one of items - 7.8 version ElasticSearch JavaAPI RestClient未给出响应 - ElasticSearch JavaAPI RestClient not giving response Elasticsearch RestClient 连接由对等方重置 - Elasticsearch RestClient Connection reset by peer 使用Elasticsearch的RestClient时如何解决“对等连接重置” - How to get around “connection reset by peer” when using Elasticsearch's RestClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM