jillesvangurp/kt-search

[FEAT] Make it possible to create/update an index using raw JSON to define the mappings and settings

Closed this issue · 3 comments

Describe the enhancement

Make it possible to create/update an index using raw JSON to define the mappings and settings

Why is this needed?

It would be really convenient to be able to store serialized JSON snapshots of the mapping state (client.getIndex(<INDEX_NAME>).toString()) and then use those snapshots to generate a IndexSettingsAndMappingsDSL object that could be used to roll back to the previous state.

How do you think it should be done?

There's a couple of options I can think of:

  1. Add a createIndex signature that takes a rawJson defining the index mappings + settings
  2. Add a deserialize method to JsonDsl (I'm not very familiar with Kotlin/JSON serialization, so I'm unsure of how much work it would be to enable the classes that inherit from JsonDsl to deserialize)

Will you be able to help with a pull request?

I should be able to help.

Hi, I'm on @gueletk-affirm 's team.

I've taken a try at setting up a quick extension on our side for Option 1):

import com.jillesvangurp.ktsearch.IndexCreateResponse
import com.jillesvangurp.ktsearch.SearchClient
import com.jillesvangurp.ktsearch.parse
import com.jillesvangurp.ktsearch.put
import kotlin.time.Duration

suspend fun SearchClient.createIndex(
    name: String,
    mappingAsRawJson: String,
    waitForActiveShards: Int? = null,
    masterTimeOut: Duration? = null,
    timeout: Duration? = null,
    extraParameters: Map<String, String>? = null
    ): IndexCreateResponse {
    return restClient.put {
        path(name)

        parameter("wait_for_active_shards", waitForActiveShards)
        parameter("master_timeout", masterTimeOut)
        parameter("timeout", timeout)
        parameters(extraParameters)
        rawBody(mappingAsRawJson)
    }.parse(IndexCreateResponse.serializer(), json)
}

I'm adding something like @gillianchesnais suggested.

Feel free to create pull requests for things like this in the future.

fixed