skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search