docker-compose搭建elasticsearch+kibana环境,以及php使用elasticsearch
一、elasticsearch的Dockerfile
增加中文搜索插件analysis-ik
FROM docker.elastic.co/elasticsearch/elasticsearch:7.2.0 ENV VERSION=7.2.0 # https://github.com/medcl/elasticsearch-analysis-ik/releases
ADD https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v${VERSION}/elasticsearch-analysis-ik-$VERSION.zip /tmp/
RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install -b file:///tmp/elasticsearch-analysis-ik-$VERSION.zip RUN rm -rf /tmp/*
二、编写docker-compose.yml
version: '3'
services:
es01:
image: "beng/es:7.2.0"
build: .
container_name: es01
restart: always
volumes:
- ./es01/data:/usr/share/elasticsearch/data:rw
- ./es01/logs:/user/share/elasticsearch/logs:rw
environment:
- node.name=es01
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.seed_hosts=es02:9300,es03:9300
- cluster.initial_master_nodes=es01,es02,es03
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
- "9300:9300"
networks:
- esnet es02:
image: "beng/es:7.2.0"
build: .
container_name: es02
restart: always
volumes:
- ./es02/data:/usr/share/elasticsearch/data:rw
- ./es02/logs:/user/share/elasticsearch/logs:rw
environment:
- node.name=es02
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.seed_hosts=es01:9300,es03:9300
- cluster.initial_master_nodes=es01,es02,es03
ulimits:
memlock:
soft: -1
hard: -1
networks:
- esnet es03:
image: "beng/es:7.2.0"
build: .
container_name: es03
restart: always
volumes:
- ./es03/data:/usr/share/elasticsearch/data:rw
- ./es03/logs:/user/share/elasticsearch/logs:rw
environment:
- node.name=es03
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.seed_hosts=es01:9300,es02:9300
- cluster.initial_master_nodes=es01,es02,es03
ulimits:
memlock:
soft: -1
hard: -1
networks:
- esnet kibana:
image: docker.elastic.co/kibana/kibana:7.2.0
container_name: kibana
ports:
- 5601:5601
volumes:
- ./kibana/kibana.yml:/usr/share/kibana/config/kibana.yml:rw
networks:
- esnet networks:
esnet:
三、编写kibana配置文件
看到网上很多人将elasticsearch.hosts这些参数直接配置在docker-compose文件里设置environment里面,
但是我在docker-compose文件里设置environment怎样都没有生效(原因没找到),于是直接自己写配置文件,并将配置文件磁盘映射到容器中
#
# ** THIS IS AN AUTO-GENERATED FILE **
# # Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://es01:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: zh-CN
四、在php中使用elasticsearch
composer elasticsearch php库地址:https://packagist.org/packages/elasticsearch/elasticsearch
elasticsearch php的官方文档参考:https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html
在根目录下引入库
composer require elasticsearch/elasticsearch
use Elasticsearch\ClientBuilder; //删除索引下的所有文档
private function deleteAllDoc() {
$client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); $params = [
'index' => 'book',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must_not' => ['term' => [ 'id' => -1000 ]],
]
]
]
]; $response = $client->deleteByQuery($params);
echo "delete success\n";
print_r($response);
} //删除某一个文档
private function deleteDoc($id = 0) {
$client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); $params = [
'index' => 'book',
'type' => '_doc',
'id' => $id
]; $response = $client->delete($params);
print_r($response);
} //获取文档
private function getDoc($id = 0) { $client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); $params = [
'index' => 'book_index',
'type' => '_doc',
'id' => $id
]; try {
$response = $client->get($params);
print_r($response['_source']);
} catch (Missing404Exception $e) {
echo 'not exist!';
}
} //添加文档
private function addDoc() {
$client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build();
$params = [
'index' => 'my_index',
'type' => '_doc',
'id' => strval(2),
'body' => [
'first_name' => '关 搜键隔字,以英逗索用号分,书本标文签用label字,不要段混淆',
'age' => 18
]
];
$response = $client->index($params);
print_r($response);
print "\n";
} //删除索引
private function deleteIndex() {
$client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); $params = ['index' => 'book'];
$response = $client->indices()->delete($params);
print_r($response);
} //创建索引
private function createIndex() { $client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); $params = [
'index' => 'book_index',
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 1
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer',
],
'title' => [
'type' => 'text',
//使用ik中文分词
'analyzer' => 'ik_max_word'
],
'image' => [
'type' => 'text'
],
'author' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'words' => [
'type' => 'integer'
],
'description' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'is_vip' => [
'type' => 'integer'
],
'bookstatus' => [
'type' => 'text'
],
'online_status' => [
'type' => 'integer'
],
'type_name' => [
'type' => 'text'
],
'heat' => [
'type' => 'integer'
],
'stars' => [
'type' => 'float'
],
]
]
]
]; $response = $client->indices()->create($params);
print_r($response);
} //搜索书籍
public function searchBook($key, $vip = 0, $page = 1) { $size = 20;
$words_size = 30;
$match_num = 2;
$offset = ($page - 1) * $size; $client = ClientBuilder::create()
->setHosts(config('api.elasticsearch_host'))
->build(); //插入到高亮搜索字前面的内容
$pre_tags = "";
//插入到高亮搜索字后面的内容
$post_tags = "";
$title_weight = 20;
$author_weight = 20;
$description_weight = 1; $params = [
'index' => 'book_index',
'type' => '_doc',
//分页
"from" => $offset,
"size" => $size,
'body' => [
'query' => [
'bool' => [
//其他必须条件,书的vip
'must' => ['term' => [ 'is_vip' => intval($vip) ]],
//其他必须条件,书的状态必须为已上架
'must' => ['term' => [ 'online_status' => 5 ]],
//撞击分数规则
'should' => [
'multi_match' => [
//搜索关键字
'query' => $key,
//书的标题权重20,书的作者标题权重20,书简述权重为1
'fields' => ['title^'.$title_weight, 'author^'.$author_weight, 'description^'.$description_weight],
"type" => "cross_fields",
"operator" => "OR",
//至少匹配度30%
"minimum_should_match" => "30%"
]
]
]
],
//关键字高亮
'highlight' => [
//匹配包含关键字的高亮次数,比如有30段文本包含关键字,如果只需要前面两次的,那$match_num=2
'number_of_fragments' => $match_num,
//匹配包含关键字的高亮段落保留多少文字,比如一片文章中有30个片段包含关键字,
//每个片段保留30个文字,则$words_size=30,多余的会忽略
'fragment_size' => $words_size,
'fields' => [
//高亮的字段,这里高亮书的标题和作者以及书简述
'title' => ['pre_tags' => $pre_tags, 'post_tags' => $post_tags,],
'author' => ['pre_tags' => $pre_tags, 'post_tags' => $post_tags,],
'description' => ['pre_tags' => $pre_tags, 'post_tags' => $post_tags,],
]
]
]
]; $results = $client->search($params);
$hits = $results['hits']['hits']; if (empty($results['hits']['hits'])) {
return [];
} $books = [];
//提取书信息
foreach ($hits as $key => $value) {
$book = [];
$book['book_id'] = $value['_source']['id']; if (!empty($value['highlight']['title'])) {
$book['title'] = $value['highlight']['title'][0];
} else {
$book['title'] = $value['_source']['title'];
} if (!empty($value['highlight']['author'])) {
$book['author'] = $value['highlight']['author'][0];
} else {
$book['author'] = $value['_source']['author'];
} if (!empty($value['highlight']['description'])) {
$book['description'] = $value['highlight']['description'][0]; if (!empty($value['highlight']['description'][1])) {
$book['description'] = $book['description']. '...' .$value['highlight']['description'][1];
}
} else {
$book['description'] = $value['_source']['description'];
}
$book['description'] = str_replace(array( "\r\n", "\n\n", "\r", " "), "", $book['description']); $book['bookstatus'] = $value['_source']['bookstatus'];
$book['image'] = $value['_source']['image'];
$book['words'] = $value['_source']['words'];
$book['stars'] = $value['_source']['stars'];
$book['heat'] = $value['_source']['heat'];
$book['type_name'] = $value['_source']['type_name'];
array_push($books, $book);
} return $books;
}
docker-compose搭建elasticsearch+kibana环境,以及php使用elasticsearch的更多相关文章
- 利用 Docker Compose 搭建 SpringBoot 运行环境(超详细步骤和分析)
0.前言 相信点进来看这篇文章的同学们已经对 Docker Dompose 有一定的了解了,下面,我们拿最简单的例子来介绍如何使用 Docker Compose 来管理项目. 本文例子: 一个应用服务 ...
- 一文教您如何通过 Docker 快速搭建各种测试环境(Mysql, Redis, Elasticsearch, MongoDB) | 建议收藏
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Docker Compose 搭建 Redis Cluster 集群环境
在前文<Docker 搭建 Redis Cluster 集群环境>中我已经教过大家如何搭建了,本文使用 Docker Compose 再带大家搭建一遍,其目的主要是为了让大家感受 Dock ...
- Istio入门实战与架构原理——使用Docker Compose搭建Service Mesh
本文将介绍如何使用Docker Compose搭建Istio.Istio号称支持多种平台(不仅仅Kubernetes).然而,官网上非基于Kubernetes的教程仿佛不是亲儿子,写得非常随便,不仅缺 ...
- 使用Docker Compose搭建Service Mesh
使用Docker Compose搭建Service Mesh 本文将介绍如何使用Docker Compose搭建Istio.Istio号称支持多种平台(不仅仅Kubernetes).然而,官网上非基于 ...
- Docker-教你如何通过 Docker 快速搭建各种测试环境
今天给大家分享的主题是,如何通过 Docker 快速搭建各种测试环境,本文列举的,也是作者在工作中经常用到的,其中包括 MySQL.Redis.Elasticsearch.MongoDB 安装步骤,通 ...
- PyCharm使用之利用Docker镜像搭建Python开发环境
在我们平时使用PyCharm的过程中,一般都是连接本地的Python环境进行开发,但是如果是离线的环境呢?这样就不好搭建Python开发环境,因为第三方模块的依赖复杂,不好通过离线安装包的方式安装 ...
- 使用Docker快速搭建PHP开发环境
最近有个同事找过来,希望我对在很早之前写的一个PHP网站上增加一些功能,当时开发使用xampp构建的本地开发环境,但是现在我的笔记本电脑已经更新,没有当时的开发环境.本着尽量不往电脑上装无用软件的原则 ...
- Docker容器搭建android编译环境
Docker容器搭建android编译环境 目录 1 部署容器 1.1 手动部署 1.1.1 配置docker 1.1.2 启动容器 1.1.3 配置环境 1.2 Dockerfile 2 镜像管理 ...
随机推荐
- TCP首部的TimeStamp时间戳选项 转载
TCP应该是以太网协议族中被应用最为广泛的协议之中的一个,这里就聊一聊TCP协议中的TimeStamp选项.这个选项是由RFC 1323引入的,该C建议提交于1992年.到今天已经足足有20个年头.只 ...
- ReactJS和AngularJS对比
Angular的特点: 优势: AngularJS是一套完整的框架,angular有自带的数据绑定.render渲染.angularUI库,过滤器,$filter,$directive(模板),$se ...
- AddLayer和AddTag
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; pu ...
- RGB-D(深度图像) & 图像深度
RGB-D(深度图像) 深度图像 = 普通的RGB三通道彩色图像 + Depth Map 在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...
- 【Java/MySql】使用JDBC访问MySql数据库的Maven工程
下载链接:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-1.rar pom.xml里写: <p ...
- mod 运算与乘法逆元
mod 运算与乘法逆元 %运算 边乘边mod 乘法 除法 mod 希望计算5/2%7=6 乘法 除法 mod 希望计算5/2%7=6 两边同时/x 在取mod(p)运算下,a/b=a*bp-2 bp- ...
- 多位IT专家分享他们离不开的实用工具
本文的 PDF版本可供下载. #1: John Bartow,顾问 John Bartow的工作领域是网络和PC安全,他提供了自己从事的咨询公司, WinHaven Computer Consulti ...
- 按下F2编辑dxDBTreeView的节点
在TdxDBTreeView控件的OnKeyDown事件中写入if Key = VK_F2 thenbegin if DBTreeMain.DBSelected = nil then Exit ...
- CPU 上下文切换及案例分析
什么是CPU 上下文 我们都知道,Linux是一个多任务操作系统,它远支持大于CPU数量的任务同时运行,当然,这些任务实际上并不是真的在同时运行,而是因为系统在很短时间内,将CPU轮流分配给他们,造成 ...
- UICollectionview的头视图和尾视图
UITableView有头视图和尾视图,那么UICollectionView有没有头视图和尾视图呢? 答案是有的. 1.新建一个类,必须继承自 UICollectionReusableView. 2. ...