问题
I have a very simple query :
match: {
field => {
boost: 4,
query: term,
fuzziness: 'AUTO',
}
}
Composed with several (about 10) others queries most of them using constant_score. The problem is that on specific terms, my query has a too big score that cancel all others queries results.
Here is a part of the explain :
"details" => [
[0] {
"value" => 63.656006,
"description" => "sum of:",
"details" => [
[0] {
"value" => 63.656006,
"description" => "weight(title.de:kandinsky in 1694239) [PerFieldSimilarity], result of:",
"details" => [
[0] {
"value" => 63.656006,
"description" => "score(doc=1694239,freq=1.0 = termFreq=1.0\n), product of:",
"details" => [
[0] {
"value" => 4.0,
"description" => "boost",
"details" => []
},
[1] {
"value" => 11.3820715,
"description" => "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
[...]
Has you can see, I have a score of 11.38 due to the IDF. My others queries (with scores between 1 and 3) are totally useless.
My question is :
How can I set a max possible score for a query ?
Or, even better, can I set a range of score for my query ?
I would like to avoid a constant_score query for this field, I need a few TF/IDF and score notion for this field but not that strong.
I tried this :
function_score: {
query: { match: {
field => term,
}},
score_mode: :avg,
script_score: {
script: {
inline: "4 * (1 + Math.log(2 + _score))",
}
},
}
It's better but it can still perform a very high score on certain cases.
回答1:
have u tried using Function score query ? Here is the link for the same https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
回答2:
Finaly I used a function score with a script score using a 1 - (1/x)
function in script_score
GET _search
{
"query": {
"function_score": {
"query": {
"match": {
"postgresql.log.message": "alter"
}
},
"script_score" : {
"script" : {
"params": {
"max_score": 5
},
"source": "params.max_score * (1 - 1 / _score)"
}
}
}
}
}
This way, I will have a score between 0 and nearly 5 (max_score).
You can try it here with the word alter
(score 3.9150627) or alter table pgbench_branches add primary key (bid)
(score 4.8539715).
You can adapt the 1 - (1/x)
function to approach the asymptote faster.
来源:https://stackoverflow.com/questions/44965601/is-there-a-way-to-set-a-score-range-or-a-max-score-for-a-query