测试环境:debian 9
官网提供了 deb,rpm,源码下载java
官方下载地址:https://www.elastic.co/downloads/elasticsearchgit
经过源码安装会遇到一些小问题,为了方便,我直接下载deb安装(我不肯定经过deb安装是否须要java环境,由于我提早安装了openjdk-8-jdk)。github
能够经过 service elasticsearch start/stop 启动关闭服务,默认监听了 9200端口,能够更改配置文件json
经过deb安装的配置文件在:/etc/elasticsearch/elasticsearch.ymlapp
若是要在localhost外链接elasticsearch ,更改配置文件中的 network.host:0.0.0.0curl
若是一块儿顺利就能够开始测试了elasticsearch
查看es基本信息 curl localhost:9200 列出全部的Index curl -X GET 'http://localhost:9200/_cat/indices?v' 列举每一个Index下的Type curl 'localhost:9200/_mapping?pretty=true' 添加Index curl -X PUT 'localhost:9200/weather' 删除Index curl -X DELETE 'localhost:9200/weather' 安装中文分词插件ik (安装完须要重启es) elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip 建立一个Index,并设置其结构和分词 curl -X PUT -H 'Content-Type: application/json' '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" } } } } }' 向Index增长记录 PUT方式 curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d ' { "user": "张三", "title": "工程师" }' POST方式(POST方式不须要传id,id随机生成) curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d ' { "user": "李四", "title": "工程师" }' 注意:若是没有先建立 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。因此,打字的时候要当心,不要写错 Index 的名称。 查看指定条目的记录 curl 'localhost:9200/accounts/person/1?pretty=true' 删除一条记录 curl -X DELETE 'localhost:9200/accounts/person/1' 更新一条记录 curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d ' { "user" : "张三", "title" : "软件开发" }' 查询全部记录 curl 'localhost:9200/accounts/person/_search?pretty=true' 简单查询 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query" : { "match" : { "title" : "工程" }}, "from": 1, #0开始 "size": 1, #返回几条数据 }' OR查询 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query" : { "match" : { "title" : "工程 哈哈" }} }' AND查询 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query": { "bool": { "must": [ { "match": { "title": "工程" } }, { "match": { "title": "哈哈" } } ] } } }'