I have the following query to use with Elasticsearch.
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"script_score": {
"script": {
"source": "cosineSimilarity(params.query_vector, 'vector') + 1",
"params": {
"query_vector": [/* Your query vector here */]
}
}
}
}
}
}
I am trying to convert this query into kotlin with the new Java API. Unfortunately, I am unable to find documentation on how to convert this query. I have tried multiple ways but to no avail.
I have tried the following from chatGPT but am unable to get it to work.
fun main() {
val queryVector = /* Your query vector here */
val scriptScoreFunction = ScriptScoreFunction(
Script("painless", "cosineSimilarity(params.query_vector, 'vector') + 1"),
mapOf("query_vector" to queryVector)
)
val functionScoreQuery = FunctionScoreQuery(
MatchAllQuery(),
listOf(ScriptScoreFunctionBuilder(scriptScoreFunction))
).scoreMode(ScoreMode.SUM)
val searchRequest = SearchRequest(Index("_all")).apply {
source(SearchSourceBuilder().apply {
query(functionScoreQuery)
})
}
println(searchRequest.source().toString())
}
Most resources are also using the older elasticsearch library but I have to use co.elastic.clients library which is the latest api for java