Consider I have the following function score query with an idea to find all animals
by tags
, and score them by mustHaveTags
occurances:
POST /animals/_search
{
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"filter": [
{
"terms": {
"tags": ["Monkey", "Lion"]
}
}
]
}
},
"functions": [
{
"filter": {
"term": {
"mustHaveTags.keyword": {"value": "Monkey"}
}
},
"weight": 1
},
{
"filter": {
"term": {
"mustHaveTags.keyword": {"value": "Lion"}
}
},
"weight": 1
}
],
"score_mode": "sum",
"boost_mode": "sum"
}
}
}
That returns me something like this when executing from Kibana Console:
{
"hits": {
"total": {
"value": 84,
"relation": "eq"
},
"max_score": 2, <- !!!
"hits": [...]
}
}
There are some hits with score = 2
that apply to all animals that have all mustHaveTags = ["Lion", "Monkey"]
However, when migrating this query to the native Elasticsearch Java Client (I'm using Kotlin, but I don't think it matters):
val functionScoreQuery = QueryBuilders.functionScore {
it
.query(
QueryBuilders.bool { b ->
b.filter { fb ->
fb.terms {
TermsQuery.Builder()
.field("tags")
.terms { tm -> tm.value(tags.map { tag -> FieldValue.of(tag) }) }
}
}
}
)
.functions(tags.map { tag ->
FunctionScore.Builder()
.filter(QueryBuilders.term { t -> t.field("mustHaveTags.keyword").value(tag) })
.weight(1.0)
.build()
})
.boostMode(FunctionBoostMode.Sum)
.scoreMode(FunctionScoreMode.Sum)
}
val searchResponse: SearchResponse<Animal> = client.search(
SearchRequest.of { sr ->
sr.index("animals")
.query(functionScoreQuery)
.size(30)
}, Animal::class.java
)
That produces the following (similar!) query toString()
in the debugger:
{
"query": {
"function_score": {
"boost_mode": "sum",
"functions": [
{
"filter": {
"term": {
"mustHaveTags.keyword": {
"value": "Monkey"
}
}
},
"weight": 1
},
{
"filter": {
"term": {
"mustHaveTags.keyword": {
"value": "Lion"
}
}
},
"weight": 1
}
],
"query": {
"bool": {
"filter": [
{
"terms": {
"tags": [
"Lion",
"Monkey"
]
}
}
]
}
},
"score_mode": "sum"
}
}
}
Returns the same amount of hits but with score = 1
at max. and doesn't give any more scores to the documents that have more than 1 mustHaveTags
occurrences. Any ideas why?