# KNN search with Function score or Scoring script

**URL:** https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432
**Category:** Elasticsearch
**Tags:** vector-search
**Created:** [March 28, 2024, 11:01pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432 "2024-03-28T23:01:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yevgeny\_Yurgenson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yevgeny_yurgenson/32/126132_2.png) [@Yevgeny\_Yurgenson](https://discuss.elastic.co/u/Yevgeny_Yurgenson)
#### Post date: [March 28, 2024, 11:01pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432/1 "2024-03-28T23:01:38Z")

</div>

I would like to incorporate a decay function based off of a time field attached to a vector when conducting a KNN search. I am using the Kotlin client. WIth a regular query, this would be done through the function score builder, but knn does not seem to expose any such API. Is this possible?

---

<div class="post-metadata">

### Author: ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)
#### Post date: [March 29, 2024, 12:10pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432/2 "2024-03-29T12:10:59Z")

</div>

Hi there @Yevgeny_Yurgenson - if you're using Elasticsearch 8.12 or higher, you could check out the [knn query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html). This allows you to search with `knn` just as with any query type in our query DSL and you should be able to combine this just like any other query type - in boolean queries, with rescores, etc.

---

<div class="post-metadata">

### Author: ![Yevgeny\_Yurgenson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yevgeny_yurgenson/32/126132_2.png) [@Yevgeny\_Yurgenson](https://discuss.elastic.co/u/Yevgeny_Yurgenson)
#### Post date: [March 29, 2024, 3:15pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432/3 "2024-03-29T15:15:15Z")

</div>

The goal is to rescore the KNN vectors returned from the KNN search based on some other field parameter. For example, if i have two vectors that both have similarity scores of .8, but one of them is a year old, i would like it to be penalized by .4, so that my resulting scores are .8 and .4.

This is trivial to do with a regular query, but KNN does not seem to have a rescoring ability, nor does it seem to have the ability to attach a custom scoring script. If I am mistaken, could you please demonstrate code that would accomplish this?

---

<div class="post-metadata">

### Author: ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)
#### Post date: [March 29, 2024, 4:59pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432/4 "2024-03-29T16:59:45Z")

</div>

Hi, the KNN query that I linked above is a different syntax than a regular top level knn search. This is an actual query included in the regular query structure. Please give that a try and if you are still running into difficulties please provide an example that we can reproduce. Thank you.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 26, 2024, 4:59pm UTC](https://discuss.elastic.co/t/knn-search-with-function-score-or-scoring-script/356432/5 "2024-04-26T16:59:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
