首頁 > 後端開發 > php教程 > PHP中使用Elasticsearch的方法

PHP中使用Elasticsearch的方法

不言
發布: 2023-04-02 19:28:01
原創
8997 人瀏覽過

這篇文章主要介紹了關於PHP中使用Elasticsearch的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

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

來自課程

《千萬級資料並發解決方案(理論實戰)》

PHP中使用Elasticsearch

composer require elasticsearch/elasticsearch
登入後複製

會自動載入適當的版本!我的php是5.6的,它會自動載入5.3的elasticsearch版本!

Using version ^5.3 for elasticsearch/elasticsearch
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing react/promise (v2.7.0): Downloading (100%)         
  - Installing guzzlehttp/streams (3.0.0): Downloading (100%)         
  - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%)         
  - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%)         
Writing lock file
Generating autoload files
登入後複製
簡單使用
<?php

class MyElasticSearch
{
    private $es;
    // 构造函数
    public function __construct()
    {
        include(&#39;../vendor/autoload.php&#39;);
        $params = array(
            &#39;127.0.0.1:9200&#39;
        );
        $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();
    }

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

                    ]
                ]
            ]
        ];

        $res = $this->es->search($params);

        print_r($res);
    }
}
登入後複製
<?php
require "./MyElasticSearch.php";

$es = new MyElasticSearch();

$es->search();
登入後複製

執行結果

Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 3
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => 李
                                    [last_name] => 四
                                    [age] => 24
                                    [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。
                                    [interests] => Array
                                        (
                                            [0] => 英雄联盟
                                        )

                                )

                        )

                )

        )

)
登入後複製

以下是官方的一些範例:初始化

require &#39;../vendor/autoload.php&#39;;
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
登入後複製

#增加設定# ##
$hosts = [
    &#39;127.0.01:9200&#39;,         // IP + Port
];

$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
->setHosts($hosts)      // Set the hosts
->build();              // Build the client object
登入後複製
###或###
$hosts = [
    &#39;127.0.01:9200&#39;,         // IP + Port
];

$clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder
$clientBuilder->setHosts($hosts);           // Set the hosts
$client = $clientBuilder->build();          // Build the client object
登入後複製
###插入一個文件###
// Index 一个文档
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;,
    &#39;body&#39; => [&#39;testField&#39; => &#39;abc&#39;]
];

$response = $client->index($params);
print_r($response);
登入後複製
###取得一個文件###
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;
];

$response = $client->get($params);
print_r($response);
登入後複製
###查詢一個文件###
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;body&#39; => [
        &#39;query&#39; => [
            &#39;match&#39; => [
                &#39;testField&#39; => &#39;abc&#39;
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);
登入後複製
###刪除一個文件###
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;
];

$response = $client->delete($params);
print_r($response);
登入後複製
###結果如下###
Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 3
    [result] => deleted
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 2
    [_primary_term] => 1
)
登入後複製
###刪除一個索引###
$deleteParams = [
    &#39;index&#39; => &#39;my_index&#39;
];
$response = $client->indices()->delete($deleteParams);
print_r($response);
登入後複製
###建立一個索引###
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;body&#39; => [
        &#39;settings&#39; => [
            &#39;number_of_shards&#39; => 2,
            &#39;number_of_replicas&#39; => 0
        ]
    ]
];

$response = $client->indices()->create($params);
print_r($response);
登入後複製
###以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網! ###### 相關推薦:#########PHP資料結構基礎堆疊################PHP運算Beanstalkd的方法與參數註解################### ######

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板