If we have 10,000 documents with the same score, but we limit the search to 1,000, is there a way to give more weight to newer documents so the newer 1,000 show up?
CodePudding user response:
If all the documents have the same score then the most straightforward way to go is just sorting by creation date:
https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html
If you want to add score on top the query score you can use a distance query on the creation date field.
PUT /items
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"creation_date": {
"type": "date"
}
}
}
}
PUT /items/_doc/1?refresh
{
"name" : "chocolate",
"production_date": "2018-02-01",
"location": [-71.34, 41.12]
}
PUT /items/_doc/2?refresh
{
"name" : "chocolate",
"creation_date": "2018-01-01"
}
PUT /items/_doc/3?refresh
{
"name" : "chocolate",
"creation_date": "2017-12-01"
}
GET /items/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "chocolate"
}
},
"should": {
"distance_feature": {
"field": "creation_date",
"pivot": "7d",
"origin": "now"
}
}
}
}
}
origin
will define the starting point from where you want to give more weight to the documents which are close, in the example the closest to "now" the document is, the weight it will have.
pivot
distance of the origin the document will receive half of the score.