例如,我在 Elasticsearch 中有以下文档:
{ make: 'honda',
model: 'civic 5-door'
year: '2011'
}
{ make: 'honda',
model: 'CIVIC TOURER 5-DOOR '
year: '2011'
}
{ make: 'honda',
model: 'JAZZ 5-DOOR'
year: '2012'
}
{ make: 'mazda',
model: 'some multi word name'
year: '2012'
}
我想制作一个可以在所有三个字段中进行搜索的搜索栏。我尝试了以下查询:$query = 'some query string'
$results = $search->search([
'index' => 'cars',
'body' => [
"query" =>[
'multi_match' => [
"query" => $query,
"type" => "cross_fields",
"analyzer"=> "standard",
"fields" => ['model', 'make', 'year'] ]
]
]
]);
当我使用“honda 2011”之类的搜索查询时,查询返回结果(它与“make”和“year”中的查询词匹配),但是如果我只是键入“civic”(请参见多字字段“model” ),则不会显示任何结果。为什么对多工作 Realm 的搜索不起作用?我想念什么吗?请帮忙。我的索引映射如下所示:
$params = ['index' => 'cars'];
$params['body'] = [
'mappings' => [
'properties' => [
'body_type' => [
'type' => 'keyword'
],
'fuel_type' => [
'type' => 'keyword'
],
'engine_size' => [
'type' => 'short'
],
'make' => [
'type' => 'keyword'
],
'model' => [
'type' => 'keyword'
],
'dealership' => [
'type' => 'keyword'
],
'transmission' => [
'type' => 'keyword'
],
'colour' => [
'type' => 'keyword',
],
'location' => [
'type' => 'geo_point'
]
]
]
];
最佳答案
为了使multi_match起作用,您需要将model
字段更改为text
而不是keyword
。
使用keyword
字段,您只能运行完全匹配,因为未分析关键字字段值。
尝试更改为
'model' => [
'type' => 'text'
],
然后重新索引您的数据,您的查询将起作用。
关于elasticsearch - 多词领域的多匹配 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63128902/