美文网首页
php + Elasticsearch 使用

php + Elasticsearch 使用

作者: 小哥哥吖Q | 来源:发表于2020-07-09 15:26 被阅读0次

window环境安装 

安装es 官方下载速度太慢了 我提供的是7.31的版本 7.3的版本内置了java解释器 不需要在安装

window 7.31 安装

链接: https://pan.baidu.com/s/1CM-4D4oSxNpPXuvZveLPdQ  提取码: 5k8y

linux es 7.31安装 下载解压就可以了 注意的是es不能使用root账号运行

链接:https://pan.baidu.com/s/13AkN_I8b7QZdyzBEUp_YuA 提取码:oysi

中文分词

https://github.com/medcl/elasticsearch-analysis-ik/releases

  kibana可视化工具下载地址

https://www.elastic.co/cn/downloads/kibana

使用 composer 内置管理器  

composer require 'elasticsearch/elasticsearch'

然后在家composer的默认自动加载类就好了 

Elasticsearch 详细讲解  https://www.cnblogs.com/codeAB/p/10283304.html

es官方已经禁止了type默认 如果要使用可以在创建索引的时间添加   'include_type_name' => true 方法 

8.0以后默认都是type _doc 已经废除了 type自定义方式

es中文社区 https://elasticsearch.cn/article/601

php 代码分享 作者版本 7.31 

use Elasticsearch\ClientBuilder;

class ElasticsearchService

{

    protected static $client = null;

    public function __construct()

{

        if (!self::$client) {

            self::$client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();

}

}

    public function indexAdd($index_name, $number_of_shards = 2, $number_of_replicas = 0)

{

        $params = [

            'index' => $index_name, #index的名字不能是大写和下划线开头

            'body' => [

                'settings' => [

                    'number_of_shards' => $number_of_shards,

                    'number_of_replicas' => $number_of_replicas,

]

]

];

        return self::$client->indices()->create($params);

}

    /**

    * @param $index_name

    * @param $type_name

    * @param array $field

    * @return array

* [

* 'id' => [

* 'type' => 'integer'

* ],

* 'first_name' => [

* 'type' => 'text',

* 'analyzer' => 'ik_max_word'

* ],

* 'last_name' => [

* 'type' => 'text',

* 'analyzer' => 'ik_max_word'

* ],

* 'age' => [

* 'type' => 'integer'

* ]

* ]

*/

    public function typeAdd($index_name, $field = [],$type_name = '_doc')

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'body' => [

                'mytype' => [

                    '_source' => [

                        'enabled' => true

                    ],

                    'properties' => $field,

//                    'include_type_name' => true,

                ]

]

];

        return self::$client->indices()->putMapping($params);

}

    /**

    * 创建一个数据  'body' => [

    * 'first_name' => '张',

    * 'last_name' => '三',

* 'age' => 35

* ]

*/

    public function indexTypeAddRow($index_name, $body,$type_name ='_doc',$id = null)

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'id' => $id, #可以手动指定id,也可以不指定随机生成

            'body' => $body,

//            'include_type_name' => true,

        ];

        $params = array_filter($params);

        return self::$client->index($params);

}

    public function searchAll()

{

        return self::$client->search();

}

    public function search($index_name, $param,$type_name = '_doc')

{

        $params['index'] = $index_name;

        $params['type'] = $type_name;

        $params['body'] = $param;

        return self::$client->search($params);

}

    public function get($index_name, $id,$type_name = '_doc')

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'id' => $id

        ];

        return self::$client->get($params);

}

    public function del($index_name, $type_name, $id)

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'id' => $id

        ];

        return self::$client->delete($params);

}

    /**

    * 一次获取多个文档

    * @param $ids

    * @return array

*/

    public function gets($index_name, $type_name, array $id)

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'id' => $id

        ];

        return self::$client->mget($params);

}

    /**

    * @param $index_name

    * @param $type_name

    * @param $body 'doc' => [

    * 'first_name' => '张',

    * 'last_name' => '三',

* 'age' => 99

* ]

    * @param $id

    * @return mixed

*/

    public function update($index_name, $type_name, $body, $id)

{

        $params = [

            'index' => $index_name,

            'type' => $type_name,

            'id' => $id,

            'body' => [

                $body

            ]

];

        return self::$client->update($params);

}

    /**

    * @return array

*/

    public function esStatus()

{

        return self::$client->indices()->stats();

}

    /**

    * 检查Index 是否存在

    * @return bool

*/

    public function checkIndexExists($index_name)

{

        $params = [

            'index' => $index_name

        ];

        return self::$client->indices()->exists($params);

}

    public function delIndex($index_name)

{

        $params = [

            'index' => $index_name

        ];

        if ($this->checkIndexExists($index_name)) {

            return self::$client->indices()->delete($params);

}

}

    public function delAllIndex()

{

        $indexList = $this->esStatus()['indices'];

        foreach ($indexList as $item => $index) {

            $this->delIndex($item);

}

        return true;

}

}

删除文档

es只有删除索引才会释放空间

https://elastic.blog.csdn.net/article/details/80038930

相关文章

网友评论

      本文标题:php + Elasticsearch 使用

      本文链接:https://www.haomeiwen.com/subject/pfuhqktx.html