简体   繁体   English

ElasticSearch索引的“建议”映射不会返回结果

[英]'suggest' mapping for ElasticSearch index doesn't return results

I have an index that looks like the following 我有一个如下所示的索引

[question_index] => Array
        (
            [mappings] => Array
                (
                    [QUESTION] => Array
                        (
                            [properties] => Array
                                (
                                    [body] => Array
                                        (
                                            [type] => string
                                        )

                                    [general_suggest] => Array
                                        (
                                            [type] => completion
                                            [analyzer] => simple
                                            [payloads] => 1
                                            [preserve_separators] => 1
                                            [preserve_position_increments] => 1
                                            [max_input_length] => 50
                                        )
                                )

                        )

                )

        )

I added the suggest mapping with the following PHP code. 我使用以下PHP代码添加了建议映射。

$param['index']    = 'question_index';
$param['type']      = 'QUESTION';
$param['body']      = array(
    '_source'       => array(
        'enabled' => true
    ),
    'properties'    => array(
        'body'      => array('type' => 'string'),
        'general_suggest'   => array(
            'type'              => 'completion',
            'index_analyzer'    => 'simple',
            'search_analyzer'   => 'simple',
            'payloads'          => true
        )
    )
);
$result     = $client->indices()->putMapping($param);

I tried to get suggested results with the following PHP code 我尝试通过以下PHP代码获得建议的结果

    $param                                  = array();
    $param['index']                     = 'question_index';
    $param['body']['text']                  = 'someth';//should return 'something' as a suggest
    $param['body']['completion']['field']   = 'general_suggest';
    $result     = $client->suggest($param);

Whenever I run the above code I get the following errors. 每当我运行上面的代码时,都会出现以下错误。

Array
(
    [_shards] => Array
        (
            [total] => 5
            [successful] => 0
            [failed] => 5
            [failures] => Array
                (
                    [0] => Array
                        (
                            [index] => question_index
                            [shard] => 0
                            [reason] => BroadcastShardOperationFailedException[[question_index][0] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [field]]; 
                        )

Anyone know what could be the problem here? 有人知道这可能是什么问题吗? Are my indexes or the parameters I'm using setup incorrectly? 我的索引或使用的参数设置不正确吗?

Please note that I'm using the PHP client library and not accessing the API directly via HTTP. 请注意,我正在使用PHP客户端库,而不是直接通过HTTP访问API。

I got it to work with the following setup. 我将其与以下设置配合使用。

1. Setup the index first 1.首先设置索引

$client      = new Elasticsearch\Client();
$param           = array();
$param['index'] = 'user_index';
$param['type']  = 'user';
$param['body']  = new stdClass();               
$client->create($param);

2. Setup the mapping for suggestions 2.设置映射以获取建议

$param  = array();
$param['index']                 = 'user_index';
$param['type']                  = 'user';
$param['body']                  = array(
    'properties'    => array(
        'body'          => array(
            'type'  => 'string'
        ),
        'autosuggest'   => array(
            'type'              => 'completion',
            'index_analyzer'    => 'simple',
            'search_analyzer'   => 'simple',
            'payloads'          => true
        )
    )
);
$client->indices()->putMapping($param);

3. Add documents to the index 3.将文档添加到索引

//assume $user is an object available to you
$params = array();
$params['body']  = array(
    'autosuggest'   => array(
        'input'     => [$user->full_name, $user->username],
        'output'    => $user->full_name,
        'payload'   => array(
            'id'        => $user->id,
            'full_name' => $user->full_name,
            'username'  => $user->username,
            'type'      => 'user'
        )
    ),
    'body'          => $user->full_name . "(@{$user->username})",
);
$params['index'] = 'user_index';
$params['type']  = 'user';
$params['id']    = $user->id;
$client->index($params);

4. Finally, the way to get suggestions 4.最后,获得建议的方式

$client             = new Elasticsearch\Client();
$param              = array();
$param['index']     = 'user_index';
$param['body']['search_suggest']['text']                    = 'search_query_here';
$param['body']['search_suggest']['completion']['field']    = 'autosuggest';
$results        = $client->suggest($param);

Try to wrap your query in another array like so: 尝试将查询包装在另一个数组中,如下所示:

$param                              = array();
$param['index']                     = 'question_index';
$param['body']['suggestions']['text']                  = 'someth';//should return 'something' as a suggest
$param['body']['suggestions']['completion']['field']   = 'general_suggest';
$result     = $client->suggest($param);

The $param variable would then look like: $ param变量将如下所示:

$param = array(
    'index' => 'question_index',
    'body'  => array(
        'suggestions' => array(
            'text' => 'someth',
            'completion' => array(
                'field' => 'general_suggest'
            )
        )
    )
);

Note that "suggestions" has been added ; 注意,已经添加了“建议”; you can replace it by whatever you want. 您可以根据需要替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM