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 镜像管理 ...
随机推荐
- 使用axios请求的坑
配置axios在vue-cli中的使用: 在main.js中配置 import axios from "axios" Vue.config.productionTip = fals ...
- tomcat 启动报错org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].xxx
今天在写完一个非常简单的servlet页面跳转的web项目后,启动tomcat报错org.apache.catalina.LifecycleException: Failed to start com ...
- 重读APUE(5)-文件权限
文件,目录,权限 1. 用名称打开任一个类型的文件时,对该名字中包含的每一个目录,包括它可能隐含的当前工作目录都应该具有执行权限:这就是目录执行权限通常被称为搜索位的原因: 例如:为了打开文件/usr ...
- Jenkins与gitlib实现自动化部署与持续构建
Jenkins概念 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测试和 ...
- JS合并多个数组去重算法
var arr1 = ['a','b']; var arr2 = ['a','c','d']; var arr3 = [1,'d',undefined,true,null]; //合并两个数组,去重 ...
- DBCA创建数据库
工具/原料 oracle database 11g 步骤/方法 1 确保安装好oracle database 11g 2 打开命令提示符(运行中输入CMD打开 或是在 附件中点击打开) 3 输入dbc ...
- 推送kafka消息失败
晚上变更 怎么都推不过去,蛋疼,睡饱后加了个hosts没想到好了,然后搜了一下,大概是如下的原因 转自 https://www.cnblogs.com/linlianhuan/p/9258061.ht ...
- MySQL中information_schema数据库是干啥的
大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.information_schema数据库是做什么用的呢,使用WordPress博客 ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_4.RabbitMQ研究-安装RabbitMQ
RabbitMQ由Erlang语言开发,Erlang语言用于并发及分布式系统的开发,在电信领域应用广泛,OTP(Open Telecom Platform)作为Erlang语言的一部分,包含了很多基于 ...
- 使用xdebug调试程序后程序很慢的原因
有一个原因就是开启调试的会话没有正确的关闭,即PhpStorm这边关闭了而没有通知服务端xdebug关闭,导致服务器资源被耗尽,这时只有重启服务端的服务才可以. 所以必须保证每一个调试会话被正确关闭. ...