Elastic fuzzy match with exact matches showing the first

I want to use fuzzy matching on demand, but with exact matches displayed at the top of the results.

I have 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 does not work due to an incorrect request error.

Any ideas?

+4
source share
2 answers

In the end, I did not use fuzzy matching to solve my problem and went 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 builds matches based on how many parts of ngram have a search query in it.

+5
source

Disclaimer, I am not a php guy, but I have some tips for your request:

$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',
        ),
    )
);

, , . bool: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

0

Source: https://habr.com/ru/post/1546831/


All Articles