无法获得以下需求的解决方案。有人可以建议一下吗?
我的索引中有 4 个字段,分别是标题、描述、副标题、章节,我正在所有这些字段中执行搜索操作,因此标题是固定的,我将在前端显示。然后,从其他 3 个字段中,我将检查哪个字段与搜索关键字更相关,并且我将显示这一点,因为我将仅显示 3 个字段中的一个字段以及前端的标题。
$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,
];
任何建议都会有帮助。谢谢。
执行此操作的技巧是将各个字段的单独查询合并到它们自己的
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
}