DSL-指定返回字段
# 指定
默认情况下,Elasticsearch 在搜索的结果中,会把文档中保存在_source
的所有字段都返回,如果只想获取其中的部分字段,可以添加_source
的过滤
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"_source":["name","age"],
"query": {
"term": {
"name": {
"value": "zhangsan"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_score": 1.540445,
"_source": {
"name": "zhangsan",
"age": 30
}
}
]
}
}
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
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
# 排除
excludes 排除不想要显示的字段
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"_source":{
"excludes":["name"]
},
"query": {
"term": {
"name": {
"value": "zhangsan"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_score": 1.540445,
"_source": {
"sex": "男",
"nickname": "zhangsan",
"age": 30
}
}
]
}
}
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
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
# 包含
includes:来指定想要显示的字段,效果同上面的指定字段
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"_source":{
"includes":["name"]
},
"query": {
"term": {
"name": {
"value": "zhangsan"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_score": 1.540445,
"_source": {
"name": "zhangsan"
}
}
]
}
}
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
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
Last Updated: 2022/02/05, 15:58:51