iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >elasticsearch 简单使用【php版本】
  • 279
分享到

elasticsearch 简单使用【php版本】

elasticsearchphp 2023-09-15 14:09:21 279人浏览 八月长安
摘要

版本说明 本文是在es8.4.1下进行操作,同时已经安装了ik分词器。PHP操作es的库为​elasticsearch-php,GitHub地址为: GitHub - elastic/elastics

版本说明

{  "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

--结束END--

本文标题: elasticsearch 简单使用【php版本】

本文链接: https://www.lsjlt.com/news/408638.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作