我在MySql数据库中有一个产品表,我想搜索它们的产品名称和描述。
如果我在搜索中输入产品的全名,那么一切都很好,但不会将术语分解为几个部分。例如,它将找到“先锋”一词的产品,但找不到“ pio”或“ pionee”的任何东西。
我猜我是在错误地建立索引还是在错误地进行搜索?
我正在这样创建索引:
$params = [
'index' => 'products',
'type' => 'product',
'body' => [
'settings' => [
'analysis' => [
'filter' => [
'ngram_filter' => [
"type" => "ngram",
"min_gram" => 1,
"max_gram" => 10
]
],
'analyzer' => [
'ngram_analyzer' => [
"type" => "custom",
"tokenizer" => "standard",
"filter" => [
"lowercase",
"asciifolding",
"ngram_filter"
]
]
]
]
],
'mappings' => [
'products' => [
'name' => [
'type' => 'string',
'include_in_all' => true,
'analyzer' => 'ngram_analyzer',
'search_analyzer' => 'standard'
],
'description' => [
'type' => 'string',
'include_in_all' => true,
'analyzer' => 'ngram_analyzer',
'search_analyzer' => 'standard'
]
]
]
]
];
我正在像这样索引数据库结果:
foreach($products as $product){
$params = [
'index' => 'products',
'type' => 'product',
'id' => $product['id'],
'body' => [
'name' => $product['name'],
'description' => $product['description']
]
];
$response = $client->index($params);
var_dump($response);
}
然后要搜索,我正在执行以下操作:
$params = [
'index' => 'products',
'type' => 'product',
'body' => [
'query' => [
'match' => [
'name' => $term
],
'match' => [
'description' => $term
]
]
]
];
$response = $client->search($params);
foreach($response['hits']['hits'] as $result){
echo $result['_source']['name'] . '<br>';
}
print_r($response);