如何在 ElasticSearch 中对突出显示的结果进行评分

问题描述 投票:0回答:1

无法获得以下需求的解决方案。有人可以建议一下吗?

  • 我有一个 ES 查询,它在我的索引中搜索 4 个字段并提供突出显示的结果。但我想对每个文档的突出显示字段进行评分。
  • 虽然我能够获得每个文档的分数,但我想获取每个文档的突出显示字段的分数。

我需要这个的实际场景

我的索引中有 4 个字段,分别是标题、描述、副标题、章节,我正在所有这些字段中执行搜索操作,因此标题是固定的,我将在前端显示。然后,从其他 3 个字段中,我将检查哪个字段与搜索关键字更相关,并且我将显示这一点,因为我将仅显示 3 个字段中的一个字段以及前端的标题。

尝试过的解决方案

  • 尝试使用explain=true,但它非常复杂,我没有得到任何密钥来获取每个字段的分数。
  • 尝试过sub_searches但对我不起作用。

这是我当前的查询

$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,
        ];

任何建议都会有帮助。谢谢。

elasticsearch highlight scoring elasticsearch-php
1个回答
0
投票

执行此操作的技巧是将各个字段的单独查询合并到它们自己的

bool
查询中。然后,您可以命名
bool
查询并依靠查询名称和评分来获取匹配的查询及其贡献分数。

看起来

function_score
操作没有任何作用,所以我在示例中删除了它,但是当然,如果它很重要,您可以将其添加回来。

仍然是所有

should
子句,但现在每个字段都包含在自己的
bool
中,并且
_name
与该字段匹配。

include_named_queries_score
是8.8中添加的搜索参数。这不仅会返回匹配的查询名称,还会返回它们的贡献分数。

{
  "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
}
© www.soinside.com 2019 - 2024. All rights reserved.