我的索引类似于以下
[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
)
)
)
)
)
我使用以下PHP代码添加了suggest映射。
$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);
我尝试使用以下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);
每当我运行上面的代码时,我都会遇到以下错误。
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]];
)
任何人都知道这里可能出现什么问题?我的索引或我正在使用的参数是否设置错误?
请注意,我正在使用PHP客户端库,而不是直接通过HTTP访问API。
答案 0 :(得分:6)
我让它使用以下设置。
$client = new Elasticsearch\Client();
$param = array();
$param['index'] = 'user_index';
$param['type'] = 'user';
$param['body'] = new stdClass();
$client->create($param);
$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);
//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);
$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);
答案 1 :(得分:2)
尝试将查询包装在另一个数组中,如下所示:
$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);
$ param变量如下所示:
$param = array(
'index' => 'question_index',
'body' => array(
'suggestions' => array(
'text' => 'someth',
'completion' => array(
'field' => 'general_suggest'
)
)
)
);
请注意"建议"已被添加;你可以用任何你想要的东西替换它。