最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP中使用ElasticSearch最新實(shí)例講解

 更新時(shí)間:2021年03月18日 10:58:39   作者:李留廣  
這篇文章主要介紹了PHP中使用ElasticSearch最新實(shí)例講解,這篇文章的教程是比較詳細(xì),有需要的同學(xué)可以研究下

網(wǎng)上很多關(guān)于ES的例子都過(guò)時(shí)了,版本很老,這篇文章的測(cè)試環(huán)境是ES6.5

通過(guò)composer安裝

composer require 'elasticsearch/elasticsearch'

在代碼中引入

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

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

下面循序漸進(jìn)完成一個(gè)簡(jiǎn)單的添加和搜索的功能。

首先要新建一個(gè)index:

index對(duì)應(yīng)關(guān)系型數(shù)據(jù)(以下簡(jiǎn)稱(chēng)MySQL)里面的數(shù)據(jù)庫(kù),而不是對(duì)應(yīng)MySQL里面的索引,這點(diǎn)要清楚

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

在MySQL里面,光有了數(shù)據(jù)庫(kù)還不行,還需要建立表,ES也是一樣的,ES中的type對(duì)應(yīng)MySQL里面的表。

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

type不是單獨(dú)定義的,而是和字段一起定義

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

在定義字段的時(shí)候,可以看出每個(gè)字段可以定義單獨(dú)的類(lèi)型,在first_name中還自定義了分詞器 ik,

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

現(xiàn)在數(shù)據(jù)庫(kù)和表都有了,可以往里面插入數(shù)據(jù)了

概念:這里的 數(shù)據(jù) 在ES中叫文檔

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

多插入一點(diǎn)數(shù)據(jù),然后來(lái)看看怎么把數(shù)據(jù)取出來(lái):

通過(guò)id取出單條數(shù)據(jù):

插曲:如果你之前添加文檔的時(shí)候沒(méi)有傳入id,ES會(huì)隨機(jī)生成一個(gè)id,這個(gè)時(shí)候怎么通過(guò)id查?id是多少都不知道啊。

所以這個(gè)插入一個(gè)簡(jiǎn)單的搜索,最簡(jiǎn)單的,一個(gè)搜索條件都不要,返回所有index下所有文檔:

$data = $client->search();

現(xiàn)在可以去找一找id了,不過(guò)你會(huì)發(fā)現(xiàn)id可能長(zhǎng)這樣:zU65WWgBVD80YaV8iVMk,不要驚訝,這是ES隨機(jī)生成的。

現(xiàn)在可以通過(guò)id查找指定文檔了:

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

最后一個(gè)稍微麻煩點(diǎn)的功能:

注意:這個(gè)例子我不打算在此詳細(xì)解釋?zhuān)床欢疀](méi)關(guān)系,這篇文章主要的目的是基本用法,并沒(méi)有涉及到ES的精髓地方,

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

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

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

上面的是一個(gè)簡(jiǎn)單的使用流程,但是不夠完整,只講了添加文檔,沒(méi)有說(shuō)怎么刪除文檔,

下面我貼出完整的測(cè)試代碼,基于Laravel環(huán)境,當(dāng)然環(huán)境只影響運(yùn)行,不影響理解,包含基本的常用操作:    

<?php
use Elasticsearch\ClientBuilder;
use Faker\Generator as Faker;
/**
* ES 的 php 實(shí)測(cè)代碼
*/
class EsDemo {
	private $EsClient = null;
	private $faker = null;
	/**
* 為了簡(jiǎn)化測(cè)試,本測(cè)試默認(rèn)只操作一個(gè)Index,一個(gè)Type,
* 所以這里固定為 megacorp和employee
*/
	private $index = 'megacorp';
	private $type = 'employee';
	public function __construct(Faker $faker) {
		/**
* 實(shí)例化 ES 客戶端
*/
		$this->EsClient = ClientBuilder::create()->setHosts(['172.16.55.53'])->build();
		/**
* 這是一個(gè)數(shù)據(jù)生成庫(kù),詳細(xì)信息可以參考網(wǎng)絡(luò)
*/
		$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)
			]);
		}
	}
	/**
* 刪除一個(gè)文檔
* @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 是可以模糊匹配的,甚至這兩個(gè)參數(shù)都是可選的
		'type' => $this->type,
		'_source' => ['first_name','age'], // 請(qǐng)求指定的字段
		'body' => array_merge([
		'from' => $from,
		'size' => $size
		],$query)
		];
		return $this->EsClient->search($params);
	}
	/**
* 一次獲取多個(gè)文檔
* @param $ids
* @return array
*/
	public function getDocs($ids) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'body' => ['ids' => $ids]
		];
		return $this->EsClient->mget($params);
	}
	/**
* 獲取單個(gè)文檔
* @param $id
* @return array
*/
	public function getDoc($id) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		'id' =>$id
		];
		return $this->EsClient->get($params);
	}
	/**
* 更新一個(gè)文檔
* @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);
	}
	/**
* 添加一個(gè)文檔到 Index 的Type中
* @param array $body
* @return void
*/
	public function putDoc($body = []) {
		$params = [
		'index' => $this->index,
		'type' => $this->type,
		// 'id' => 1, #可以手動(dòng)指定id,也可以不指定隨機(jī)生成
		'body' => $body
		];
		$this->EsClient->index($params);
	}
	/**
* 刪除所有的 Index
*/
	public function delAllIndex() {
		$indexList = $this->esStatus()['indices'];
		foreach ($indexList as $item => $index) {
			$this->delIndex();
		}
	}
	/**
* 獲取 ES 的狀態(tài)信息,包括index 列表
* @return array
*/
	public function esStatus() {
		return $this->EsClient->indices()->stats();
	}
	/**
* 創(chuàng)建一個(gè)索引 Index (非關(guān)系型數(shù)據(jù)庫(kù)里面那個(gè)索引,而是關(guān)系型數(shù)據(jù)里面的數(shù)據(jù)庫(kù)的意思)
* @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);
	}
	/**
* 刪除一個(gè)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);
	}
	/**
* 創(chuàng)建文檔模板
* @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();
	}
}

到此這篇關(guān)于PHP中使用ElasticSearch最新實(shí)例講解的文章就介紹到這了,更多相關(guān)PHP中使用ElasticSearch最內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 初識(shí)Laravel

    初識(shí)Laravel

    最近因?yàn)轫?xiàng)目問(wèn)題,接觸到了Laravel框架,說(shuō)說(shuō)自己的使用感受吧。
    2014-10-10
  • Laravel5.1框架路由分組用法實(shí)例分析

    Laravel5.1框架路由分組用法實(shí)例分析

    這篇文章主要介紹了Laravel5.1框架路由分組用法,結(jié)合實(shí)例形式分析了laravel5.1框架路由分組基本功能與相關(guān)共享屬性使用技巧,需要的朋友可以參考下
    2020-01-01
  • php實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的開(kāi)發(fā)

    php實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的開(kāi)發(fā)

    本文給大家分享的是如何使用php開(kāi)發(fā)簡(jiǎn)單的網(wǎng)頁(yè)爬蟲(chóng)的思路以及代碼,非常的簡(jiǎn)單,有需要的小伙伴可以參考下
    2016-03-03
  • Thinkphp模板中截取字符串函數(shù)簡(jiǎn)介

    Thinkphp模板中截取字符串函數(shù)簡(jiǎn)介

    在php中截取字符串的函數(shù)有很多,而在thinkphp中也可以直接使用php的函數(shù),本文給大家簡(jiǎn)單的介紹thinkPHP模板中截取字符串的具體用法,希望能對(duì)各位有所幫助
    2014-06-06
  • php實(shí)現(xiàn)圖片壓縮處理

    php實(shí)現(xiàn)圖片壓縮處理

    這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)圖片壓縮處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • PHP集成環(huán)境XAMPP的安裝與配置

    PHP集成環(huán)境XAMPP的安裝與配置

    XAMPP(Apache+MySQL+PHP+PERL)是一個(gè)功能強(qiáng)大的建站集成軟件包。接下來(lái)通過(guò)本文給大家分享PHP集成環(huán)境XAMPP的安裝與配置 ,需要的朋友可以參考下
    2018-11-11
  • php獲取''/''傳參的值簡(jiǎn)單方法

    php獲取''/''傳參的值簡(jiǎn)單方法

    下面小編就為大家?guī)?lái)一篇php獲取'/'傳參的值簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • 基于PHP實(shí)現(xiàn)等比壓縮圖片大小

    基于PHP實(shí)現(xiàn)等比壓縮圖片大小

    通過(guò)本段代碼給大家介紹基于php實(shí)現(xiàn)等比壓縮圖片大小的相關(guān)知識(shí),代碼簡(jiǎn)單易懂,對(duì)php壓縮圖片相關(guān)知識(shí)感興趣的朋友參考下吧
    2016-03-03
  • ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng)分析

    ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng)分析

    這篇文章主要介紹了ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng),以注釋的形式詳細(xì)分析了入口文件設(shè)置時(shí)相關(guān)設(shè)置項(xiàng)的含義與設(shè)置技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • symfony2.4的twig中date用法分析

    symfony2.4的twig中date用法分析

    這篇文章主要介紹了symfony2.4的twig中date用法,結(jié)合實(shí)例形式分析了twig中針對(duì)日期與時(shí)間操作的常見(jiàn)方法,需要的朋友可以參考下
    2016-03-03

最新評(píng)論

扎兰屯市| 花莲县| 仁怀市| 广平县| 金沙县| 平泉县| 阳高县| 正阳县| 航空| 金溪县| 葵青区| 道孚县| 临漳县| 台北县| 平乡县| 济宁市| 南溪县| 胶州市| 东兰县| 民县| 子洲县| 同德县| 扬州市| 比如县| 南雄市| 光泽县| 朝阳县| 巴里| 普定县| 晋城| 宣化县| 晋江市| 定边县| 卓资县| 剑河县| 牡丹江市| 青冈县| 桂东县| 涞水县| 宝山区| 富阳市|