99久久国产露脸精品麻豆,欧美日韩精品小说,亚洲免费在线美女视频,国产三级中文字幕,91极品国产情侣高潮对白,国产亚洲一区二区三区不卡片,欧美jizz精品欧美性,久久国产精品久久国产片

Elasticsearch基本的CURD

袁志蒙 1818次瀏覽

摘要:前面介紹過(guò)Elasticsearch是面向文檔的存儲(chǔ)引擎,存儲(chǔ)的數(shù)據(jù)是以JSON文檔為基本單位進(jìn)行的,本文主要介紹在PHP中怎么對(duì)ES中文檔的CRUD操作。...

前面介紹過(guò)Elasticsearch是面向文檔的存儲(chǔ)引擎,存儲(chǔ)的數(shù)據(jù)是以JSON文檔為基本單位進(jìn)行的,本文主要介紹在PHP中怎么對(duì)ES中文檔的CRUD操作。

1.什么是文檔

在Elasticsearch中,文檔其實(shí)就是一條JSON數(shù)據(jù),SON數(shù)據(jù)格式可以非常復(fù)雜,也可以很簡(jiǎn)單。


2.什么是文檔元數(shù)據(jù)

文檔元數(shù)據(jù),指的是插入JSON文檔的時(shí)候,Elasticsearch為這條數(shù)據(jù),自動(dòng)生成的系統(tǒng)字段。

元數(shù)據(jù)的字段名都是以下劃線開(kāi)頭的,常見(jiàn)的元數(shù)據(jù)如下:

_index - 代表當(dāng)前JSON文檔所屬的文檔名字

_type - 代表當(dāng)前JSON文檔所屬的類(lèi)型,雖然新版ES廢棄了type的用法,但是元數(shù)據(jù)還是可以看到。

_id - 文檔唯一Id, 如果我們沒(méi)有為文檔指定id,系統(tǒng)會(huì)自動(dòng)生成

_source - 代表我們插入進(jìn)去的JSON數(shù)據(jù)

_version - 文檔的版本號(hào),每修改一次文檔數(shù)據(jù),字段就會(huì)加1, 這個(gè)字段新版的ES已經(jīng)不使用了

_seq_no - 文檔的版本號(hào), 替代老的_version字段

_primary_term - 文檔所在主分區(qū),這個(gè)可以跟_seq_no字段搭配實(shí)現(xiàn)樂(lè)觀鎖。

例如下面是從ES查詢出來(lái)的一條文檔的例子:

{
  "_index" : "order",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1, // 老ES版本的文檔版本號(hào),最新版本已經(jīng)不使用了
  "_seq_no" : 0, // 新ES版本的文檔版本號(hào)
  "_primary_term" : 1, // 主分區(qū)id
  "_source" : { // _source字段保存的就是我們插入到ES中的JSON數(shù)據(jù)
    "id" : 1,
    "status" : 1,
    "total_price" : 100,
    "create_time" : "2021-12-12 12:20:22",
    "user" : {
      "id" : 11,
      "username" : "yzmcms",
      "phone" : "18801108888",
      "address" : "北京市海淀區(qū)"
    }
  }
}

3.在PHP中操作ES:

在PHP中我們對(duì)ES進(jìn)行操作時(shí),我們要用到 elasticsearch-php 依賴包,在 composer.json 文件中引入 elasticsearch-php:

{
    "require": {
        "elasticsearch/elasticsearch": "~7.0"
    }
}

用 composer 安裝客戶端:

curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev

在項(xiàng)目中引入自動(dòng)加載文件(如果還沒(méi)引入),并且實(shí)例化一個(gè)客戶端:

require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();

4.查詢文檔(基本查詢):

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

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

5.添加文檔:

$params = [
	'body' => '我是內(nèi)容',
	'id' => 'article_1',
	'index' => 'articles_index',
	'type' => 'articles_type'
];
//批量添加時(shí),也可以使用 bulk 方法
$response = $this->client->index($params);
print_r($response);

6.刪除文檔:

$params = [
	'index' => 'articles_index',
	'type' => 'articles_type',
	'id' => 'article_1'
];
$response = $this->client->delete($params);
print_r($response)

7.更新文檔:

$params = [
	'index' => 'articles_index',
	'type' => 'articles_type',
	'id' => 'article_1',
	'body' => [
		'doc' => [  // 必須帶上這個(gè).表示是文檔操作
			'title' => '我是新的標(biāo)題文檔哈'
		]
	]
];
$response = $this->client->update($params);
print_r($response)


隨機(jī)內(nèi)容

表情

共0條評(píng)論
  • 這篇文章還沒(méi)有收到評(píng)論,趕緊來(lái)?yè)屔嘲l(fā)吧~