企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>elasticsearch版本必须 >= 7.0 <br/> ## createIndex - 创建索引 ~~~ use \api\Es; // @param 索引名称 // @param 字段 // @param setting Es::createIndex('index', [ 'field1' => [ 'type' => 'text' ], 'field2' => [ 'type' => 'keyword' ], 'field3' => [ 'type' => 'text', // 字段分词方式 'analyzer' => 'standard' ] ]); ~~~ ## deleteIndex - 删除索引 ~~~ use \api\Es; // @param 要删除的索引名称 Es::deleteIndex('index'); ~~~ ## getMapping - 获取索引映射信息 ~~~ use \api\Es; // @param 索引名称 Es::getMapping('index'); ~~~ ## putMapping - 设置索引字段映射,注意:已经设置过的无法修改 ~~~ use \api\Es; // @param 索引名称 // @param 字段 Es::putMapping('index', [ 'field' => [ 'type' => 'text' ] ]); ~~~ ## select - 设置查询字段 ~~~ use \api\Es; // 不设置则查询所有字段,类似SQL里的 select * Es::select('field1, field2'); ~~~ ## from - 指定操作的索引 ~~~ use \api\Es; Es::from('index'); // 也可以简写,如下 es('index'); ~~~ ## where - 查询条件 ~~~ use \api\Es; Es::where([ 'must' => [ [ 'match' => ['title' => '索引'] ], [ 'range' => [ 'timestamp' => ['gt' => 1525760500] ] ] ] ]); ~~~ ## order - 排序 ~~~ use \api\Es; Es::order([ 'timestamp' => 'desc' // 升序用 asc ]); ~~~ ## limit - 查询条数 ~~~ use \api\Es; // 查询10条 Es::limit(10); // 查询10到20条 Es::limit(10, 10); ~~~ ## get - 根据_id快速查询一条 ~~~ $data = es('index')->get(1); ~~~ ## all - 查询多条 ~~~ $data = es('index')->select('name, title')->where([ 'must' => [ [ 'match' => ['title' => '索引'] ], [ 'match' => ['name' => 'phpcan'] ] ] ])->order([ 'timestamp' => 'desc' ])->limit(10, 10)->all(); // 指定查询后的高亮字段是title $data = es('index')->select('name, title')->where([ 'must' => [ [ 'match' => ['title' => '索引'] ], [ 'match' => ['name' => 'phpcan'] ] ] ])->order([ 'timestamp' => 'desc' ])->limit(10, 10)->all([ 'title' ]); ~~~ ## add - 插入数据 >插入数据时会自动加上 timestamp 字段用来表示数据索引的时间 ~~~ // 插入后会返回insertid $id = es('index')->add([ 'title' => 'title', 'name' => 'phpcan' ]); // 批量插入,第二个参数设置为TRUE表示批量插入操作 es('index')->add([ [ 'title' => 'title', 'name' => 'phpcan' ], [ 'title' => 'title', 'name' => 'phpcan' ], [ 'title' => 'title', 'name' => 'phpcan' ], [ 'title' => 'title', 'name' => 'phpcan' ], [ 'title' => 'title', 'name' => 'phpcan' ] ], TRUE); ~~~ ## edit - 更新数据 > 注意:更新操作只能通过主键来完成 ~~~ // @param 主键ID // @param 更新的内容 es('index')->edit(1, [ 'title' => 'new title', 'name' => 'new name' ]); ~~~ ## delete - 删除数据 ~~~ // 返回删除的数据条数 $count = es('index')->where([ 'must' => [ [ 'match' => ['_id' => 1] ] ] ])->delete(); ~~~