Elastic Search: How to set constant score for only one of the queries

Using ElasticSearch 2.3,

I have a query like:
{
"query" : {
"bool" : {
"should" : [{
"query_string" : {
"query" : "fullName:("guy")"
}
}, {
"query_string" : {
"query" : "nickName:("guy")"
}
}, {
"term" : {
"fullExactName" : "guy"
}
}
]
}
},
"sort" : [{
"_score" : {
"order" : "desc"
}
}, {
"fullExactName" : {
"order" : "asc"
}
}
]
}

How can I assign constant scores to records that match each of the query?

  • Assign all records matching nickName query a constant _score of 1
  • Assign all records matching fullName query a constant _score of 2
  • Assign all records matching fullExactName query a constant _score of 3

I need the scores to be equal so I can still sort them alphabetically.

Thanks!

You can wrap the queries with a constant_score query, which assigns a score based on the boost: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html

Note: scores will be combined if the document matches multiple parts of the query. If you want only one score to be added, you'll need to setup the various logical combinations.

E.g. +nickName NOT (fullName OR fullExactName), +fullName NOT (nickName OR fullExactName), etc. That way only one score is applied.

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