Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] multi-search is not correctly parsed #57

Closed
NikkyAI opened this issue Mar 4, 2023 · 0 comments
Closed

[BUG] multi-search is not correctly parsed #57

NikkyAI opened this issue Mar 4, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@NikkyAI
Copy link

NikkyAI commented Mar 4, 2023

Describe the bug

A clear and concise description of what the bug is.

elasticsearch responds to <target?_msearch with a json object, but kt-search is trying to parse nd-json

also.. kt-search is currently dropping the content-header even if it is set using rawBody(body, contentType)

To Fix

the response needs to be parsed with

@Serializable
data class MultiSearchResponse(
    val took: Long,
    val responses: List<SearchResponse>
)

suspend fun SearchClient.msearch(
    target: String?=null,
    body: String?,
    allowNoIndices: Boolean? = null,
    cssMinimizeRoundtrips: Boolean? = null,
    expandWildcards: ExpandWildCards? = null,
    ignoreThrottled: Boolean? = null,
    ignoreUnavailable: Boolean? = null,
    maxConcurrentSearches: Int? = null,
    maxConcurrentShardRequests: Int? = null,
    preFilterShardSize: Int? = null,
    routing: String? = null,
    searchType: SearchType? = null,
    typedKeys: Boolean? = null,
): MultiSearchResponse {
    return restClient.post {
        path(*listOfNotNull(target.takeIf { !it.isNullOrBlank() }, "_msearch").toTypedArray())

        parameter("allow_no_indices", allowNoIndices)
        parameter("ccs_minimize_roundtrips", cssMinimizeRoundtrips)
        parameter("expand_wildcards", expandWildcards)
        parameter("ignore_throttled", ignoreThrottled)
        parameter("ignore_unavailable", ignoreUnavailable)
        parameter("max_concurrent_searches", maxConcurrentSearches)
        parameter("max_concurrent_shard_requests", maxConcurrentShardRequests)
        parameter("max_concurrent_shard_requests", maxConcurrentShardRequests)
        parameter("pre_filter_shard_size", preFilterShardSize)
        parameter("routing", routing)
        parameter("search_type", searchType)
        parameter("typed_keys", typedKeys)

        body?.let {
            rawBody(
                body = body,
                contentType = ContentType("application", "x-ndjson")
            )
        }
    }.parse(MultiSearchResponse.serializer())
}

and to fix the content-type not being passed along (although it works without it anyways.. just not quite like the elasticsearch docs say)

suspend fun RestClient.get(block: SearchAPIRequest.() -> Unit): Result<RestResponse.Status2XX> {
    val request = SearchAPIRequest()
    block.invoke(request)
    return doRequest(
        pathComponents = listOf("/" + request.pathComponents.joinToString("/")),
        payload = request.body,
        contentType = request.contentType,
        httpMethod = HttpMethod.Get,
        parameters = request.parameters,
        headers = request.headers
    ).asResult()
}

Expected behavior

A clear and concise description of what you expected to happen.

Your context

  • version: 2.0.0-RC-6

Will you be able to help with a pull request?

yes, ofc

@NikkyAI NikkyAI added the bug Something isn't working label Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant