目录

安装

ubuntu环境

elasticsearch安装

#下载elasticsearch 6.4.0的docker镜像
sudo docker pull elasticsearch:6.4.0

#修改虚拟内存区域大小,否则会因为过小而无法启动;
sudo sysctl -w vm.max_map_count=262144

#使用docker命令启动
sudo docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \
-e "discovery.type=single-node" \
-e "cluster.name=elasticsearch" \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-d elasticsearch:6.4.0

#启动时会发现/usr/share/elasticsearch/data目录没有访问权限,只需要修改该目录的权限,再重新启动即可
sudo chmod 777 /mydata/elasticsearch/data/

IKAnalyzer分词器插件安装

#进入elasticsearch docker容器
sudo docker exec -it elasticsearch /bin/bash

#注意以下在容器中运行插件安装:
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip

如果一切顺利的话,将会有类似的输出:

[root@32542d28592e elasticsearch]# elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik

如果在容器中运行安装ik插件失败,解析不了host下载不了zip等问题,可以stop所有容器,重新启动docker服务:

sudo service docker restart
# 重启docker服务后,再重新start elasticsearch容器,进入容器执行前面安装插件的命令

最后重启容器

#重启容器
sudo docker restart elasticsearch
# 确认是否重启容器成功
sudo docker ps -a 
jm@ubuntu:~$ sudo docker ps -a
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS                       PORTS                                            NAMES
32542d28592e        elasticsearch:6.4.0                     "/usr/local/bin/dock…"   4 hours ago         Up 10 seconds                0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   elasticsearch

确认成功

访问http://192.168.8.130:9200/,返回相关版本信息,进行确认安装成功:

{
    "name" : "7z4vRo-",
    "cluster_name" : "elasticsearch",
    "cluster_uuid" : "shTOzHEiTwqEMK_dijjHjQ",
    "version" : {
        "number" : "6.4.0",
        "build_flavor" : "default",
        "build_type" : "tar",
        "build_hash" : "595516e",
        "build_date" : "2018-08-17T23:18:47.308994Z",
        "build_snapshot" : false,
        "lucene_version" : "7.4.0",
        "minimum_wire_compatibility_version" : "5.6.0",
        "minimum_index_compatibility_version" : "5.0.0"
    },
    "tagline" : "You Know, for Search"
}

Kibana客户端

Kibina作为访问elasticsearch服务的客户端。

# 拉取kibana镜像
sudo docker pull kibana:6.4.0

# 启动容器
sudo docker run --name kibana -p 5601:5601 \
--link elasticsearch:es \
-e "elasticsearch.hosts=http://es:9200" \
-d kibana:6.4.0

–link:在容器创建一个快捷访问方式es,可以直接访问容器elasticsearch容器。

-e:配置一个环境变量。

访问测试:http://192.168.8.130:5601/,将打开kibana客户端。

操作

通过kibana客户端,在Dev Tools模块中,我们可以通过指令完成查询等操作:

集群信息查看

# 查看集群健康状态
GET /_cat/health?v

# 查看节点状态
GET /_cat/nodes?v 

索引操作

# 创建索引
PUT /order

# 查看所有索引
GET /_cat/indices?v

# 删除索引
DELETE /order

类型操作

# 查看文档类型
GET /bank/account/_mapping

文档记录操作

# 添加文档(记录)
PUT /order/doc/1
{
    "name": "apple",
    "price":100
}

# 查看文档
GET /order/doc/1

# 修改文档
POST /order/doc/1/_update
{
    "doc": { "price": 90 }
}

# 删除文档
DELETE /order/doc/1

# 批量操作(同时修改或新增以下两条文档)
POST /order/doc/_bulk
{"index":{"_id":"1"}}
{"order_num":1,"price":39225,"name":"Amber","alias":"Duke","counts":32}
{"index":{"_id":"6"}}
{"order_num":6,"price":5686,"name":"Hattie","alias":"Bond","counts":36}

数据搜索

# 搜索全部(即没有匹配任何条件),分页每页10条 from表示偏移量,从0开始,size表示每页显示的数量
GET /order/_search
{
    "query": { "match_all": {} },
    "from": 0,
    "size": 10  
}

# 搜索排序,使用sort表示,例如按price字段升序asc排列
GET /order/_search
{
    "query": { "match_all": {} },
    "sort": { "price": { "order": "asc" } }
}

# 搜索并指定返回字段(order_num、price)    
GET /order/_search
{
    "query": { "match_all": {} },
    "_source": ["order_num", "price"]
}

# 条件搜索
# 匹配条件order_num=20的文档记录(数值精确匹配)
GET /order/_search
{
    "query": {
        "match": {
        "order_num": 20
        }
    }
}

# 搜索from含有street的文档记录(字符串模糊匹配,且不区分大小写)
GET /order/_search
{
    "query": {
        "match": {
            "from": "street"
        }
    },
    "_source": [
        "order_num",
        "from"
    ]
}

# 短语搜索(使用match依然有效?)
GET /order/_search
{
    "query": {
        "match_phrase": {
            "from": "259 Kane"
        }
    },
    "_source": [
        "order_num",
        "from"
    ]
}


# 组合搜索
# 组合搜索,使用bool来进行组合,must表示同时满足,例如搜索from字段中包含street,同时name字段包含lilly的文档
GET /order/_search
{
    "query": {
        "bool": {
            "must": [
                { "match": { "from": "street" } },
                { "match": { "name": "lilly" } }
            ]
        }
    }
}

# 组合搜索,should表示满足其中任意一个,搜索 from 字段中包含street或者name字段包含betty的文档(大小写忽略)
GET /order/_search
{
    "query": {
        "bool": {
            "should": [
                { "match": { "from": "street" } },
                { "match": { "name": "betty" } }
            ]
        }
    }
}

# 组合搜索,must_not表示同时不满足。mast与mast_not可以一起用,表示满足xx且不满足xxx的文档记录


过滤搜索

# 搜索过滤,使用filter来表示,例如过滤出price字段在35000~36000的文档
GET /order/_search
{
    "query": {
        "bool": {
            "must": { "match_all": {} },
            "filter": {
                "range": {
                    "price": {
                        "gte": 35000,
                        "lte": 36000
                    }
                }
            }
        }
    }
}

# 聚合
# 对搜索结果进行聚合,使用aggs来表示,类似于MySql中的group by,例如对type字段进行聚合,统计出相同type的文档数量。(对于数值类型字段无效?)
GET /order/_search
{
    "size": 0,
    "aggs": {
        "group_by_type": {
            "terms": {
                "field": "type.keyword"
            }
        }
    }
}

# 嵌套聚合, 对type进行聚合后,再统计出对应type的平均price,最后按照avg_price平均值进行排序:
GET /order/_search
{
    "size": 0,
    "aggs": {
        "group_by_type": {
            "terms": {
                "field": "type.keyword",
                "order":{
                    "avg_price":"desc"
                }
            },
            "aggs": {
                "avg_price": {
                    "avg": {
                        "field": "price"
                    }
                }
            }
        }
    }
}