## 创建索引和字段 ```php $params = [ 'index' => 'category', 'body' => [ "mappings" => [ 'properties' => [ "id" => ["type" => "integer"], "parent_id" => ["type" => "integer"], "level" => ["type" => "integer"], "name" => ["type" => "text", "analyzer" => "ik_max_word"], "disable_flag" => ["type" => "integer"], 'children' => [ 'type' => "nested", "properties" => [ "id" => ["type" => "integer",'copy_to' => 'children_id'], ] ] ] ] ] ]; $info = app('es')->indices()->create($params);
联想搜索
/** * @Author: 荷逸 * @Description 联想搜索 * @DateTime 2022-05-20 */ public function autocompleteSearch(CategoryRequest $request) { $keyword = $request->post('keyword'); $params = [ 'index' => 'category', 'body' => [ 'query' => [ "bool" => [ "should" => [ ["prefix" => [ 'name.keyword' => [ 'value' => $keyword, 'boost' => 10, ] ]], ["match_phrase_prefix" => [ 'name' => [ 'query' => $keyword, 'boost' => 7, ] ]], ["match" => [ 'name' => [ 'query' => $keyword, 'boost' => 6, ], ]], ['wildcard' => [ 'name.keyword' => [ 'value' => $keyword . '*', 'boost' => 5, ] ]], ] ], ], '_source'=>[ 'id','name' ], "size" => 10 ], ]; try { $res = app('es')->search($params)->asArray()['hits']['hits']; $data = collect($res)->pluck('_source')->all(); return resReturn(1, $data); } catch (Exception $th) { return resReturn(0, $th->getMessage()); } }