PHP操作Elasticsearch7.6


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

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

安裝操作Elasticsearch的PHP庫

我們使用TP5來作為示例

首先需要安裝操作Elasticsearch的PHP客戶端庫,我們打開https://packagist.org/,搜索Elasticsearch。

這里有個Elasticsearch-PHP和Elasticsearch版本的對照表,我們需要根據我們自己使用的Elasticsearch的版本下載對應的Elasticsearch-PHP

由於我的Elasticsearch版本是7.6.2,所以這里我們可以下載最新的Elasticsearch-PHP版本為7.8.0

我們進入到自己的項目目錄里安裝Elasticsearch-PHP

composer require elasticsearch/elasticsearch=7.8.*

PHP連接Elasticsearch

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

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

創建索引和映射

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

$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);

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM