好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHP中使用ElasticSearch最新實例講解

PHP中使用ElasticSearch最新實例講解

熱門標簽:福州人工外呼系統哪家強 釘釘打卡地圖標注 衡水外呼系統平臺 百度商鋪地圖標注 安裝電銷外呼系統 常州地圖標注服務商 注冊400電話申請 新河科技智能外呼系統怎么樣 地圖標注平臺怎么給錢注冊

網上很多關于ES的例子都過時了,版本很老,這篇文章的測試環境是ES6.5

通過composer安裝

composer require 'elasticsearch/elasticsearch'

在代碼中引入

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->setHosts(['172.16.55.53'])->build();

下面循序漸進完成一個簡單的添加和搜索的功能。

首先要新建一個index:

index對應關系型數據(以下簡稱MySQL)里面的數據庫,而不是對應MySQL里面的索引,這點要清楚

$params = [
  'index' => 'myindex', #index的名字不能是大寫和下劃線開頭
  'body' => [
    'settings' => [
      'number_of_shards' => 2,
      'number_of_replicas' => 0
    ]
  ]
];
$client->indices()->create($params);

在MySQL里面,光有了數據庫還不行,還需要建立表,ES也是一樣的,ES中的type對應MySQL里面的表。

注意:ES6以前,一個index有多個type,就像MySQL中一個數據庫有多個表一樣自然,但是ES6以后,每個index只允許一個type,在往以后的版本中很可能會取消type。

type不是單獨定義的,而是和字段一起定義

$params = [
  'index' => 'myindex',
  'type' => 'mytype',
  'body' => [
    'mytype' => [
      '_source' => [
        'enabled' => true
      ],
      'properties' => [
        'id' => [
          'type' => 'integer'
        ],
        'first_name' => [
          'type' => 'text',
          'analyzer' => 'ik_max_word'
        ],
        'last_name' => [
          'type' => 'text',
          'analyzer' => 'ik_max_word'
        ],
        'age' => [
          'type' => 'integer'
        ]
      ]
    ]
  ]
];
$client->indices()->putMapping($params);

在定義字段的時候,可以看出每個字段可以定義單獨的類型,在first_name中還自定義了分詞器 ik,

這個分詞器是一個插件,需要單獨安裝的,參考另一篇文章:ElasticSearch基本嘗試

現在數據庫和表都有了,可以往里面插入數據了

概念:這里的 數據 在ES中叫文檔

$params = [
  'index' => 'myindex',
  'type' => 'mytype',
  //'id' => 1, #可以手動指定id,也可以不指定隨機生成
  'body' => [
    'first_name' => '張',
    'last_name' => '三',
    'age' => 35
  ]
];
$client->index($params);

多插入一點數據,然后來看看怎么把數據取出來:

通過id取出單條數據:

插曲:如果你之前添加文檔的時候沒有傳入id,ES會隨機生成一個id,這個時候怎么通過id查?id是多少都不知道啊。

所以這個插入一個簡單的搜索,最簡單的,一個搜索條件都不要,返回所有index下所有文檔:

$data = $client->search();

現在可以去找一找id了,不過你會發現id可能長這樣:zU65WWgBVD80YaV8iVMk,不要驚訝,這是ES隨機生成的。

現在可以通過id查找指定文檔了:

$params = [
  'index' => 'myindex',
  'type' => 'mytype',
  'id' =>'zU65WWgBVD80YaV8iVMk'
];
$data = $client->get($params);

最后一個稍微麻煩點的功能:

注意:這個例子我不打算在此詳細解釋,看不懂沒關系,這篇文章主要的目的是基本用法,并沒有涉及到ES的精髓地方,

ES精髓的地方就在于搜索,后面的文章我會繼續深入分析

$query = [
  'query' => [
    'bool' => [
      'must' => [
        'match' => [
          'first_name' => '張',
        ]
      ],
      'filter' => [
        'range' => [
          'age' => ['gt' => 76]
        ]
      ]
    ]

  ]
];
$params = [
  'index' => 'myindex',
// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至這兩個參數都是可選的
  'type' => 'mytype',
  '_source' => ['first_name','age'], // 請求指定的字段
  'body' => array_merge([
    'from' => 0,
    'size' => 5
  ],$query)
];
$data = $this->EsClient->search($params);

上面的是一個簡單的使用流程,但是不夠完整,只講了添加文檔,沒有說怎么刪除文檔,

下面我貼出完整的測試代碼,基于Laravel環境,當然環境只影響運行,不影響理解,包含基本的常用操作:    

?php
use Elasticsearch\ClientBuilder;
use Faker\Generator as Faker;
/**
* ES 的 php 實測代碼
*/
class EsDemo {
	private $EsClient = null;
	private $faker = null;
	/**
* 為了簡化測試,本測試默認只操作一個Index,一個Type,
* 所以這里固定為 megacorp和employee
*/
	private $index = 'megacorp';
	private $type = 'employee';
	public function __construct(Faker $faker) {
		/**
* 實例化 ES 客戶端
*/
		$this->EsClient = ClientBuilder::create()->setHosts(['172.16.55.53'])->build();
		/**
* 這是一個數據生成庫,詳細信息可以參考網絡
*/
		$this->faker = $faker;
	}
	/**
* 批量生成文檔
* @param $num
*/
	public function generateDoc($num = 100) {
		foreach (range(1,$num) as $item) {
			$this->putDoc([
			'first_name' => $this->faker->name,
			'last_name' => $this->faker->name,
			'age' => $this->faker->numberBetween(20,80)
			]);
		}
	}
	/**
* 刪除一個文檔
* @param $id
* @return array
*/
	public function delDoc($id) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'id' =>$id
		];
		return $this->EsClient->delete($params);
	}
	/**
* 搜索文檔,query是查詢條件
* @param array $query
* @param int $from
* @param int $size
* @return array
*/
	public function search($query = [], $from = 0, $size = 5) {
		// $query = [
		// 'query' => [
		// 'bool' => [
		// 'must' => [
		// 'match' => [
		// 'first_name' => 'Cronin',
		// ]
		// ],
		// 'filter' => [
		// 'range' => [
		// 'age' => ['gt' => 76]
		// ]
		// ]
		// ]
		//
		// ]
		// ];
		$params = [
		'index' => $this->index,
		// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至這兩個參數都是可選的
		'type' => $this->type,
		'_source' => ['first_name','age'], // 請求指定的字段
		'body' => array_merge([
		'from' => $from,
		'size' => $size
		],$query)
		];
		return $this->EsClient->search($params);
	}
	/**
* 一次獲取多個文檔
* @param $ids
* @return array
*/
	public function getDocs($ids) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'body' => ['ids' => $ids]
		];
		return $this->EsClient->mget($params);
	}
	/**
* 獲取單個文檔
* @param $id
* @return array
*/
	public function getDoc($id) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'id' =>$id
		];
		return $this->EsClient->get($params);
	}
	/**
* 更新一個文檔
* @param $id
* @return array
*/
	public function updateDoc($id) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'id' =>$id,
		'body' => [
		'doc' => [
		'first_name' => '張',
		'last_name' => '三',
		'age' => 99
		]
		]
		];
		return $this->EsClient->update($params);
	}
	/**
* 添加一個文檔到 Index 的Type中
* @param array $body
* @return void
*/
	public function putDoc($body = []) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		// 'id' => 1, #可以手動指定id,也可以不指定隨機生成
		'body' => $body
		];
		$this->EsClient->index($params);
	}
	/**
* 刪除所有的 Index
*/
	public function delAllIndex() {
		$indexList = $this->esStatus()['indices'];
		foreach ($indexList as $item => $index) {
			$this->delIndex();
		}
	}
	/**
* 獲取 ES 的狀態信息,包括index 列表
* @return array
*/
	public function esStatus() {
		return $this->EsClient->indices()->stats();
	}
	/**
* 創建一個索引 Index (非關系型數據庫里面那個索引,而是關系型數據里面的數據庫的意思)
* @return void
*/
	public function createIndex() {
		$this->delIndex();
		$params = [
		'index' => $this->index,
		'body' => [
		'settings' => [
		'number_of_shards' => 2,
		'number_of_replicas' => 0
		]
		]
		];
		$this->EsClient->indices()->create($params);
	}
	/**
* 檢查Index 是否存在
* @return bool
*/
	public function checkIndexExists() {
		$params = [
		'index' => $this->index
		];
		return $this->EsClient->indices()->exists($params);
	}
	/**
* 刪除一個Index
* @return void
*/
	public function delIndex() {
		$params = [
		'index' => $this->index
		];
		if ($this->checkIndexExists()) {
			$this->EsClient->indices()->delete($params);
		}
	}
	/**
* 獲取Index的文檔模板信息
* @return array
*/
	public function getMapping() {
		$params = [
		'index' => $this->index
		];
		return $this->EsClient->indices()->getMapping($params);
	}
	/**
* 創建文檔模板
* @return void
*/
	public function createMapping() {
		$this->createIndex();
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'body' => [
		$this->type => [
		'_source' => [
		'enabled' => true
		],
		'properties' => [
		'id' => [
		'type' => 'integer'
		],
		'first_name' => [
		'type' => 'text',
		'analyzer' => 'ik_max_word'
		],
		'last_name' => [
		'type' => 'text',
		'analyzer' => 'ik_max_word'
		],
		'age' => [
		'type' => 'integer'
		]
		]
		]
		]
		];
		$this->EsClient->indices()->putMapping($params);
		$this->generateDoc();
	}
}

到此這篇關于PHP中使用ElasticSearch最新實例講解的文章就介紹到這了,更多相關PHP中使用ElasticSearch最內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 時間輕松學會.NET Core操作ElasticSearch7的方法
  • Java調用elasticsearch本地代碼的操作方法
  • 使用elasticsearch定時刪除索引數據
  • Elasticsearch工具cerebro的安裝與使用教程
  • Java elasticSearch-api的具體操作步驟講解

標簽:白城 鶴崗 克拉瑪依 六安 遼陽 唐山 鷹潭 柳州

巨人網絡通訊聲明:本文標題《PHP中使用ElasticSearch最新實例講解》,本文關鍵詞  PHP,中,使用,ElasticSearch,最新,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP中使用ElasticSearch最新實例講解》相關的同類信息!
  • 本頁收集關于PHP中使用ElasticSearch最新實例講解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 17C娇喘????白丝| 亚洲伊人久久综合影院2021| 国产精品色情国产三级在电影观看 | 精品无码国产自产在线观看水浒传| 上课被同桌摸下面摸出水来了| 欧美一级精品片在线看| 3D黄漫?18禁视频免费看网站| 亚洲免费三区| 日进去| ai人脸替换李知恩造梦| 久久久久久亚洲精品美女| 国产女教师一级爽A片69视频| 韩国漫画大全免费完整版观看| 亚洲国产天堂在线网址| 强势标记小说免费观看| 亚洲AV老汉天堂| 少妇高潮喷水久久久久久久久| 黄污软件| 春宵苦短少女前进吧| 亚洲精品久久久狠狠爱小说| 裸吊自慰喷浓精Gay2023| 印度丰满三级在线播放| 一级一级一级毛片| 欧美一级黃色A片免费看蜜桃熟了| 抵债的男老婆(女攻)的文案| 92国产视频| 国富产二代破解版| 妈咪湿透的三角裤| 免费?无码?国产成年大黄| 欧美视频一区二区三区四区| 日本漫画之口工番h| 久久香蕉电影| 被20个男人灌了一夜精子| av亚洲产国偷v产偷v自拍小说| 青娱乐视屏| 丁小伟和周谨行| 老师腿开大点我添添公视频| 高H全肉NP双龙男男| 亚洲精品综合在线| 91私密保健女子养生spa| 日韩日批|