1、安装ElasticSearch、kibana
官方安装地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
https://www.elastic.co/guide/en/kibana/current/docker.html
1.1 拉取镜像
>docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.1
>docker pull docker.elastic.co/kibana/kibana:7.10.1
1.2 开发模式,快速启动
1.2.1 安装elasticsearch
>sudo mkdir -p /mydata/elasticsearch/config
>sudo mkdir -p /mydata/elasticsearch/data
>sudo echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml #可以远程访问
>chmod -R 777 /mydata/elasticsearch
>sudo docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-d \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
docker.elastic.co/elasticsearch/elasticsearch:7.10.1
1.2.2 安装IK
ik地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
下载
>wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.1/elasticsearch-analysis-ik-7.10.1.zip
> unzip elasticsearch-analysis-ik-7.10.1.zip -d ik/
>mv ik/ /mydata/elasticsearch/plugins/
>docker restart elasticsearch #重启elasticsearch
1.2.3 安装kibana
docker run --link YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_ID:elasticsearch -p 5601:5601 docker.elastic.co/kibana/kibana:7.10.1
YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_ID:elasticsearch的docker的name或id。
>docker run -d --name kibana --link elasticsearch:elasticsearch -p 5601:5601 docker.elastic.co/kibana/kibana:7.10.1
找到Kibana配置文件所在:config/kibana.ym
重启即可。
>docker exec -it kibana /bin/bash #进入容器
找到配置:i18n.locale: "en"
并将en
修改为zh-CN
(简体中文)。
1.3 访问
验证elasticsearch
配置9200是通过HTTP连接的端口,9300是TCP连接的端口
验证kibana
访问地址:http://8.136.103.242:5601/
1.4 命令
>docker start elasticsearch #启动
>docker stop elasticsearch #停止
>docker restart elasticsearch #重启
>docker exec -it elasticsearch /bin/bash #进入容器
1.5 验证中文分词
POST /docs/news
{"content":"美国留给伊拉克的是个烂摊子吗"}
POST /index/_create/1
{"content":"美国留给伊拉克的是个烂摊子吗"}
POST /index/_create/2
{"content":"公安部:各地校车将享最高路权"}
POST /index/_create/3
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
POST /index/_create/4
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
POST /index/_search
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
返回结果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.2257079,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.2257079,
"_source" : {
"content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight" : {
"content" : [
"<tag1>中</tag1>韩渔警冲突调查:韩警平均每天扣1艘<tag1>中</tag1><tag1>国</tag1>渔船"
]
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.9640833,
"_source" : {
"content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
},
"highlight" : {
"content" : [
"<tag1>中</tag1><tag1>国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
]
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.3864615,
"_source" : {
"content" : "美国留给伊拉克的是个烂摊子吗"
},
"highlight" : {
"content" : [
"美<tag1>国</tag1>留给伊拉克的是个烂摊子吗"
]
}
}
]
}
}
1.6 插入测试数据
测试数据地址:https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json
批量添加添加
2、geo
2.1 创建索引字段
PUT /show
{
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "standard"
},
"type": {
"type": "integer"
},
"position": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
2.2 创建索引文档
PUT /show/_doc/2
{
"id" : 1,
"name" : "建升大厦",
"type" : 1,
"position":{
"location" : {
"lat" : 22.6482057076,
"lon" : 114.1250142233
}
}
}
PUT /show/_doc/1
{
"id" : 2,
"name" : "深圳市第三人民医院",
"type" : 1,
"position":{
"location" : {
"lat" : 22.6352587415,
"lon" : 114.1289020619
}
}
}
PUT /show/_doc/3
{
"id" : 3,
"name" : "深圳百合医院",
"type" : 1,
"position":{
"location" : {
"lat" : 22.6164455768,
"lon" : 114.1395956293
}
}
}
2.3 查询
2.3.1 查询所有
POST /show/_search
{
"query": {
"bool": {
"must": {
"match_all": {
}
},
"filter": {
"geo_distance": {
"distance": "10km",
"position.location": {
"lat": 22.6497899384,
"lon": 114.1258725301
}
}
}
}
},
"sort": [{
"_geo_distance": {
"position.location": {
"lat": 22.6497899384,
"lon": 114.1258725301
},
"order": "asc",
"unit": "km",
"mode": "min"
}
}]
}
查询结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "show",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"id" : 1,
"name" : "建升大厦",
"type" : 1,
"position" : {
"location" : {
"lat" : 22.6482057076,
"lon" : 114.1250142233
}
}
},
"sort" : [
0.19695848610299513
]
},
{
"_index" : "show",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"id" : 2,
"name" : "深圳市第三人民医院",
"type" : 1,
"position" : {
"location" : {
"lat" : 22.6352587415,
"lon" : 114.1289020619
}
}
},
"sort" : [
1.6454376027134134
]
},
{
"_index" : "show",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"id" : 3,
"name" : "深圳百合医院",
"type" : 1,
"position" : {
"location" : {
"lat" : 22.6164455768,
"lon" : 114.1395956293
}
}
},
"sort" : [
3.9662223889048036
]
}
]
}
}
2.3.2 分词查询
POST /show/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "深圳"
}
},
"filter": {
"geo_distance": {
"distance": "100km",
"position.location": {
"lat": 22.649928,
"lon": 114.125646
}
}
}
}
},
"sort": [{
"_geo_distance": {
"position.location": {
"lat": 22.6497899384,
"lon": 114.1258725301
},
"order": "asc",
"unit": "km",
"mode": "min"
}
}]
}
查询结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "show",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"id" : 2,
"name" : "深圳市第三人民医院",
"type" : 1,
"position" : {
"location" : {
"lat" : 22.6352587415,
"lon" : 114.1289020619
}
}
},
"sort" : [
1.6454376027134134
]
},
{
"_index" : "show",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"id" : 3,
"name" : "深圳百合医院",
"type" : 1,
"position" : {
"location" : {
"lat" : 22.6164455768,
"lon" : 114.1395956293
}
}
},
"sort" : [
3.9662223889048036
]
}
]
}
}