Elasticsearch第二篇!

安装

  1. 官网:https://www.elastic.co/cn/downloads/elasticsearch
  2. 下载:wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-darwin-x86_64.tar.gz
  3. 解压:tar -zxvf elasticsearch-7.5.0-darwin-x86_64.tar.gz
  4. 总写入量:. cd elasticsearch-7.5.0
  5. sh ./bin/elasticsearch
  6. 访问 127.0.0.1:9200

依赖java环境

head插件

  1. 下载: wget https://github.com/mobz/elasticsearch-head/archive/master.zip
  2. 解压 unzip master.zip
  3. cd www/es/elasticsearch-head-master
  4. npm install
  5. npm run start
  6. 访问 127.0.0.1:9100

elasticsearch 配置修改
vim config/elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

# 集群配置
cluster.name: uiste
node.name: master
node.master: true

network.host: 127.0.0.1

# 一下内容是从节点配置信息
#cluster.name: uiste
#node.name: slave1
#network.host: 127.0.0.1
#http.port: 8200
#discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

依赖node环境

索引创建

API基本格式:

1
http://[ip]:[port]/[索引]/[类型]/[文档ID]

请求示例:

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
URL:127.0.0.1:9100/people
METHOD: PUT
PARAMS:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"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"
}
}
}
}
RESPONSE:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "people"
}

异常情况执行结果出错:Root mapping definition has unsupported parameters
出现这个的原因是,elasticsearch7默认不在支持指定索引类型,默认索引类型是_doc,如果想改变,则配置include_type_name: true 即可(这个没有测试,官方文档说的,无论是否可行,建议不要这么做,因为elasticsearch8后就不在提供该字段)。官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html

索引查询

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
URL: 127.0.0.1:9200/people/_mapping
METHOD: GET
PARAMS:
RESPONSE:
{
"people": {
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"country": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"name": {
"type": "text"
}
}
}
}
}

添加更新文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
URL: 127.0.0.1:9200/people/_doc/1
METHOD:POST
PARAMS:
{
"name": "uiste",
"country": "China",
"age": 18,
"data": "1999-01-01"
}
RESPONSE:
{
"_index": "people",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 2
}

查询文档

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
URL:127.0.0.1:9200/people/_search
METHOD:POST
PARAMS:
{
"query":{
"match_all":{}
}
}
RESPONSE:
{
"took": 453,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"name": "uiste",
"country": "China",
"age": 18,
"data": "1999-01-01"
}
}
]
}
}

参考文档
https://www.cnblogs.com/Rasang/p/12041729.html