PHP操做Elasticsearch7.6

首先打開Elasticsearch官網瞭解對應編程語言的API https://www.elastic.co/guide/en/elasticsearch/client/index.htmlphp

點擊 PHP API便可查看當前7.X版本的文檔內容了html

安裝操做Elasticsearch的PHP庫

咱們使用TP5來做爲示例shell

首先須要安裝操做Elasticsearch的PHP客戶端庫,咱們打開https://packagist.org/,搜索Elasticsearch。編程

這裏有個Elasticsearch-PHP和Elasticsearch版本的對照表,咱們須要根據咱們本身使用的Elasticsearch的版本下載對應的Elasticsearch-PHPapi

因爲個人Elasticsearch版本是7.6.2,因此這裏咱們能夠下載最新的Elasticsearch-PHP版本爲7.8.0數組

咱們進入到本身的項目目錄裏安裝Elasticsearch-PHPapp

composer require elasticsearch/elasticsearch=7.8.*

PHP鏈接Elasticsearch

官方配置文檔:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.htmlcomposer

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
var_dump($client);

建立索引和映射

建立一個名爲users的索引同時建立映射,並制定映射中各個字段的類型elasticsearch

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'users',
    'body' => [
        'settings' => [
            'number_of_shards' => 3,
            'number_of_replicas' => 2
        ],
        'mappings' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'name' => [
                    'type' => 'keyword'
                ],
                'age' => [
                    'type' => 'integer'
                ],
                'mobile' => [
                    'type' => 'text'
                ],
                'email' => [
                    'type' => 'text'
                ],
                'birthday' => [
                    'type' => 'date'
                ],
                'address' => [
                    'type' => 'text'
                ]
            ]
        ]
    ]
];


// Create the index with mappings and settings now
$response = $client->indices()->create($params);
dump($response);

添加文檔

當你要在 Elasticsearch 增長文檔時,你就須要索引 JSON 文檔。JSON 文檔會映射 PHP 關聯數組,由於 PHP 關聯數組能夠 encode 爲 JSON 數據格式。編程語言

所以在 Elasticsearch-PHP 中你能夠傳遞關聯數組給客戶端來索引文檔。咱們會概述幾種方法來增長文檔到 Elasticsearch。

單一文檔索引

當索引一個文檔時,你能夠提供一個 ID 或者讓 Elasticsearch 自動生成。

如今有以下數據,咱們將其添加到users索引中

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    'index' => 'users',
    'id'    => 1,
    'body'  => [
        'name'     => '張三',
        'age'      => 10,
        'email'    => 'zs@gmail.com',
        'birthday' => '1990-12-12',
        'address'  => '北京'
    ]
];
$client->index($params);

經過Kibana能夠查看到已經成功添加到Elasticsearch中

批量(bulk)索引

Elasticsearch 也支持批量(bulk)索引文檔。bulk API 要求提供 JSON 格式的 action/元數據 鍵值對。在 PHP 中構建批量文檔數據也是類似的。你首先要建立一個 action 數組對象(如 index 對象),而後你還要建立一個 body 對象。而 PHP 程序則重複上述操做構建文檔數據。

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$arr = [
    ['name' => '張三', 'age' => 10, 'email' => 'zs@gmail.com', 'birthday' => '1990-12-12', 'address' => '北京'],
    ['name' => '李四', 'age' => 20, 'email' => 'ls@gmail.com', 'birthday' => '1990-10-15', 'address' => '河南'],
    ['name' => '白兮', 'age' => 15, 'email' => 'bx@gmail.com', 'birthday' => '1970-08-12', 'address' => '杭州'],
    ['name' => '王五', 'age' => 25, 'email' => 'ww@gmail.com', 'birthday' => '1980-12-01', 'address' => '四川'],
];

foreach ($arr as $key => $document) {
    $params['body'][] = [
        'index' => [
            '_index' => 'users',
            '_id'    => $key
        ]
    ];

    $params['body'][] = [
        'name'     => $document['name'],
        'age'      => $document['age'],
        'email'    => $document['email'],
        'birthday' => $document['birthday'],
        'address'  => $document['address']
    ];
}
if (isset($params) && !empty($params)) {
    $client->bulk($params);
}

若是數據量很少能夠用上面的方法,若是數據量不少的話,咱們就能夠考慮分次添加

獲取文檔

Elasticsearch 提供實時獲取文檔的方法。這意味着只要文檔被索引且客戶端收到消息確認後,你就能夠當即在任何的分片中檢索文檔。Get 操做經過 index/type/id 方式請求一個文檔信息:

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'users',
    'id'    => 1
];
$response = $client->get($params);
dump($response);

更新文檔

部分更新

若是你要部分更新文檔(如更改現存字段,或添加新字段),你能夠在 body 參數中指定一個 doc 參數。這樣 doc 參數內的字段會與現存字段進行合併。

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'users',
    'id'    => 1,
    'body'  => [
        'doc' => [
            'mobile' => '17612345678'
        ]
    ]
];
$response = $client->update($params);
dump($response);

script更新

有時你要執行一個腳原本進行更新操做,如對字段進行自增操做或添加新字段。爲了執行一個腳本更新,你要提供腳本命令和一些參數:

例如:將李四的年齡增長5歲

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'users',
    'id'    => '1',
    'body'  => [
        'script' => 'ctx._source.age += 5',
    ]
];
$response = $client->update($params);
dump($response);

經過Kibana查看發現年齡已經增長了5歲

刪除文檔

經過指定文檔的 /index/type/id 路徑能夠刪除文檔:

$hosts = [
    '127.0.0.1:9200', //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'users',
    'id'    => 2,
];
$response = $client->delete($params);
dump($response);

若是該文章對您有幫助,請您點個推薦,感謝。

相關文章
相關標籤/搜索