一,安装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步骤进行 ,第三步就可以把数据库数据导入。
搜索成功!