一、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. 注解之 @RestController 和 @RequestMapping

    Controller 是 Spring 中最基本的组件,主要处理用户交互,一般每个业务逻辑都会有一个 Controller,供用户请求接口进行数据访问:@RequestMapping 注解用于绑定UR ...

  2. 在AspNetCore3.0中使用Autofac

    1. 引入Nuget包 Autofac Autofac.Extensions.DependencyInjection 2. 修改Program.cs 将默认ServiceProviderFactory ...

  3. SQL连接查询基础知识点

    什么是连接 连接(join)查询是基于多个表中的关联字段将数据行拼接到一起,可以同时返回多个表中的数据. 下面以两个表为例子,举例说明一下不同的连接. SELECT * FROM products i ...

  4. plus.zip.compressImage 压缩报错{"code":-5,"message":"输出图片失败"}

    var pathCompress = "_doc/" + getNowFormatDate(new Date()) + ' ' + Math.random() + ".c ...

  5. XMLHttpRequest Level2 新功能

    XMLHttpRequest是浏览器的接口,使得javascript可以进行HTTP(S)通信: 2008年2月,就提出了XMLHttpRequest Level 2 草案. 这个XMLHttpReq ...

  6. vue中书写JSX一些坑-特殊属性名

    举例说明, T1和T2引用Sub时, key2会出现在props以及data.attrs中, 而key则在data中 const Sub = ({data, props}) => { conso ...

  7. win10系统搭建vagrant时开启bios,虚拟化问题

    VT-x is disabled in the BIOS的意思是VT-X虚拟化技术处于禁止关闭状态,需要在电脑主板BIOS中开启CPU虚拟化技术thinkpad重启F1进入BIOS,选择: Sercu ...

  8. 阿里RDS数据库 全量备份恢复到本地MYSQL

    阿里RDS数据库 全量备份恢复到本地MYSQL   1.首先下载RDS的全量备份 下载完成后上传到服务器备用   2.安装MySQL 5.6 首先删除机上其他的MySQL版本 检查系统是否安装其他版本 ...

  9. 浏览器与NodeJS环境 eventloop异同详解(转)

    结论:浏览器中是一个宏任务,所有微任务,一个宏任务,所有微任务...           NodeJS中,一种宏任务队列所有任务,所有微任务,一种宏任务队列所有任务,所有微任务... ┌─────── ...

  10. JAVA 基础编程练习题31 【程序 31 数组逆序】

    31 [程序 31 数组逆序] 题目:将一个数组逆序输出. 程序分析:用第一个与最后一个交换. package cskaoyan; public class cskaoyan31 { @org.jun ...