es like模糊匹配_Elastic search模糊匹配,精确匹配显示在前
小編典典
我最終沒(méi)有使用模糊匹配來(lái)解決我的問(wèn)題,而是使用了ngram。
/**
* Map - Create a new index with property mapping
*/
public function map()
{
$params['index'] = self::INDEX;
$params['body']['settings'] = array(
'index' => array(
'analysis' => array(
'analyzer' => array(
'product_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => array('lowercase', 'product_ngram'),
),
),
'filter' => array(
'product_ngram' => array(
'type' => 'nGram',
'min_gram' => 3,
'max_gram' => 5,
),
)
),
)
);
//all the beans
$mapping = array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'id' => array(
'type' => 'string',
),
'name' => array(
'type' => 'string',
'analyzer' => 'product_analyzer',
'boost' => '10',
),
'brand' => array(
'type' => 'string',
'analyzer' => 'product_analyzer',
'boost' => '5',
),
'description' => array(
'type' => 'string',
),
'barcodes' => array(
'type' => 'string'
),
),
);
$params['body']['mappings'][self::TYPE] = $mapping;
$this->_client->indices()->create($params);
}
public function search($query)
{
$return = $this->_client->search(
array(
'index' => self::INDEX,
'type' => self::TYPE,
'body' => array(
'query' => array(
'multi_match' => array(
'query' => $query,
'fields' => array('id', 'name', 'brand', 'description', 'barcodes'),
),
),
'size' => '5000',
),
)
);
$productIds = array();
if (!empty($return['hits']['hits'])) {
foreach ($return['hits']['hits'] as $hit) {
$productIds[] = $hit['_id'];
}
}
return $productIds;
}
結(jié)果正是我想要的。它根據(jù)搜索查詢中包含的ngram部分構(gòu)造匹配項(xiàng)。
2020-06-22
總結(jié)
以上是生活随笔為你收集整理的es like模糊匹配_Elastic search模糊匹配,精确匹配显示在前的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux内核编程踩坑
- 下一篇: Android 蓝牙4.0在实际开发中的