Reputation: 697
I have posts
index and i want to
a) Filter out all the posts by date i.e. return posts only created before the given date
b) Apply function_score
to the results.
I came up with this query which looks kinda like this:
get posts/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"filter": [
{
"range": {
"created_at": {
"lte": "2020-09-12T17:23:00Z"
}
}
}
]
}
},
"functions": [
{
"field_value_factor": {
"field": "likes",
"factor": 0.1,
"modifier": "ln1p",
"missing": 1
}
}
],
"score_mode": "sum"
}
},
"from": 0,
"size": 10
}
However the issue is that elastic search does NOT apply score to the results, all documents have 0.0
score. I can "trick" ES by moving filter into the functions, and then it does work, but i feel like it's not optimal, any idea why the query above won't return scores?
Example of a "cheat" query which DOES calculate scores for every document:
get posts/_search
{
"query": {
"function_score": {
"functions": [
{
"filter": {
"range": {
"created_at": {
"lte": "2020-09-12T17:22:58Z"
}
}
},
"weight": 100
},
{
"gauss": {
"created_at": {
"origin": "2020-09-12T17:22:58Z",
"scale": "1h",
"decay": 0.95
}
}
},
{
"field_value_factor": {
"field": "likes",
"factor": 0.1,
"modifier": "ln1p",
"missing": 1
}
}
]
}
},
"from": 0,
"size": 10
}
Upvotes: 0
Views: 2204
Reputation: 2089
Filter does not influence score. it's just 0 OR 1. To be able to influence a score with a function_score query you have to use a function that calculate score (match, Match_phrase, geo query etc...) You can have more details on context in the documentation
The field_value_factor influence existing score but in your first query you don't have any scoring at this stage, as it is nearly the same than ordering on like quantity.
In your second query you calculate a score which depends of the recentness of your docs, then influence it with likes. That's why it works.
Upvotes: 1