准备工作
1 2 3 4 5 |
# 安装 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.x.x-linux-x86_64.tar.gz # 请根据实际版本进行替换 tar -zxvf elasticsearch-7.x.x-linux-x86_64.tar.gz cd elasticsearch-7.x.x ./bin/elasticsearch # 启动,回-d 后台启动 |
访问地址:http://127.0.0.1:9200/
Elasticsearch Head
1 2 3 4 5 6 7 8 9 10 |
# 配置 Elasticsearch # vi config/elasticsearch.yml http.cors.enabled: true http.cors.allow-origin: "*" # 安装 wget https://github.com/mobz/elasticsearch-head/archive/master.zip unzip master.zip cd elasticsearch-head-master npm install npm run start |
访问http://localhost:9100/,若 Elasticsearch 处于启动状态则会自动连接
分布式集群搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# master # vi config/elasticsearch.yml cluster.name: my_cluster node.name: master node.master: true network.host: 127.0.0.1 # 端口不写默认为9200 # slave cluster.name: my_cluster node.name: slave1 network.host: 127.0.0.1 http.port: 9201 discovery.zen.ping.unicast.hosts: ["127.0.0.1"] |
参照以上从服务器的配置可配置多个 slave,使用中注意如果是拷贝原 master 或 slave 的文件夹的话,应删除根目录下的 data 目录
基础知识
RESTful API
- API基本格式 http://<ip>:<port>/<索引>/<类型>/<文档id>
创建索引
1 2 |
# 查看 unassigned的原因 curl -XGET localhost:9200/_cluster/allocation/explain?pretty |
创建、添加和更新
可使用Postman 协助操作,也可在 Elasticsearch Head 中执行操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
###创建### # PUT http://localhost:9200/people?include_type_name=true { "settings":{ "number_of_shards": 3, "number_of_replicas": 1 }, "mappings":{ "man": { "properties": { "name": { "type": "text" }, "country": { "type": "keyword" }, "age":{ "type": "integer" }, "date":{ "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } } ###添加### # POST http://localhost:9200/people/man { "_id": 1, // 不指定系统会自动生成 "name": "B先生", "country": "China", "age": 40, "date": "1977-03-07" } ###更新### # POST http://localhost:9200/people/man/1/_update # 直接更新 { "doc":{ "name":"AAA" } } # 脚本更新 # 年龄加10 { "script":{ "lang":"painless", "inline": "ctx._source.age += 10" } } # 使用参数更新 { "script":{ "lang":"painless", "inline": "ctx._source.age = params.age", "params":{ "age": 100 } } } ##删除## # 删除文档 # DELETE http://localhost:9200/people/man/1 # 删除索引 # DELETE http://localhost:9200/people ##查询## # GET http://localhost:9200/book/novel/_search # 查询所有记录 { "query":{ "match_all":{} } } # 仅显示一条 { "query":{ "match_all":{} }, "from": 1, "size": 1 } # 指定条件、指定排序 { "query":{ "match":{ "title":"ElasticSearch" } }, "sort":[ {"publish_date":{"order": "desc"}} ] } # 聚合查询 { "aggs":{ "group_by_word_count":{ "terms":{ "field":"word_count" } }, "group_by_publish_date":{ "terms":{ "field":"publish_date" } } } } |
常见问题
1、Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true
请求的链接后添加?include_type_name=true即可