PHP Elasticsearch的基本使用方法

小云云
發布: 2023-03-21 07:56:01
原創
36620 人瀏覽過

對於Elasticsearch與Elasticsearch-php的安裝,網路上有比較多的教程,這裡不再累述。只是要注意Elasticsearch、Elasticsearch-php與php的版本。這裡筆者使用的是Elasticsearch 5.6.8 windows版、php 5.6 、php onethink框架(以下簡稱ot)、Elasticsearch-php composer如下:(PHP Composer 影片教學

{  
    "require":{  
        "elasticsearch/elasticsearch" : "~5.0"  
    }  
}
登入後複製

課程推薦→:《elasticsearch全文搜尋實戰》(實戰影片)

來自課程《千萬級資料並發解決方案(理論+實戰)》

一、連接Elasticsearch:

1、Elasticsearch開啟之後,可以直接透過http://127.0.0.1:9200 /查看基本資訊。

2、將composer vendor下的檔案複製到ot ThinkPHP\Library\Vendor\elasticsearch目錄下。

3、連接Elasticsearch,

    public $es;

    /**
     * 初始化
     */
    public function _initialize()
    {
        Vendor('elasticsearch.autoload');
        //host数组可配置多个节点
        $params = array(
            '127.0.0.1:9200'
        );
        $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();
    }
登入後複製

其中build()方法會將ClientBuilder 物件轉換為一個Client物件。

二、Elasticsearch-php使用:

1、創造index:

關於index與type,這裡特別修正一個說法,index 就像關係型資料庫裡的database, type 就像database 裡的table,這種理解是錯誤的。

    /**
     * 创建索引
     */
    public function createIndex(){
        $params = [
            'index' => 'test', //索引名称
            'body' => [
                'settings'=> [ //配置
                    'number_of_shards'=> 3,//主分片数
                    'number_of_replicas'=> 1 //主分片的副本数
                ],
                'mappings'=> [  //映射
                    '_default_' => [ //默认配置,每个类型缺省的配置使用默认配置
                        '_all'=>[   //  关闭所有字段的检索
                            'enabled' => 'false'
                        ],
                        '_source'=>[   //  存储原始文档
                            'enabled' => 'true'
                        ],
                        'properties'=> [ //配置数据结构与类型
                            'name'=> [ //字段1
                                'type'=>'string',//类型 string、integer、float、double、boolean、date
                                'index'=> 'analyzed',//索引是否精确值  analyzed not_analyzed
                            ],
                            'age'=> [ //字段2
                                'type'=>'integer',
                            ],
                            'sex'=> [ //字段3
                                'type'=>'string',
                                'index'=> 'not_analyzed', 
                            ],
                        ]
                    ],
                    'my_type' => [ 
                        'properties' => [
                            'phone'=> [ 
                                'type'=>'string',
                            ],                            
                        ]
                    ],
                ],
            ]
        ];

        $res = $this->es->indices()->create($params);
    }
登入後複製

在使用Elasticsearch-php API的時候,參數$params一般是用陣列來,因為陣列結構能很方便的轉換為json。其中

_default_是預設配置,其他配置的預設值都與_default_的相同。

_all設定true會將所有原始文檔拼接在一起額外存儲,

_source設定為true會儲存原始文檔,設定false一般用在只需要索引出文檔的標題或Url,透過Url去存取文檔,而不需要在es中儲存一份文檔內容的場景。

最後,注意同一index下不同type中的同名稱欄位的資料類型與配置也必須相同

2、刪除index:

    /**
     * 删除索引
     */
     public function deleteIndex(){
        $params = [
            'index' => 'test'
        ];

        $res = $this->es->indices()->delete($params);
     }
登入後複製

3、查看Mappings:

    public function getMappings(){
        $params = [
            'index' => 'test'
        ];

        $res = $this->es->indices()->getMapping($params);
    }
登入後複製

4、修改Mappings:

    public function putMappings(){
        $params = [           
            'index' => 'test',
            'type' => 'my_type',
            'body' => [
                'my_type' => [
                    'properties' => [
                        'idcard' => [
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ]; 

        $res = $this->es->indices()->putMapping($params);      
    }
登入後複製

注意:修改Mappings的API必須要指明type,且只能加,不能修改已有的屬性。

5、插入單條 Document:

    public function postSinDoc(){
        $params = [
            'index' => 'test',
            'type' => 'my_type',
            'body' => [ 
                'age' => 17,
                'name' => 'saki',
                'sex' => '女性',
                'idcard' => 1112,
                'phone' => '1245789',
            ]
        ];

        $res = $this->es->index($params);
    }
登入後複製

6、插入多條Document:

    public function postBulkDoc(){
        for($i = 0; $i < 5; $i++) {
            $params[&#39;body&#39;][] = [
                &#39;index&#39; => [
                    &#39;_index&#39; => &#39;test&#39;,
                    &#39;_type&#39; => &#39;my_type&#39;,
                ]
            ];

            $params[&#39;body&#39;][] = [
                &#39;age&#39; => 17+$i,
                &#39;name&#39; => &#39;reimu&#39;.$i,
                &#39;sex&#39; => &#39;女性&#39;,
                &#39;idcard&#39; => 1112+$i,
                &#39;phone&#39; => &#39;1245789&#39;.$i,
            ];
        }

        $res = $this->es->bulk($params);
    }
登入後複製

7、透過id取得Document:

    public function getDocById(){
        $params = [
            &#39;index&#39; => &#39;test&#39;,
            &#39;type&#39; => &#39;my_type&#39;,
            &#39;id&#39; => &#39;AWIDV5l2A907wJBVKu6k&#39;
        ];

        $res = $this->es->get($params);
    }
登入後複製

8、通過id更新Document:

    public function updateDocById(){
        $params = [
            &#39;index&#39; => &#39;test&#39;,
            &#39;type&#39; => &#39;my_type&#39;,
            &#39;id&#39; => &#39;AWIDV5l2A907wJBVKu6k&#39;,
            &#39;body&#39; => [
                &#39;doc&#39; => [ //将doc中的文档与现有文档合并
                    &#39;name&#39; => &#39;marisa&#39;
                ]
            ]
        ];

        $res = $this->es->update($params);
    }
登入後複製

9、透過id刪除Document:

    public function deleteDocById(){
        $params = [
            &#39;index&#39; => &#39;test&#39;,
            &#39;type&#39; => &#39;my_type&#39;,
            &#39;id&#39; => &#39;AWIDV5l2A907wJBVKu6k&#39;
        ];

        $res = $this->es->delete($params);
    }
登入後複製

注意:以上透過id的三個操作,如果未找到id,Elasticsearch-php會直接報錯!

10、搜尋Document:

    public function searchDoc(){
        $params = [
            &#39;index&#39; => &#39;test&#39;,
            &#39;type&#39; => &#39;my_type&#39;,
            &#39;body&#39; => [
                &#39;query&#39; => [
                    &#39;constant_score&#39; => [ //非评分模式执行
                        &#39;filter&#39; => [ //过滤器,不会计算相关度,速度快
                            &#39;term&#39; => [ //精确查找,不支持多个条件
                                &#39;name&#39; => &#39;reimu0&#39;
                            ]
                        ]
                        
                    ]
                ]
            ]
        ];

        $res = $this->es->search($params);
登入後複製

這裡只是搜尋的一個範例。

相關推薦:

實例詳解MySQL 與 Elasticsearch 資料不對稱問題

Elasticsearch是什麼? Elasticsearch 能夠被用在什麼地方?

Elasticsearch索引與文件操作實例教學

以上是PHP Elasticsearch的基本使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!