ES查询文档
method | url 地址 | 描述 |
---|---|---|
PUT | localhost:9200/索引名称/类型名称/文档 id | 创建文档(指定文档 id) |
POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档 id) |
POST | localhost:9200/索引名称/类型名称/文档 id/_update | 修改文档 |
DELETE | localhost:9200/索引名称/类型名称/文档 id | 删除文档 |
GET | localhost:9200/索引名称/类型名称/文档 id | 查询文档(通过文档 ID) |
GET | localhost:9200/索引名称/类型名称/_search | 查询所有数据 |
GET | /索引名称 | 查询索引信息 |
GET | /索引名称/类型名称/_search?q=name:mrcdh | 根据条件查询文档 |
# 创建一个索引
# 语法
PUT /索引名/类型名/文档id
{请求体}
1
2
2
PUT /test1/type1/1
{
"name": "mrcdh",
"age": 18
}
1
2
3
4
5
2
3
4
5
# 返回结果
{
"_index": "test1",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建索引规则
name
类型为text
age
类型为long
birthday
类型为date
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"aget": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 复杂搜索
# 精确查询 match
查询 name
等于 张三
的文档
GET /mrcdh/user/_search
{
"query": {
"match": {
"name": "张三"
}
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 多条件查询 bool
通过 bool
来进行多条件查询
must
: 所有条件都要符合,相当于 mysql 的and
should
: 只要有一个条件符合,相当于 mysql 的or
must_not
: 反向操作,相当于 mysql 的not
filter
: 使用filter
进行数据过滤
GET /mrcdh/user/_search
{
"query": {
"bool":{
"must": [
{
"match": {
"name": "张三"
}
},
{
"match": {
"age": 18
}
}
],
"should": [
{
"match": {
"name": "张三"
}
}
],
"must_not": [
{
"match": {
"name": "张三"
}
}
],
"filter": {
"range": {
"age": {
"gt": 20
}
}
}
}
},
}
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
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
# 获取指定字段 _source
使用 _source
来指定要获取的字段
GET /mrcdh/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"_source": ["name", "desc"]
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 排序 sort
GET /mrcdh/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"sort": [
{
"排序字段": {
"order": "desc"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 分页 from
size
from
: 从第几条开始size
: 取几条数据
GET /mrcdh/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"from": 0,
"size": 1
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 高亮查询 highlight
搜索的结果可以高亮显示
fields
: 指定要高亮的字段pre_tags
: 自定义高亮包裹开始标签post_tags
: 自定义高亮包裹结束标签
GET /mrcdh/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color: red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
返回
{
"took": 50,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.8132977,
"hits": [
{
"_index": "mrcdh",
"_type": "user",
"_id": "2",
"_score": 1.8132977,
"_source": {
"name": "张三",
"aget": 3,
"desc": "法外狂徒",
"tags": ["打炮", "旅游", "渣男"]
},
"highlight": {
"name": ["<em>张</em><em>三</em>"]
}
}
]
}
}
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
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
上次更新: 2023/09/22, 16:54:32