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

introduce analyze API #132

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.jillesvangurp.ktsearch

import com.jillesvangurp.jsondsl.JsonDsl
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


@Suppress("unused")
class AnalyzeRequest : JsonDsl() {
var analyzer by property<String>()
var attributes by property<List<String>>()
var charFilter by property<List<String>>()
var field by property<String>()
var filter by property<List<String>>()
var normalizer by property<String>()
var text by property<List<String>>()
var tokenizer by property<String>()
}


@Serializable
data class AnalyzeResponse(
@SerialName("tokens") val tokens: List<AnalyzeToken>,
)

@Serializable
data class AnalyzeToken(
@SerialName("token") val token: String,
@SerialName("start_offset") val startOffset: Int,
@SerialName("end_offset") val endOffset: Int,
@SerialName("type") val type: String,
@SerialName("position") val position: Int,
)

suspend fun SearchClient.analyze(
target: String? = null,
block: AnalyzeRequest.() -> Unit
): AnalyzeResponse {
return restClient.post {
path(*listOfNotNull(target.takeIf { !it.isNullOrBlank() }, "_analyze").toTypedArray())
json(AnalyzeRequest().apply(block))
}.parse(AnalyzeResponse.serializer())
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jillesvangurp.ktsearch

import io.kotest.matchers.shouldBe
import kotlin.test.Test

class AnalyzeTest : SearchTestBase() {

@Test
fun shouldAnalyzeQuery() = coRun {
val resp = client.analyze {
text = listOf("foo bar")
analyzer = "standard"
}

resp.tokens.size shouldBe 2
}
}

Loading