Unable to get solution for the following requirement. Can anyone suggest on this please ?
- I have a ES query that searches for 4 fields in my index and provides highlighted result. But i want to score the highlighted fields of every single document.
- Although i'm able to get Score for every document but i want to fetch score of highlighted fields of each document.
Actuall scenario why i need this
i have 4 field in my index that are title, description, subtitle, chapter and I'm performing search operation in all these fields so title is fixed which i will show anyhow in frontend. Then from other 3 fields i will check which is more relevant to the search keyword and i will show that because i will show only one field from th 3 fields along with title in frontend.
Solutions Tried
- Tried using explain=true but its very complex and i didn't get any key to fetch score of each fields.
- Tried sub_searches but didn't worked for me.
Here is my current query
$filesQuery['body'] = [
'size' => 5,
'query' => [
'function_score' => [
'query' => [
'bool' => [
'must' => [
[
'bool' => [
'should' => [
[
'match' => [
'title' => [
'query' => $request['query'],
'boost' => 4
],
],
],
[
'wildcard' => [
'title' => [
'value' => '*' . $request['query'] . '*',
],
],
],
[
'match' => [
'description' => [
'query' => $request['query'],
'boost' => 3
],
],
],
[
'wildcard' => [
'description' => [
'value' => '*' . $request['query'] . '*',
],
],
],
[
'match' => [
'chapters_v1.chapter' => [
'query' => $request['query'],
'boost' => 3
],
],
],
[
'wildcard' => [
'chapters_v1.chapter' => [
'value' => '*' . $request['query'] . '*',
],
],
],
[
'match' => [
'subtitle' => [
'query' => $request['query'],
'boost' => 1
],
],
],
[
'wildcard' => [
'subtitle' => [
'value' => '*' . $request['query'] . '*',
],
],
],
],
],
],
[
'term' => [
'team_id' => $teamId,
],
],
],
],
],
],
],
'collapse' => [
'field' => 'file_id',
],
'aggs' => [
'total_files' => [
'cardinality' => [
'field' => 'file_id',
],
],
],
'highlight' => [
'fields' => [
'title' => [
'type' => 'plain',
"fragment_size" => 70,
"no_match_size" => 70,
],
'description' => [
'type' => 'plain',
"fragment_size" => 55,
"no_match_size" => 55,
],
'subtitle' => [
'type' => 'plain',
'number_of_fragments' => 1,
'fragment_size' => 30,
],
'chapters_v1.chapter' => [
'type' => 'plain',
'number_of_fragments' => 1,
'fragment_size' => 30,
],
],
],
'track_total_hits' => true,
];
Any suggestion would be helpful. Thanks.
The trick to do this is to combine your individual queries for individual fields into their own
boolqueries. Then you can name theboolquery and rely on query names & scoring to get the matching queries & their contributing scores.It seems that the
function_scoreaction does nothing, so I removed that in my example, but of course, if its critical, you can add it back.Still all
shouldclauses, but now each field is contained in its ownboolwith a_namematching that field.include_named_queries_scoreis a search parameter added in 8.8. This will not only return the query names that matched, but their contributing score.