专注于快乐的事情

Elasticsearch入门学习

docker安装

假设宿主机ip为10.168.1.111
下载
docker pull elasticsearch:5.6.4
docker pull mobz/elasticsearch-head:5

安装es

master配置

mkdir -p ~/mydocker/esdata1/nodes

vi ~/mydocker/es1.yml

cluster.name: "dali"
node.name: node1
node.master: true
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: false
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1

docker run -u 1000 -d –name es1 -p 9200:9200 -p 9300:9300 -v ~/mydocker/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ~/mydocker/esdata1:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:5.6.4

docker run -u 1000 -d –name es1 -p 9200:9200 -p 9300:9300 -v ~/mydocker/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ~/mydocker/esdata1:/usr/share/elasticsearch/data elasticsearch:5.6.4

如果启动出现docker max virtual memory areas vm.max_map_count
sudo sysctl -w vm.max_map_count=655360

node配置

mkdir -p ~/mydocker/esdata2/nodes

vi ~/mydocker/es2.yml

cluster.name: "dali"
node.name: node2
node.master: false
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: false
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: es1

docker run -d –name es2 –link es1:es1 -p 9201:9200 -p 9301:9300 -v ~/mydocker/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ~/mydocker/esdata2:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:5.6.4

安装head

elasticsearch head是集群管理工具、数据可视化、增删改查工具, Elasticsearch 语句可视化

安装过程如下:

docker run -d –name head -p 9100:9100 docker.io/mobz/elasticsearch-head:5

通过访问
http://10.168.1.111:9100,打开head界面,
修改集群地址http://10.168.1.111:9200/

安装中文分词

进入容器

docker exec -it es1 /bin/bash

执行如下:

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.4/elasticsearch-analysis-ik-5.6.4.zip

重新启动docker restart es1

基本概念

Node 与 Cluster

节点(Node),一个运行的Elasticearch实例,一般是一台机器上的一个进程。
一组节点构成一个集群(cluster)

索引(index)/文档(document)

索引看成关系型数据库的表。
Index里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。一个文档相当于数据库表中的一行记录。

文档的json表示如下:

{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}

分片 副本

为了支持更大量的数据,索引一般会按某个维度分成多个部分,每个部分就是一个分片,分片被节点(Node)管理。

同一个分片(Shard)的备份数据,一个分片可能会有0个或多个副本,这些副本中的数据保证强一致或最终一致。

基本操作

索引操作

新建和删除Index

curl -X PUT 'localhost:9200/weather'
curl -X DELETE 'localhost:9200/weather'

索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号

查询所有的索引

curl -X GET 'http://localhost:9200/_cat/indices?v'

创建文档

格式http://localhost:9200/<index>/<type>/[<id>]
分别为:索引名称/类型名称/ID

curl -XPUT 'http://10.168.1.111:9200/blog/article/1' -d'{"title": "New version of Elasticsearch released!","content":"Version 1.0 released today!"}'

索引和类型是必需的,而id部分是可选的
如果不指定ID,ElasticSearch会为我们生成一个ID。应该使用HTTP的POST而不是PUT请求
如果使用put,出现No handler found for uri错误

以下为不指定id的使用示例

curl -XPOST 'http://10.168.1.111:9200/blog/article/' -d'{"title": "New version of Elasticsearch released!","content":"Version 2.0 released today!"}'

更新文档

curl -XPOST http://localhost:9200/blog/article/1/_update -d '{"script": "ctx._source.content = \"new content\""}'

检索文档

curl -XGET http://localhost:9200/blog/article/1

##中文索引设置

凡是需要搜索的中文字段,都要单独设置一下

curl -X PUT ‘localhost:9200/accounts’ -d ‘
{
“mappings”: {
“person”: {
“properties”: {
“user”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“title”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“desc”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
}
}
}
}
}’

数据查询

返回所有记录

curl 'localhost:9200/accounts/person/_search'

全文搜索

curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "软件" }}
}'

如果搜索单个

curl 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "_all",
"query": "张"
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}'

or

curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "软件 系统" }}
}'

执行多个关键词的and搜索,必须使用布尔查询

curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "软件" } },
        { "match": { "desc": "系统" } }
      ]
    }
  }
}'

安装kibana

docker pull docker.elastic.co/kibana/kibana:5.6.2

docker run -p 5601:5601 -e "ELASTICSEARCH_URL=http://10.168.1.111:9200" --name my-kibana \
--network host -d docker.elastic.co/kibana/kibana:5.6.2

参考

社区
https://elasticsearch.cn

Elasticsearch: 权威指南

评论系统未开启,无法评论!