当前位置:   article > 正文

PHP使用ES[Elasticsearch]_php es

php es

一、为什么使用ES

  1. 因为业务需要,现在需要统计所有出入库、报价、采购的一些数量数据
  2. 但是最早开发的小伙伴为赶时间将很多数据,没有去做拆分,直接在字段中存入json字符串,可是要统计的数据都在这个json中
  3. 能想到的解决方案如下:
    ① 拆表,重写CURD功能
    ② 数据量不大的前提下直接全表扫描在代码中处理数据
    ③ 使用mysql存储过程,其实也算是在全表扫描
    ④ 升级mysql版本到8+,使用其自带的json数据结构去处理数据
    ⑤ 使用es代替方案①的拆表操作

二、环境

  1. PHP:8.0.2
  2. ES:7.6.1
  3. IK分词器:7.6.1
  4. es-head
  5. JDK:1.8
  6. win10
  7. 框架

三、启动ES

  1. ES安装包
  2. 进入启动目录: cd es安装目录/bin
  3. 启动ES:./elasticsearch.bat (确保已经安装jdk)
  4. 安装es-head插件:如何安装es-head
  5. 安装ik分词器 ik分词器安装包
  6. 将分词器安装包放置到 es安装目录/plugins
  7. 重启es

四、安装PHP依赖

  1. composer require elasticsearch/elasticsearch:^7.6.1

五、在PHP中使用ES

namespace app\common\model;
use Elasticsearch\ClientBuilder;

class EsModel
{
	protected $client;
	// 连接es
	public function __construct()
    {
        try {
            $this->client = ClientBuilder::create()
                ->setHosts(['127.0.0.1:9200'])
                ->build();
        } catch (\Exception $e) {
            // 输出连接错误信息
            echo $e->getMessage();
            exit;
        }
    }
	// 创建es索引
    public function addIndex(){
        // 定义索引的设置和映射
        $indexParams = [
            'index' => 'product_info',
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    'properties' => [
                        'title_name' => [
                            'type' => 'text',
                            'analyzer' => 'ik_smart'
                        ],
                        'describe' => [
                            'type' => 'text',
                            'analyzer' => 'ik_smart'
                        ]
                    ]
                ]
            ]
        ];

        // 发送创建索引的请求
        $this->client->indices()->create($indexParams);
    }
	
	// 批量插入数据
	public function addEsData(){
        $data = [
            [
                "index" => ['_id' => 1,]
            ],
            [
                "title_name" => "一个新的文章",
                "describe" => "陈美凤日前出席假发品牌32周年活动,只见她顶著一头粉色的秀发,再配上深V领黑西服"
            ],
        ];

        $params = [
            'index' => 'product_info', // 索引名称
            'body' => $data,
        ];

        $response = $this->client->bulk($params);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

六、查看es数据以及分词结果

  1. 使用谷歌浏览器打开组件es-head
  2. es-head复合查询
  3. http://localhost:9200/{index}/{type}/{id}/_termvectors?fields={fieldName}
    index:product_info
    type:_doc
    id:1
    fieldName:describe
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/883337
推荐阅读
相关标签
  

闽ICP备14008679号