弹性搜索模糊匹配与精确匹配显示第一 [英] Elastic search fuzzy match with exact matches showing first

查看:107
本文介绍了弹性搜索模糊匹配与精确匹配显示第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在查询中使用模糊匹配,但精确匹配显示在结果的顶部。

I am wanting to use fuzzy matching on a query but with exact matches showing at the top of the results.

我尝试过以下操作。 >

I've tried the following.

$return = $this->_client->search(
            array(
                'index' => self::INDEX,
                'type'  => self::TYPE,
                'body'  => array(
                    'query' => array(
                        'bool' => array(
                            'must' => array(
                                'multi_match' => array(
                                    'query'     => $query,
                                    'fields'    => array('name', 'brand', 'description'),
                                    'boost'     => 10,
                                ),
                                'fuzzy_like_this' => array(
                                    'like_text' => $query,
                                    'fields'    => array('name', 'brand', 'description'),
                                    'fuzziness' => 1,
                                ),
                            ),
                        ),
                    ),
                    'size' => '5000',
                ),
            )
        );

由于格式错误的查询错误,这无效。

This doesn't work due a malformed query error.

任何想法?

推荐答案

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

I ended up not using fuzzy matching to solve my problem and went with using ngram's.

/**
 * 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;
}

结果正是我正在寻找的。它根据搜索查询中有多少个ngram部分构建匹配。

The result is exactly what I was looking for. It constructs matches based on how many ngram part the search query has within it.

这篇关于弹性搜索模糊匹配与精确匹配显示第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆