PHP Elasticsearch在字符串开头搜索
我在MySql数据库中有一个产品表,我想搜索它们的产品名称和描述 如果我在搜索中输入产品的全名,一切都会很好,但它不会将术语分解成几个部分。例如,它会找到“先锋”一词的产品,但不会找到任何“pio”或“pionee”的产品 我猜我不是建立了错误的索引,就是做了错误的搜索 我正在创建索引,如下所示:PHP Elasticsearch在字符串开头搜索,php,
elasticsearch,Php,
elasticsearch,我在MySql数据库中有一个产品表,我想搜索它们的产品名称和描述 如果我在搜索中输入产品的全名,一切都会很好,但它不会将术语分解成几个部分。例如,它会找到“先锋”一词的产品,但不会找到任何“pio”或“pionee”的产品 我猜我不是建立了错误的索引,就是做了错误的搜索 我正在创建索引,如下所示: $params = [ 'index' => 'products', 'type' => 'product', 'body' => [
$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);
$params=[
“索引”=>“产品”,
“类型”=>“产品”,
“正文”=>[
“查询”=>[
“匹配”=>[
'name'=>$term
],
“匹配”=>[
'description'=>$term
]
]
]
];
$response=$client->search($params);
foreach($response['hits']['hits']as$result){
echo$result[''u source']['name'].
;
}
打印(回复);
你有没有试着检查一下你的术语是什么样子的?@Tim老实说,没有,PHP文档在基础上非常薄弱。上面的设置看起来正确吗?我对PHP不是很熟悉,但是如果您不想使用curl,您应该能够使用a将json发送到Elastic。这将允许您使用_analyzeAPI。至于你的设置,我会再次查看你的Ngram。通常,他们建议最小值和最大值彼此在1以内,或者相同。我会从三元图开始(最小值3和最大值3)。