栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elastic search模糊匹配,精确匹配显示在前

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elastic search模糊匹配,精确匹配显示在前

我最终没有使用模糊匹配来解决我的问题,而是使用了ngram。

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', ), 'barpres'    => 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', 'barpres'),         ),     ),     'size' => '5000', ),        )    );    $productIds = array();    if (!empty($return['hits']['hits'])) {        foreach ($return['hits']['hits'] as $hit) { $productIds[] = $hit['_id'];        }    }    return $productIds;}

结果正是我想要的。它根据搜索查询中包含的ngram部分构造匹配项。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387419.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号