How to get scoring of highlighted results in ElasticSearch

36 Views Asked by At

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.

1

There are 1 best solutions below

2
Benjamin Trent On

The trick to do this is to combine your individual queries for individual fields into their own bool queries. Then you can name the bool query and rely on query names & scoring to get the matching queries & their contributing scores.

It seems that the function_score action does nothing, so I removed that in my example, but of course, if its critical, you can add it back.

Still all should clauses, but now each field is contained in its own bool with a _name matching that field.

include_named_queries_score is a search parameter added in 8.8. This will not only return the query names that matched, but their contributing score.

{
  "size": 5,
  "query": {
    "query": {
      "bool": {
        "must": [
          {
            "bool": {
              "should": [
                {
                  "bool": {
                    "_name": "title",
                    "should": [
                      {
                        "match": {
                          "title": {
                            "query": "<your_query_here>",
                            "boost": 4
                          }
                        }
                      },
                      {
                        "wildcard": {
                          "title": {
                            "value": "*<your_query_here>*"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "_name": "description",
                    "should": [
                      {
                        "match": {
                          "description": {
                            "query": "<your_query_here>",
                            "boost": 3
                          }
                        }
                      },
                      {
                        "wildcard": {
                          "description": {
                            "value": "*<your_query_here>*"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "_name": "chapters",
                    "should": [
                      {
                        "match": {
                          "chapters_v1.chapter": {
                            "query": "<your_query_here>",
                            "boost": 3
                          }
                        }
                      },
                      {
                        "wildcard": {
                          "chapters_v1.chapter": {
                            "value": "*<your_query_here>*"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "_name": "subtitle",
                    "should": [
                      {
                        "match": {
                          "subtitle": {
                            "query": "<your_query_here>",
                            "boost": 1
                          }
                        }
                      },
                      {
                        "wildcard": {
                          "subtitle": {
                            "value": "*<your_query_here>*"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          {
            "term": {
              "team_id": "<team_id_here>"
            }
          }
        ]
      }
    }
  },
  "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
      }
    }
  },
  "include_named_queries_score": true,
  "track_total_hits": true
}