我正在尝试创建一个用于弹性搜索的查询。条件:总是有一个客户过滤器。如果匹配,有一个字段"name"应该会创建最高分数,其次是"category",然后是"description"。但是,即使搜索在"name"或"category"中没有找到任何内容,而只在"description"中找到,也应该返回结果。
我尝试过的:
$query = [
'filtered' => [
'filter' => [
'term' => [ 'customer' => $selectedCustomer ]
],
'query' => [
'bool' => [
'should' => [
'match' => [
'name' => [
'query' => $val,
'boost' => 7
]
]
],
'should' => [
'match' => [
'category' => [
'query' => $val,
'boost' => 3
]
],
],
'should' => [
'match' => [
'description' => [
'query' => $val,
'boost' => 1
]
],
],
]
]
]
];
但这似乎只返回"描述"中匹配的结果。我做错了什么?有什么提示吗?
我似乎通过这样做找到了第一个问题的解决方案:
$query = [
'filtered' => [
'filter' => [
'term' => [ 'customer' => $selectedCustomer ]
],
'query' => [
'bool' => [
'should' => [
['match' => [
'name' => [
'query' => $val,
'boost' => 7
]
]],
['match' => [
'category' => [
'query' => $val,
'boost' => 5
]
]],
['match' => [
'description' => [
'query' => $val,
'boost' => 1
]
]],
],
]
]
]
];
然而,下一个问题是,这只会找到整个单词的匹配项。比如,如果$val='bread',我会得到所有包含单词"bread"的匹配项,但我不会得到像"姜饼"这样的东西。有人知道解决这个问题的办法吗?