Elastic search fuzzy match with exact matches showing first

不羁的心 提交于 2019-12-05 01:49:46

问题


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.

Any ideas?


回答1:


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

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




回答2:


Disclaimer, I'm not a php guy but I have some chages to your query:

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

Changing to should make it so only one of the queries must match, not both. You can read about the bool query here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html



来源:https://stackoverflow.com/questions/24528933/elastic-search-fuzzy-match-with-exact-matches-showing-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!