栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch实例操作

Elasticsearch实例操作

前提条件本机运行了elasticsearch服务

安装Elasticsearch php插件
composer require elasticsearch/elasticsearch
基本的新增、删除索引
//加载Elasticsearch php插件
require "vendor/autoload.php";

use ElasticsearchClientBuilder;

//本机host
$hosts = [
    '127.0.0.1:9200'
];
//连接
$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    //索引名
    'index' => 'artlce',
    'body' => [
        'settings' => [
            //分区数
            'number_of_shards' => 5,
            //副本数
            'number_of_replicas' => 1
        ],
        'mappings' => [
            //文档
            '_doc' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    //文档的字段以及字段的类型
                    'anchor' => [
                        //keyword类型的字段只能通过精确值搜索到
                        'type' => 'keyword'
                    ],
                    'title' => [
                  		'type' => 'text',
                        //ik分词器的ik_max_word模式
                        'analyzer' => 'ik_max_word',
                        'search_analyzer' => 'ik_max_word'
                    ],
                    'desn' => [
                        'type' => 'text',
                        //ik分词器的ik_max_word模式
                        'analyzer' => 'ik_max_word',
                        'search_analyzer' => 'ik_max_word'
                    ]
                ]
            ]
        ]
    ]
];
// 创建索引
$response = $client->indices()->create($params);


//删除索引
$params1 = ['index' => 'artlce'];
$response1 = $client->indices()->delete($params1);
ik分词器

1、ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民, 中华,华人,人民共和国,人民,人,民,共和国,共和,和国,国歌”,会穷尽各种可能的组合;

2、ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民,共和国,国歌”。 索引时,为了提供索引的覆盖范围,通常会采用ik_max_word分析器,会以最细粒度分词索引,搜索时为了提高搜索准确度,会采用ik_smart分析器,会以粗粒度分词

创建索引成功

基本的文档的操作
//加载Elasticsearch php插件
require "vendor/autoload.php";

use ElasticsearchClientBuilder;

//本机host
$hosts = [
    '127.0.0.1:9200'
];
//连接
$client = ClientBuilder::create()->setHosts($hosts)->build();
//新增文档到索引中
$params2 = [
    //索引
    'index' => 'artlce',
    //类型 es6之后一个索引对应一个type
    'type' => '_doc',
    //文档id
    'id' => 1,
    //字段值
    'body' => [
        'anchor' => 'wyq',
        'title' => '这是一篇文章',
        'desn' => '这是一篇文章的简介',
    ],
];
$response = $client->index($params2);


//修改指定索引指定文档的内容
$params3 = [
    //索引
    'index' => 'artlce',
    //类型 es6之后一个索引对应一个type
    'type' => '_doc',
    //文档id
    'id' => 1,
    //字段值
    'body' => [
        'anchor' => 'wyq123',
    ],
];
$response = $client->update($params3);

//删除指定索引的文档
$params3 = [
    //索引
    'index' => 'artlce',
    //文档id
    'id' => 1,
];
$response = $client->delete($params3);


//查询索引文档的内容
$params4 = [
    'index' => "artcle",
    'body' => [
        'query' => [
            'match' => [
                'title' => "这是一篇"
            ]
        ]
    ]
];
$response = $this->client->search($params4);

集成到CI框架中

和之前的redis一样封装一个类,里面编写一些经常用到的Elasticsearch方法,之前在thinkphp5框架中封装过。

//libraries目录下的es类
client = ClientBuilder::create()->setHosts(config_item('esHost'))->build();
    }

    public function getEs()
    {
        return $this->client;
    }

    
    public function createIndex($indexName = "", $number_of_shards = 5, $number_of_replicas = 1, $data = [])
    {
        // 创建索引
        $params = [
            'index' => $indexName,
            'body' => [
                'settings' => [
                    'number_of_shards' => $number_of_shards,
                    'number_of_replicas' => $number_of_replicas
                ],
                'mappings' => [
                    '_doc' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => $data
                    ]
                ]
            ]
        ];
        try {
            $response = $this->client->indices()->create($params);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (BadRequest400Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }

    
    public function delIndex($indexName = "")
    {
        $data = ['index' => $indexName];
        try {
            $response = $this->client->indices()->delete($data);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (Missing404Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }

    
    public function SaveDoc($indexName = "", int $id, $data = [])
    {
        // 写文档
        $params = [
            'index' => $indexName,
            'type' => '_doc',
            'id' => $id,
            'body' => $data,
        ];

        try {
            $response = $this->client->index($params);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (Missing404Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }

    
    public function deleteDoc($indexName = "", int $id)
    {
        $data = [
            'index' => $indexName,
            'id' => $id,
        ];
        try {
            $response = $this->client->delete($data);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (Missing404Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }

    
    public function updateDoc($indexName = "", int $id, $data = [])
    {
        //修改指定索引指定文档的内容
        $params = [
            //索引
            'index' => $indexName,
            //类型 es6之后一个索引对应一个type
            'type' => '_doc',
            //文档id
            'id' => $id,
            //字段值
            'body' => [
                'doc' => [
                    $data
                ]
            ],
        ];
        try {
            $response = $this->client->update($params);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (Missing404Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }

    
    public function searchDoc($indexName = "", $body =[])
    {
        $params = [
            'index' => $indexName,
            'body' => $body,
        ];
        try {
            $response = $this->client->search($params);
            $res = [
                'code' => 200,
                'message' => "success",
                'data' => $response
            ];
        } catch (Missing404Exception $exception) {
            $res = [
                'code' => $exception->getCode(),
                'message' => json_decode($exception->getMessage(), true),
                'data' => [],
            ];
        }
        return $res;
    }
}
创建索引
//控制器Api下面的代码
public function createIndex()
    {
       //获取上面的es功能类
        $es = getEs();
        $data = [
            //文档的字段以及字段的类型
            'uses_id' => [
                'type' => 'integer'
            ],
            'nickname' => [
                //keyword类型的字段只能通过精确值搜索到
                'type'=> 'keyword'
            ],
            'pageViews' => [
                'type' => 'integer'
            ],
            'likes' => [
                'type' => 'integer'
            ],
            'title' => [
                'type' => 'text',
                //ik分词器的ik_max_word模式
                'analyzer' => 'ik_max_word',
                'search_analyzer' => 'ik_max_word'
            ],
            'content' => [
                'type' => 'text',
                //ik分词器的ik_max_word模式
                'analyzer' => 'ik_max_word',
                'search_analyzer' => 'ik_max_word'
            ],
            'create_time' => [
                'type' => 'integer'
            ],
            'update_time' => [
                'type' => 'date',
                //date的格式
                "format" => "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            ],
        ];
    	//创建索引
       $res = $es->createIndex('articles',5,1,$data);
        if ($res) {
            success($res);
        }
        fail($res);
    }

结果

将全部的文章放入es中

这里面的字段都是检索之后需要展示的字段,其他不需要展示的字段为放入es中

//控制器 Job/job下的代码
public function articleToes()
    {
        $article = $this->article_model->findAll();
        $es = getEs();
        foreach ($article as $v) {
            $data = [
                'user_id' => $v['user_id'],
                'nickname' => $v['nickname'],
                'desc' => $v['desc'],
                'content' => $v['content'],
                'title' => $v['title'],
                'pageViews' => $v['pageViews'],
                'likes' => $v['likes'],
                'create_time' => $v['create_time'],
                'update_time' => date('Y-m-d H:i:s', $v['update_time'])
            ];
            $es->SaveDoc('articles', $v['id'], $data);
        }
    }

结果

对文章进行检索
//Article控制器代码
public function search()
    {
        $data = $this->input->post();
        if (!empty($data['keyword'])){
            $body = [
                'query' => [
                    'bool' => [
                        //这里的should相当于or操作,还有must相当于and操作
                        'should' => [
                            ['match' => ['title' => $data['keyword'] ] ],
                            ['match' => ['desc' => $data['keyword'] ] ],
                            ['match' => ['content' => $data['keyword'] ] ],
                            ['match' => ['nickname' => $data['keyword'] ] ],
                        ]
                    ]
                ]
            ];
            $res = $this->es->searchDoc('articles', $body);
            success($res);
        }
    }

结果:


这里对es类进行了简单的封装,以及简单的操作,后续还会对代码进行优化.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/673853.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号