如何在Kotlin中使用Java API发送JSON数据的POST请求到Elasticsearch 8.x?

huangapple go评论54阅读模式
英文:

How to use the Java API in Kotlin to send a POST request with JSON data to Elasticsearch 8.x?

问题

我想使用Kotlin中的Java API向Elasticsearch 8.x发送带有JSON数据的POST请求。我已经设置了我的Elasticsearch客户端如下:

val restClient = RestClient.builder(
    HttpHost("localhost", 9200)
).build()

// 创建具有Jackson映射器的传输
val transport: ElasticsearchTransport = RestClientTransport(
    restClient, JacksonJsonpMapper()
)

// 创建API客户端
val client = ElasticsearchClient(transport)

我想使用POST请求将整个JSON文档发送到Elasticsearch,但不确定如何操作。我看到了一些示例使用IndexRequest类,因为据我所读,这是8.x版本应该使用Java API的正确路径。

是否有人能够提供如何使用Java API将带有JSON数据的POST请求发送到Elasticsearch的示例?任何帮助将不胜感激!

英文:

I'm trying to use the Java API in Kotlin to send a POST request with JSON data to Elasticsearch 8.x. I've set up my Elasticsearch client as follows:

val restClient = RestClient.builder(
    HttpHost("localhost", 9200)
).build()

// Create the transport with a Jackson mapper
val transport: ElasticsearchTransport = RestClientTransport(
    restClient, JacksonJsonpMapper()
)

// And create the API client
val client = ElasticsearchClient(transport)

I would like to send a whole JSON document to Elasticsearch using the POST request, but I'm not sure how to do it. I've seen examples using the IndexRequest class because as far as I've read is the right path for 8.x version that should use java api

Could someone provide an example of how to send a POST request with JSON data to Elasticsearch using the Java API? Any help would be greatly appreciated!

答案1

得分: 0

from the docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/indexing.html

you have examples of how to index single docs using the fluent DSL, classic builders, async client, and - as you requested - using raw JSON data

英文:

from the docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/indexing.html

you have examples of how to index single docs using the fluent DSL, classic builders, async client, and - as you requested - using raw JSON data

答案2

得分: 0

已解决:此问题是由Android引起的。
在Android上,在处理UI的主线程上进行网络调用是被禁止的,因为它可能导致应用程序无响应。

您应该从另一个线程中执行这些调用,或者使用异步版本的RestClient.performRequestAsync()

异步版本的链接:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.7/java-rest-low-usage-requests.html

英文:

solved: This problem is caused by android
On Android making network calls on the main thread that handles the UI is forbidden as it can make the application unresponsive

You should do these calls from another thread, or use the async version RestClient.performRequestAsync().

link for the async version: https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.7/java-rest-low-usage-requests.html

huangapple
  • 本文由 发表于 2023年3月4日 01:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630379-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定