I am using elasticsearch and python for text searches in my application. I have a use case where, I have to boost a score of the result without decreasing the result count for a field which consists of array, array could be empty or could consists of n number of elements
Below is sample list of documents
[
{
"movie_name": "top gun abc",
"movie_industry": "hollywood",
"description": "xyz abc lmn pqr",
"relased_year": 2022,
"genres": [
"thriller",
"action"
]
},
{
"movie_name": "Indiana Jones: Raiders of the Lost Ark",
"movie_industry": "hollywood",
"description": "xyz abc lmn pqr abc, zyx abc",
"relased_year": 1981,
"genres": [
"action adventure"
]
},
{
"movie_name": "The Silence of the Lambs",
"movie_industry": "hollywood",
"description": "xyz abc lmn pqr abc",
"relased_year": 1991,
"genres": [
"pshychological thriller",
"pshychological horror",
"horror",
"thriller"
]
},
{
"movie_name": "Gone Girl abc",
"movie_industry": "hollywood",
"description": "xyz abc lmn pqr abc",
"relased_year": 2014,
"genres": []
}
]
What I want is if users will search for search term like 'abc', then it should give result according to relevance(most number of search term found in documents) which i was able to do with
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "abc",
"fields": [
"movie_name^10",
"description"
]
}
},
"functions": [
]
}
},
"track_total_hits": true
}
Now after this. I wanted to boost score if user apply sort by dropdown which should sort the result (without changing the result count), like if users selects thriller then it should sort by thriller boosting the score, I was able to do it by
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "abc",
"fields": [
"movie_name^10",
"description"
]
}
},
"functions": [
{
"filter": {
"match_phrase": {
"genres": "thriller"
}
},
"weight": 100
}
]
}
},
"track_total_hits": true
}
Now what i am not able to do is, sort documents which contains genres=thriller
but score of the documents should be higher for documents containing genre and the number of elements inside the genres array
What i mean by that is, score of the document is not only decided by genres which matches the sort by term but also length of the genres array consisting the matched term.
I am using
elasticsearch verison 7.16.3
how do i achieve this in efficient way
Thanks in advance!