一,安裝Java SE環境
在安裝Elasticsearch引擎之前,必須安裝ES需要的軟件環境,安裝Java JDK和配置JAVA_HOME環境變量:
1,從https://www.oracle.com/technetwork/java/javase/downloads/index.html下載和安裝Java SE開發包,我下載的是最新版本如圖:
ElasticSearch對JRE的版本是敏感的,錯誤的版本,會導致ElasticSearch無法運行,下載歸檔的JRE,請從Java Archive下載合適的版本。
2,Java SE開發包安裝完成之后,需要在服務器上創建JAVA_HOME環境變量
環境變量 JAVA_HOME,設置變量值是:C:\Program Files\Java\jdk-11.0.2
注釋:在Windows系統中,“%環境變量名%”用法的含義是獲取指定環境變量的值,創建JAVA_HOME環境變量的作用,是由於安裝ElasticSearch需要引用Java SE開發包。
二,安裝ElasticSearch服務
https://www.elastic.co/downloads/past-releases 下載對應的版本 我下載是5.4.0
* php使用的版本要與ElasticSearch服務對應!
開啟ElasticSearch 服務
進入 elasticsearch目錄的bin,雙擊執行 elasticsearch.bat,該腳本文件執行 ElasticSearch 安裝程序,稍等片刻,打開瀏覽器,輸入 http://localhost:9200 ,顯式以下畫面,說明ES安裝成功。
安裝中文分詞ik
下載 ik https://github.com/medcl/elasticsearch-analysis-ik/releases
我的es版本是 5.4.0 ik 選擇 5.4.0 必須找到對應的版本!!!!!!
下載后解壓到一個文件夾下
並把解壓后的內容放到 D:\elasticsearch-5.4.0\plugins\ik 文件夾下 沒有ik文件夾自己建一個
成功后重啟ElasticSearch 服務 ok!! ElasticSearch服務安裝成功!!!
項目內安裝ElasticSearch
composer require elasticsearch/elasticsearch
會自動加載合適的版本!5.4.0的elasticsearch版本
此時elasticsearch和php支持都安裝成功!
thinkphp5內簡單使用
先上代碼 下面的方法是官方的一些樣例整合
<?php
namespace app\index\controller;
require '../vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Search
{
private $client;
// 構造函數
public function __construct()
{
$params = array(
'127.0.0.1:9200'
);
$this->client = ClientBuilder::create()->setHosts($params)->build();
}
// 創建索引
public function index() { // 只能創建一次
$r = $this->delete_index();
$r = $this->create_index(); //1.創建索引
$r = $this->create_mappings(); //2.創建文檔模板
$r = $this->get_mapping();
$docs = [];
$docs[] = ['id'=>1,'name'=>'小明','profile'=>'我做的ui界面強無敵。','age'=>23];
$docs[] = ['id'=>2,'name'=>'小張','profile'=>'我的php代碼無懈可擊。','age'=>24];
$docs[] = ['id'=>3,'name'=>'小王','profile'=>'C的生活,快樂每一天。','age'=>29];
$docs[] = ['id'=>4,'name'=>'小趙','profile'=>'就沒有我做不出的前端頁面。','age'=>26];
$docs[] = ['id'=>5,'name'=>'小吳','profile'=>'php是最好的語言。','job'=>21];
$docs[] = ['id'=>6,'name'=>'小翁','profile'=>'別煩我,我正在敲bug呢!','age'=>25];
$docs[] = ['id'=>7,'name'=>'小楊','profile'=>'為所欲為,不行就刪庫跑路','age'=>27];
foreach ($docs as $k => $v) {
$r = $this->add_doc($v['id'],$v); //3.添加文檔
}
$r = $this->search_doc("刪庫 別煩我"); //4.搜索結果
}
// 創建索引
public function create_index($index_name = 'test_ik') { // 只能創建一次
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0
]
]
];
try {
return $this->client->indices()->create($params);
} catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
// 刪除索引
public function delete_index($index_name = 'test_ik') {
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
// 創建文檔模板
public function create_mappings($type_name = 'users',$index_name = 'test_ik') {
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => [
$type_name => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer', // 整型
'index' => 'not_analyzed',
],
'name' => [
'type' => 'string', // 字符串型
'index' => 'analyzed', // 全文搜索
'analyzer' => 'ik_max_word'
],
'profile' => [
'type' => 'string',
'index' => 'analyzed',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer',
],
]
]
]
];
$response = $this->client->indices()->putMapping($params);
return $response;
}
// 查看映射
public function get_mapping($type_name = 'users',$index_name = 'test_ik') {
$params = [
'index' => $index_name,
'type' => $type_name
];
$response = $this->client->indices()->getMapping($params);
return $response;
}
// 添加文檔
public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'users') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $doc
];
$response = $this->client->index($params);
return $response;
}
// 判斷文檔存在
public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->exists($params);
return $response;
}
// 獲取文檔
public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->get($params);
return $response;
}
// 更新文檔
public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
// 可以靈活添加新字段,最好不要亂添加
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => [
'doc' => [
'name' => '大王'
]
]
];
$response = $this->client->update($params);
return $response;
}
// 刪除文檔
public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->delete($params);
return $response;
}
// 查詢文檔 (分頁,排序,權重,過濾)
public function search_doc($keywords = "運維",$index_name = "test_ik",$type_name = "users",$from = 0,$size = 2) {
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => [
'query' => [
'bool' => [
'should' => [
[ 'match' => [ 'profile' => [
'query' => $keywords,
'boost' => 3, // 權重大
]]],
[ 'match' => [ 'name' => [
'query' => $keywords,
'boost' => 2,
]]],
],
],
],
'sort' => ['age'=>['order'=>'desc']]
, 'from' => $from, 'size' => $size
]
];
$results = $this->client->search($params);
return $results;
}
}
按1,2,3,4步驟進行 ,第三步就可以把數據庫數據導入。
搜索成功!