jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
107 stars 23 forks source link

[BUG] Searching over multiple indices + ids query not working #21

Closed lengerad closed 2 years ago

lengerad commented 2 years ago

Describe the bug In advance sorry for bringing these two things as one issue but I don't want to spam it here with questions that might not be "bugs" but more of wrong usage of the library.

I would like to be able query multiple indices - at first I was looking for some specific method but I didn't find it. Then I thought it would be enough to pass it as "index1, index2" string but that throws exception "no such index [ index2]". That looks like it somehow might support multiple indices (as it's printed as array) search and I am using it wrong but even when a dig a bit in a search-api.kt class I wasn't successful to find how it should be done or if it's supported at all?

  1. I was used to use the ids with es-kotlin-client but after migration it somehow doesn't work. If I do term query with exact value (single one) it retrieves document as it should. But once I try to use ids query where I convert List<String> to typedArray and then I use the * spread operator it's not working and I receive zero documents for the same document id. Again I tried to dig a bit in term-level-queries.kt class and I suspect that it might be because block is set to null by default? and then it's invoked even tho it seems that here:

init {
        this["values"] = values
        block?.invoke(this)
    }

it's initialised.

To Reproduce Steps to reproduce the behavior:

val response = client.search("index, index2") {
            query = ids(*ids.toTypedArray())
        }.parseHits<Document>()

2.

val documents = listOf("test")
val response = client.search("default") {
            query = ids(*documents.toTypedArray())
        }.parseHits<Document>()

Expected behavior

Possible usage of search for multiple indices. Ids SearchDSL should provide possibility to search via multiple ids.

Your context

Kotlin version: 1.7.10 Search client version: search-client:1.99.9 Version of Elasticsearch: "number" : "7.17.1",

Will you be able to help with a pull request?

Optional of course, but do let me know if you plan to do work.

jillesvangurp commented 2 years ago

It should be possible to get this working one way or another.

Can now use the logging parameter to debug what request the client actually sends. There probably is a bug somwhere. Maybe the comma is being escaped or something.

Otherwise, you can experiment with using the restClient directly. Something like this should be the solution. Just look at the search function; it should be doing something similar.

client.restClient.post {
  path("index1,index2","_search")
  body=SearchDsl().apply {
  }.json()
}
jillesvangurp commented 2 years ago

Actually found the issue; serialization issue. I'll push a fix shortly.

lengerad commented 2 years ago

Thanks for fixing up the ids, I found out that my question 1. related to the multiple indices it was silly fault that it shouldn't contain ` space between,and next index. So that was my fault 🙈. Thanks for helping me out, if anyone would read this use with the same issue just check that the structure is:index,index2`

lengerad commented 2 years ago

New release pulled and tested and it's working! 🏁