日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

elasticsearch和php,快速开始 | Elasticsearch-PHP | Elastic

發(fā)布時間:2023/12/2 php 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 elasticsearch和php,快速开始 | Elasticsearch-PHP | Elastic 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

快速開始edit

這一節(jié)會概述一下客戶端以及客戶端的一些主要方法的使用規(guī)則。

在 composer.json 文件中引入 elasticsearch-php:

{

"require": {

"elasticsearch/elasticsearch": "~6.0"

}

}

用 composer 安裝客戶端:

curl -s http://getcomposer.org/installer | php

php composer.phar install --no-dev

在項目中引入自動加載文件(如果還沒引入),并且實例化一個客戶端:

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

索引一個文檔edit

在 elasticsearch-php 中,幾乎一切操作都是用關(guān)聯(lián)數(shù)組來配置。REST 路徑(endpoint)、文檔和可選參數(shù)都是用關(guān)聯(lián)數(shù)組來配置。

為了索引一個文檔,我們要指定4部分信息:index,type,id 和一個 body。構(gòu)建一個鍵值對的關(guān)聯(lián)數(shù)組就可以完成上面的內(nèi)容。body 的鍵值對格式與文檔的數(shù)據(jù)保持一致性。(譯者注:如 ["testField" ? "abc"] 在文檔中則為 {"testField" : "abc"}):

$params = [

'index' => 'my_index',

'type' => 'my_type',

'id' => 'my_id',

'body' => ['testField' => 'abc']

];

$response = $client->index($params);

print_r($response);

收到的響應(yīng)數(shù)據(jù)表明,你指定的索引中已經(jīng)創(chuàng)建好了文檔。響應(yīng)數(shù)據(jù)是一個關(guān)聯(lián)數(shù)組,里面的內(nèi)容是 Elasticsearch 返回的decoded JSON 數(shù)據(jù):

Array

(

[_index] => my_index

[_type] => my_type

[_id] => my_id

[_version] => 1

[result] => created

[_shards] => Array

(

[total] => 2

[successful] => 1

[failed] => 0

)

[_seq_no] => 0

[_primary_term] => 1

)

獲取一個文檔edit

現(xiàn)在獲取剛才索引的文檔:

$params = [

'index' => 'my_index',

'type' => 'my_type',

'id' => 'my_id'

];

$response = $client->get($params);

print_r($response);

響應(yīng)數(shù)據(jù)包含一些元數(shù)據(jù)(如 index,type 等)和 _source 屬性,

這是你發(fā)送給 Elasticsearch 的原始文檔數(shù)據(jù)。

Array

(

[_index] => my_index

[_type] => my_type

[_id] => my_id

[_version] => 1

[found] => 1

[_source] => Array

(

[testField] => abc

)

)

搜索一個文檔edit

搜索是 elasticsearch 的一大特色,所以我們試一下執(zhí)行一個搜索。我們準(zhǔn)備用 Match 查詢來作為示范:

$params = [

'index' => 'my_index',

'type' => 'my_type',

'body' => [

'query' => [

'match' => [

'testField' => 'abc'

]

]

]

];

$response = $client->search($params);

print_r($response);

這個響應(yīng)數(shù)據(jù)與前面例子的響應(yīng)數(shù)據(jù)有所不同。這里有一些元數(shù)據(jù)(如 took, timed_out 等)和一個 hits 的數(shù)組,這代表了你的搜索結(jié)果。而 hits 內(nèi)部也有一個 hits 數(shù)組,內(nèi)部的 hits 包含特定的搜索結(jié)果:

Array

(

[took] => 16

[timed_out] =>

[_shards] => Array

(

[total] => 5

[successful] => 5

[skipped] => 0

[failed] => 0

)

[hits] => Array

(

[total] => 1

[max_score] => 0.2876821

[hits] => Array

(

[0] => Array

(

[_index] => my_index

[_type] => my_type

[_id] => my_id

[_score] => 0.2876821

[_source] => Array

(

[testField] => abc

)

)

)

)

)

刪除一個文檔edit

好了,現(xiàn)在我們看一下如何把之前添加的文檔刪除掉:

$params = [

'index' => 'my_index',

'type' => 'my_type',

'id' => 'my_id'

];

$response = $client->delete($params);

print_r($response);

你會注意到刪除文檔的語法與獲取文檔的語法是一樣的。唯一不同的是 delete 方法替代了 get 方法。下面響應(yīng)數(shù)據(jù)代表文檔已被刪除:

Array

(

[_index] => my_index

[_type] => my_type

[_id] => my_id

[_version] => 2

[result] => deleted

[_shards] => Array

(

[total] => 2

[successful] => 1

[failed] => 0

)

[_seq_no] => 1

[_primary_term] => 1

)

刪除一個索引edit

由于 elasticsearch 的動態(tài)特性,我們創(chuàng)建的第一個文檔會自動創(chuàng)建一個索引,同時也會把 settings 里面的參數(shù)設(shè)定為默認參數(shù)。由于我們在后面要指定特定的 settings,所以現(xiàn)在要刪除掉這個索引:

$deleteParams = [

'index' => 'my_index'

];

$response = $client->indices()->delete($deleteParams);

print_r($response);

響應(yīng)數(shù)據(jù)是:

Array

(

[acknowledged] => 1

)

創(chuàng)建一個索引edit

由于數(shù)據(jù)已被清空,我們可以重新開始了,現(xiàn)在要添加一個索引,同時要進行自定義 settings:

$params = [

'index' => 'my_index',

'body' => [

'settings' => [

'number_of_shards' => 2,

'number_of_replicas' => 0

]

]

];

$response = $client->indices()->create($params);

print_r($response);

Elasticsearch會創(chuàng)建一個索引,并配置你指定的參數(shù)值,然后返回一個消息確認:

Array

(

[acknowledged] => 1

[shards_acknowledged] => 1

[index] => my_index

)

本節(jié)結(jié)語edit

這里只是概述了一下客戶端以及它的語法。如果你很熟悉 elasticsearch,你會注意到這些方法的命名跟 REST 路徑(endpoint)是一樣的。

你也注意到了客戶端的參數(shù)配置從某種程度上講也是方便你的IDE易于搜索。$client 對象下的所有核心方法(索引,搜索,獲取等)都是可用的。索引管理和集群管理分別在 $client->indices() 和 $client->cluster() 中。

請查詢文檔的其余內(nèi)容以便知道整個客戶端的運作機制。

總結(jié)

以上是生活随笔為你收集整理的elasticsearch和php,快速开始 | Elasticsearch-PHP | Elastic的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。