time boosting example

This commit is contained in:
Evgeniya Sukhodolskaya
2025-08-20 13:36:01 +02:00
committed by Jenny
parent 020024aea2
commit 13ed6b91b7
4 changed files with 74 additions and 0 deletions
@@ -192,6 +192,20 @@ In this case we use a **gauss_decay** function.
{{< code-snippet path="/documentation/headless/snippets/query-points/score-boost-closer-to-user/" >}}
### Time-based score boosting
Or combine the score with how close the point's timestamp is to a target datetime (for example, the time of search).
If each point has a datetime field in its payload, f.e. the time the point was uploaded or last updated, we can calculate the time difference between this value and the target (in seconds).
Using an exponential decay function, we can convert this time difference into a value between 0 and 1, then add it to the original score to prioritize results closer in time to the target.
`score = score + exp_decay(target_time - x_time)`
In this case, we use an **exp_decay** function.
{{< code-snippet path="/documentation/headless/snippets/query-points/score-boost-time/" >}}
For all decay functions, there are these parameters available
| Parameter | Default | Description |
@@ -0,0 +1 @@
This code snippet applies exponential decay to boost the relevance of search results based on a datetime field in the payload. Items closer in time to a specified `target` datetime receive higher scores, with relevance decreasing exponentially and reaching a specified 0.1 `midpoint` of relevance after a defined time `scale` period of 1 week.
@@ -0,0 +1,28 @@
```http
POST /collections/{collection_name}/points/query
{
"prefetch": {
"query": [0.2, 0.8, ...], // <-- dense vector
"limit": 50
}
"query": {
"formula": {
"sum": [
"$score", // the final score = score + exp_decay(target_time - x_time)
{
"exp_decay": {
"x": {
"datetime_key": "upload_time" // payload key
},
"target": {
"datetime": "2025-08-04T00:00:00Z" // target time, for example, time of the search
},
"scale": 86400, // 1 week in seconds
"midpoint": 0.1 // 0.1 output with deviation on `scale` (1 week) from `target`
}
}
]
}
}
}
```
@@ -0,0 +1,31 @@
```python
from qdrant_client import models
time_boosted = client.query_points(
collection_name="{collection_name}",
prefetch=models.Prefetch(
query=[0.2, 0.8, ...], # <-- dense vector
limit=50
),
query=models.FormulaQuery(
formula=models.SumExpression(
sum=[
"$score", # the final score = score + exp_decay(target_time - x_time)
models.ExpDecayExpression(
exp_decay=models.DecayParamsExpression(
x=models.DatetimeKeyExpression(
datetime_key="upload_time" # payload key
),
target=models.DatetimeExpression(
datetime="2025-08-04T00:00:00Z" # target time, for example, time of the search
),
scale=86400, # 1 week in seconds
midpoint=0.1 # 0.1 output with deviation on `scale` (1 week) from `target`
)
)
]
)
)
)
```