版本说明
- 本文是在es8.4.1下进行操作,同时已经安装了ik分词器。
- php操作es的库为elasticsearch-php,github地址为:
GitHub - elastic/elasticsearch-php: Official PHP client for Elasticsearch. - 所有的操作都基于本地服务器进行,本地安装时关闭了安全验证
- index为goods,mapping结构如下:
{ "goods": { "mappings": { "properties": { "brand": { "type": "keyword" }, "content": { "type": "text", "analyzer": "ik_max_word" }, "id": { "type": "integer" }, "sort": { "type": "integer" }, "title": { "type": "text", "analyzer": "ik_smart" } } } }}
- 进行操作时,新建es对象
use Elastic\Elasticsearch\ClientBuilder;$client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build();
一些查询操作
我们使用es的情景大部分都是查询,下面列举一些常用的操作场景以及对应的代码
获取mapping结构
$response = $client->indices()->getMapping(['index'=>'goods_bak']);
批量批量插入数据
//$count 为从数据库中取的总数,$result为对应的结果集for($i = 0; $i < $count; $i++) { $add_params['body'][] = [ 'index' => [ '_index' => "goods", '_id' => ($i+1) . "" ], ]; $add_params['body'][] = [ 'id' => $result[$i][0] + 1, 'title' => $result[$i][1], 'content' => $result[$i][2], 'sort' => $result[$i][3], 'brand' => $result[$i][4], ];}$responses = $client->bulk($add_params);
删除文档数据
$delete_params = [ 'index' => 'goods', 'body' => [ 'query' => [ 'range' => [ 'id' => [ //删除id > 1的文档记录 'gte' =>1 ] ] ] ]];$responses = $client->deleteByQuery($delete_params);
查询title中包含关键词懒虫的文档记录
// match 会对查询的关键词进行分词,term不会。例如查询华为手机,match会查询title中包含华为或者手机或者华为手机的,term不会对查询的关键词做拆分//不支持对多个字段进行同时查询//es默认返回10条记录,可以用from(偏移量)和size(pagesize)进行分页$params = [ 'index' => 'goods', 'body' => [ 'query' => [ 'term' => [// 'match' => [ 'title' => '懒虫天鹅' ] ] ], 'from' => 0, //分页的位置 'size' => 20 //返回多少条];$response = $client->search($params)->asArray();
模糊查询之wildcard、前缀查询
$params = [ 'index' => 'goods', 'body' => [ 'query' => [ //wildcard会对查询条件进行分词,后面可以接* ? 通配符进行匹配 // prefix前缀匹配,匹配到分词中以设置的关键词开头的分词// 'wildcard' => [ 'prefix' => [ 'brand' => '星' ] ] ], 'from' => 0, 'size' => 20];
范围查询
$params = [ 'index' => 'goods', 'body' => [ 'query' => [ 'range' => [ 'sort' => [ 'gte' => 10, 'lte' => 100, ] ] ] ], 'from' => 0, 'size' => 20];
bool查询
//bool查询,多个条件共同成立,有四种选择,must(计算得分,性能较低) filter(过滤条件,不计算得分,性能高),must_not(条件必须不成立),should(条件可以成立)//查询title包含懒虫且brand中含有华为的文档记录$params = [ 'index' => 'goods', 'body' => [ 'query'=>[ 'bool' => [ 'filter' => [ //多个筛选条件时,每个term是数组形式 ['term' => ['title' => '懒虫',]], ['term' => ['brand' =>'华为',]] ] ] ] ], 'from' => 0, 'size' => 30];
多个字段不同关键词查询的并集操作
$params = [ 'index' => 'goods', 'body' => [ 'query'=>[ //查询title包含懒虫或者brand包含华为的文档记录 'dis_max' => [ 'queries' => [ //多个筛选条件时,每个term是数组形式 ['term' => ['title' => '懒虫',]], ['term' => ['brand' =>'华为',]] ] ] ], //对结果进行聚合操作 'aggs' => [ //最大得分的记录,返回的结果字段为max_sort 'max_sort' => [ 'max' => ['field' => 'sort'] ], //对查询结果按brand进行聚合汇总,取出前size条记录 'goods_brand_rank' => [ 'terms' => [ 'field' => 'brand', 'size' => 10 ] ] ], //对结果进行高亮显示 'highlight' => [ //这里定义高亮显示的字段以及高亮显示的tag,前端拿到tag后进行处理 'fields' => [ 'title' => [ 'pre_tags' => '', 'post_tags' => '', ] ] ] ], 'from' => 0, 'size' => 30];
来源地址:https://blog.csdn.net/u010242979/article/details/128107439