I'm trying to implement a custom score based on how important a field is.
However I need to compare across multiple indices of different document types. These documents have different fields with different importances. I need the scores from these results to be comparable and therefore want to ignore TF/IDF and score normalisation.
So if a search query matches 2 important fields and 1 less important field it's score should be twice the important score plus the less important score:
(8* (1+1)) + (3*(1)) = 19
The result I am getting is a score of 11. As the query below seems to ignore the inner function score and computes:
(8*1) + (3*1).
The score explanation is also below which seems to show it's ignoring the inner function_score and just giving it a constant score of 1(this is what I want to stop happening).
I have tried not nesting function scores and using simple should queries as well as trying boost_factor instead of 'weight' and giving matched fields a constant score all of which have the same outcome.
Also rather than applying a constant weight to multiply by I'd like to use a script_score to compute the outer result. However the '_score' that gets passed up is not the score that I have just computed but the original search score. Is there a field I can use other than '_score' within a script_score to get this?
Thanks in advance!
Query
"query": {
"function_score": {
"functions": [
{
"weight": 8.0,
"filter": {
"fquery": {
"query": {
"function_score": {
"functions": [
{
"weight": 1.0,
"filter": {
"fquery": {
"query": {
"query_string": {
"query": "match*",
"fields": [
"ImportantField1"
],
"default_operator": "and",
"analyzer": "english",
"analyze_wildcard": true
}
}
}
}
},
{
"weight": 1.0,
"filter": {
"fquery": {
"query": {
"query_string": {
"query": "match*",
"fields": [
"ImportantField2"
],
"default_operator": "and",
"analyzer": "english",
"analyze_wildcard": true
}
}
}
} // More field queries that don't match omitted for clarity
}
],
"score_mode": "sum",
"boost_mode": "replace"
}
}
}
}
},
{
"weight": 3.0,
"filter": {
"fquery": {
"query": {
"function_score": {
"functions": [
{
"weight": 1.0,
"filter": {
"fquery": {
"query": {
"query_string": {
"query": "match*",
"fields": [
"LessImportantField"
],
"default_operator": "and",
"analyzer": "english",
"analyze_wildcard": true
}
}
}
}
}// More field queries that don't match omitted for clarity
],
"query": {
"match_all": {}
},
"score_mode": "sum",
"boost_mode": "replace"
}
}
}
}
}
],
"query": {
"match_all": {} // Filtering done here, omitted for clarity
}
},
"score_mode": "sum",
"boost_mode": "replace"
}
}
Score Explanation
"_explanation": {
"value": 11,
"description": "function score, product of:",
"details": [
{
"value": 11,
"description": "Math.min of",
"details": [
{
"value": 11,
"description": "function score, score mode [sum]",
"details": [
{
"value": 8,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "match filter: QueryWrapperFilter(function score (ConstantScore(*:*), functions: [{filter(QueryWrapperFilter(ImportantField1:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@64b3fd0e]}{filter(QueryWrapperFilter(ImportantField2:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@38ed4b5c]}]))"
},
{
"value": 8,
"description": "product of:",
"details": [
{
"value": 1,
"description": "constant score 1.0 - no function provided"
},
{
"value": 8,
"description": "weight"
}
]
}
]
},
{
"value": 3,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "match filter: QueryWrapperFilter(function score (ConstantScore(*:*), functions: [{filter(QueryWrapperFilter(LessImportantField:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@3ce99ebf]}]))"
},
{
"value": 3,
"description": "product of:",
"details": [
{
"value": 1,
"description": "constant score 1.0 - no function provided"
},
{
"value": 3,
"description": "weight"
}
]
}
]
}
]
},
{
"value": 3.4028235e+38,
"description": "maxBoost"
}
]
},
{
"value": 1,
"description": "queryBoost"
}
]
}