一、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的更多相关文章

  1. 利用 Docker Compose 搭建 SpringBoot 运行环境(超详细步骤和分析)

    0.前言 相信点进来看这篇文章的同学们已经对 Docker Dompose 有一定的了解了,下面,我们拿最简单的例子来介绍如何使用 Docker Compose 来管理项目. 本文例子: 一个应用服务 ...

  2. 一文教您如何通过 Docker 快速搭建各种测试环境(Mysql, Redis, Elasticsearch, MongoDB) | 建议收藏

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  3. Docker Compose 搭建 Redis Cluster 集群环境

    在前文<Docker 搭建 Redis Cluster 集群环境>中我已经教过大家如何搭建了,本文使用 Docker Compose 再带大家搭建一遍,其目的主要是为了让大家感受 Dock ...

  4. Istio入门实战与架构原理——使用Docker Compose搭建Service Mesh

    本文将介绍如何使用Docker Compose搭建Istio.Istio号称支持多种平台(不仅仅Kubernetes).然而,官网上非基于Kubernetes的教程仿佛不是亲儿子,写得非常随便,不仅缺 ...

  5. 使用Docker Compose搭建Service Mesh

    使用Docker Compose搭建Service Mesh 本文将介绍如何使用Docker Compose搭建Istio.Istio号称支持多种平台(不仅仅Kubernetes).然而,官网上非基于 ...

  6. Docker-教你如何通过 Docker 快速搭建各种测试环境

    今天给大家分享的主题是,如何通过 Docker 快速搭建各种测试环境,本文列举的,也是作者在工作中经常用到的,其中包括 MySQL.Redis.Elasticsearch.MongoDB 安装步骤,通 ...

  7. PyCharm使用之利用Docker镜像搭建Python开发环境

      在我们平时使用PyCharm的过程中,一般都是连接本地的Python环境进行开发,但是如果是离线的环境呢?这样就不好搭建Python开发环境,因为第三方模块的依赖复杂,不好通过离线安装包的方式安装 ...

  8. 使用Docker快速搭建PHP开发环境

    最近有个同事找过来,希望我对在很早之前写的一个PHP网站上增加一些功能,当时开发使用xampp构建的本地开发环境,但是现在我的笔记本电脑已经更新,没有当时的开发环境.本着尽量不往电脑上装无用软件的原则 ...

  9. Docker容器搭建android编译环境

    Docker容器搭建android编译环境 目录 1 部署容器 1.1 手动部署 1.1.1 配置docker 1.1.2 启动容器 1.1.3 配置环境 1.2 Dockerfile 2 镜像管理 ...

随机推荐

  1. 2019.6.28 校内测试 T4 【音乐会】达拉崩吧·上

    考试的一道附加题~ 一看题目描述:把区间[l,r]里每个数异或上x,求区间[l,r]里所有数的异或和,这明显的要用数据结构或RMQ吧. 恩,所以正解就是线段树啦,至于树状数组行与否,不知道~ wate ...

  2. 银联刷卡POS机冲正

    冲正是为系统认为可能交易失败时采取的补救手法. 即一笔交易在终端已经置为成功标志,但是发送到主机的账务交易包没有得到响应,即终端交易超时,所以不确定该笔交易是否在主机端也成功完成,为了确保用户的利益, ...

  3. slax中改变终端字体

    修改~/.Xresources文件 ! English font      Xterm*faceName: DejaVu Sans Mono=True:size=16 修改颜色: ! colos XT ...

  4. idea 2018注册码(激活码)

    最近做一个项目,用idea 社区版的   但是缺少了好多功能 无奈只能用专业版的,但是需要注册激活  下面是我的注册方法 1.打开了idea  会提示让激活  选择Licensse server 2. ...

  5. 蜗牛圈圈-时尚智能的运动计时App

    Duang! 各类运动爱好者的福音来啦! 蜗牛圈圈-最智能的圈速计时助手 扫描二维码下载体验 [产品简介] -蜗牛圈圈是一款专业的圈速计时工具,帮助您获得整个运动过程中的各项数据,保存记录,分享激情. ...

  6. ajax与HTML5 history API实现无刷新跳转

    一.ajax载入与浏览器历史的前进与后退 ajax可以实现页面的无刷新操作,但是无法前进与后退,淡出使用Ajax不利于SEO.如今,HTML5让事情变得简单.当执行ajax操作时,往浏览器histor ...

  7. SQL-W3School-高级:SQL BETWEEN 操作符

    ylbtech-SQL-W3School-高级:SQL BETWEEN 操作符 1.返回顶部 1. BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. BETWEE ...

  8. in mind (不是 切记 的意思)

    Both Grunt and Gulp.js perform these automation tasks particularly well, although Gulp.js has the ed ...

  9. Qt编写自定义控件33-图片切换动画

    一.前言 在很多看图软件中,切换图片的时候可以带上动画过渡或者切换效果,显得更人性化,其实主要还是炫一些,比如百叶窗.透明度变化.左下角飞入等,无论多少种效果,核心都是围绕QPainter来进行,将各 ...

  10. CCIE总结:路由器、交换机

    bbs.spoto.net/forum--.html -----雏鹰部落 GNS3安装 .安装的所有目录不能使用中文 ISO如何操作 securecrt如何使用建立会话:之前总是连不上的原因是没有选 ...