环境和脚本
> go get -u github.com/go-redis/redis/v8
> go get -u github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi
原创大约 2 分钟
环境和脚本
> go get -u github.com/go-redis/redis/v8
> go get -u github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi
可以参考官方提供的Docker部署指南。
安装好Docker,然后执行下面的脚本。
# 关闭并禁用防火墙
> systemctl stop firewalld.service
> systemctl disable firewalld.service
> systemctl status firewalld.service
# 新建文件夹
> cd /home/work
> mkdir elasticsearch
> mkdir -p ./elasticsearch/data
> mkdir -p ./elasticsearch/plugins
> chmod 777 -R ./elasticsearch
# 安装Elasticsearch
> docker run -d \
--restart=always \
--name elasticsearch -p 9200:9200 -p 9300:9300 \
--privileged=true \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-v /home/work/volumes/elasticsearch/data:/usr/share/elasticsearch/data \
-v /home/work/volumes/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
docker.elastic.co/elasticsearch/elasticsearch:7.17.22
# 安装Kibana
> docker run -d \
--name kibana \
--privileged=true \
-e ELASTICSEARCH_HOSTS="http://172.16.185.168:9200" -p 5601:5601 \
kibana:7.17.22
# 下载IK分词器
> cd /home/work
> wget https://github.com/infinilabs/analysis-ik/releases/download/v7.17.6/elasticsearch-analysis-ik-7.17.6.zip
# 如果是集群,那么每一个机器或者每一个docker都要安装ik
> docker ps -a
> docker exec <elasticsearch容器ID> -it bash
> mkdir /opt/elasticsearch-7.17.22/plugins/ik
> exit
> docker cp /home/work/elasticsearch-analysis-ik-7.17.6.zip <elasticsearch容器ID>:/opt/elasticsearch-7.17.22/plugins/ik/elasticsearch-analysis-ik-7.17.6.zip
> docker restart <elasticsearch容器ID>
> curl -XPUT http://172.16.185.176:9200/index
> curl -XPOST http://172.16.185.176:9200/index/fulltext/_mapping?pretty -d'
{
"fulltext": {
"_all": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"term_vector": "no",
"store": "false"
},
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
> curl -XPOST http://172.16.185.176:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}'
> curl -XPOST http://172.16.185.176:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}'
> curl -XPOST http://172.16.185.176:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'
> curl -XPOST http://172.16.185.176:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'
# 高亮查询
> curl -XPOST http://172.16.185.176:9200/index/fulltext/_search?pretty -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}'
# 删除单个索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/blink?pretty'
# 删除多个索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/blink,blog?pretty'
# 创建索引
> curl --user elastic:company2020 -XPUT -H 'Content-Type: application/json' 'localhost:9200/blink?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
},
"mappings": {
"properties": {
"guid": {
"type": "long"
},
"bizline": {
"type": "keyword"
},
"resid": {
"type": "keyword"
},
"block": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"url": {
"type": "keyword"
},
"kind": {
"type": "integer"
},
"status": {
"type": "integer"
},
"suggestion": {
"type": "keyword"
},
"again": {
"type": "integer"
},
"senstitle": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"senstext": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"sensimage": {
"type": "keyword"
},
"createtime": {
"type": "long"
},
"updatetime": {
"type": "long"
}
}
}
}'
# 创建产品索引(不带分词)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
# 创建产品索引(带分词)
> curl --user elastic:company2020 -XPUT 'localhost:9200/twitter?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
},
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern",
"pattern": "\\,"
}
}
}
}
}'
# 创建产品mapping(数据表)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/_mapping/product_plan?pretty' -d'
{
"properties": {
"id" : { "type": "keyword" },
"name" : { "type": "text","analyzer": "ik_max_word" },
"desc" : { "type": "text","analyzer": "ik_max_word", "store": false },
"price" : { "type": "float" },
"productid" : { "type": "keyword" },
"productname" : { "type": "text","analyzer": "ik_max_word" },
"categorys" : {
"type" : "nested",
"properties" : {
"id" : { "type": "keyword" },
"name" : { "type": "text" , "analyzer": "ik_max_word" },
"parentid" : { "type": "keyword" }
}
}
}
}'
# 给索引赋值一个文档(记录值)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"id" : "1",
"name" : "儿童保险计划",
"desc" : "保险产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳保险",
"categorys" : [
{
"id" : "1",
"name" : "儿童保险",
"parentid" : "3"
}
]
}'
# 查看单个索引的创建结果
> curl --user elastic:company2020 -XGET 'http://localhost:9200/blink/_mapping?pretty'
# 查看多个索引的创建结果
> curl --user elastic:company2020 -XGET 'http://localhost:9200/blink,blog/_mapping?pretty'
# 查看全部索引
> curl --user elastic:company2020 -XGET 'http://localhost:9200/_cat/indices?v&pretty'
# 获取索引
> curl --user elastic:company2020 -XGET 'localhost:9200/product/_mapping/product_plan?pretty'
# 删除索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/product?pretty'