版本说明
本文基于Elasticsearch6.4.0版本
关于DSL
DSL是Elasticsearch提供的一种基于JSON格式的查询方式。
演示数据
mapping映射
{
"emp": {
"mappings": {
"base_info": {
"properties": {
"age": {
"type": "integer"
},
"dept": {
"type": "long"
},
"detail": {
"type": "text"
},
"name": {
"type": "keyword"
},
"salary": {
"type": "double"
},
"sex": {
"type": "integer"
}
}
}
}
}
}
GET /emp/base_info/_search
已有数据
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "5",
"_score": 1,
"_source": {
"name": "赵六",
"age": 24,
"sex": 1,
"salary": 8888,
"detail": "普通人"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "10",
"_score": 1,
"_source": {
"name": "王者",
"age": 26,
"sez": 1,
"salary": 6666,
"detail": "渣渣辉"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 22,
"sex": 0,
"salary": 18888,
"detail": "才华横溢、天赋异禀"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"age": 18,
"sex": 1,
"salary": 6000,
"detail": "普通人"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "11",
"_score": 1,
"_source": {
"name": "小王",
"age": 26,
"sez": 1,
"salary": 1234,
"detail": "隔壁老王"
}
}
]
}
}
查询演示
1、返回所有文档信息(match_all)
GET /_search
{
"query":{
"match_all": {}
}
}
2、返回索引中的文档信息
GET /emp/base_info/_search
{
"query": {
"match_all": {}
}
}
3、条件查询(match)
GET /emp/base_info/_search
{
"query": {
"match": {
"name": "赵六"
}
}
}
查询结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "5",
"_score": 0.2876821,
"_source": {
"name": "赵六",
"age": 24,
"sex": 1,
"salary": 8888,
"detail": "普通人"
}
}
]
}
}
4、返回指定条数(size)
GET /emp/base_info/_search
{
"query": {
"match_all": {}
},
"size": 2
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "5",
"_score": 1,
"_source": {
"name": "赵六",
"age": 24,
"sex": 1,
"salary": 8888,
"detail": "普通人"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 22,
"sex": 0,
"salary": 18888,
"detail": "才华横溢、天赋异禀"
}
}
]
}
}
5、分页查询(size、from)
GET /emp/base_info/_search
{
"query": {
"match_all": {}
},
"size": 2,
"from": 1
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 22,
"sex": 0,
"salary": 18888,
"detail": "才华横溢、天赋异禀"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"age": 18,
"sex": 1,
"salary": 6000,
"detail": "普通人"
}
}
]
}
}
6、只查找指定字段
GET /emp/base_info/_search
{
"query":{
"match": {
"name": "赵六"
}
},
"_source": ["name","salary"]
}
查询结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "5",
"_score": 0.2876821,
"_source": {
"name": "赵六",
"salary": 8888
}
}
]
}
}
7、多字段查询(multi_match)
GET /emp/base_info/_search
{
"query":{
"multi_match": {
"query": "王者",
"fields": ["name","detail"] #在指定字段中按条件查询
}
}
}
查询结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.87546873,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 0.87546873,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "10",
"_score": 0.6931472,
"_source": {
"name": "王者",
"age": 26,
"sez": 1,
"salary": 6666,
"detail": "渣渣辉"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "11",
"_score": 0.18232156,
"_source": {
"name": "小王",
"age": 26,
"sez": 1,
"salary": 1234,
"detail": "隔壁老王"
}
}
]
}
}
8、分词查询(term)
GET /emp/base_info/_search
{
"query":{
"term": {
"detail": {
"value": "王"
}
}
}
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.18232156,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 0.18232156,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "11",
"_score": 0.18232156,
"_source": {
"name": "小王",
"age": 26,
"sez": 1,
"salary": 1234,
"detail": "隔壁老王"
}
}
]
}
}
如果查“老王”
GET /emp/base_info/_search
{
"query":{
"term": {
"detail": {
"value": "老王"
}
}
}
}
查询不到数据,说明默认的分词器对中文会按照一个字一个字的进行分词
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
9、范围查询(range)
GET /emp/base_info/_search
{
"query": {
"range": {
"salary": {
"gte": 10000,
"lte": 20000
}
}
}
}
查询结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 22,
"sex": 0,
"salary": 18888,
"detail": "才华横溢、天赋异禀"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
}
]
}
}
10、前缀查询(prefix)
GET /emp/base_info/_search
{
"query": {
"prefix": {
"name": {
"value": "王"
}
}
}
}
查询结果如下,name字段的type是keyword,之前使用match是查询不到的。
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "10",
"_score": 1,
"_source": {
"name": "王者",
"age": 26,
"sez": 1,
"salary": 6666,
"detail": "渣渣辉"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
}
]
}
}
11、通配符查询(wildcard)
GET /emp/base_info/_search
{
"query": {
"wildcard": {
"name": {
"value": "王*"
}
}
}
}
查询结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "10",
"_score": 1,
"_source": {
"name": "王者",
"age": 26,
"sez": 1,
"salary": 6666,
"detail": "渣渣辉"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"age": 23,
"sex": 1,
"salary": 10000,
"detail": "全能王者"
}
}
]
}
}
12、模糊查询(fuzzy)
GET /emp/base_info/_search
{
"query": {
"fuzzy": {
"detail": "王"
}
}
}
13、ids查询
GET /emp/base_info/_search
{
"query": {
"ids": {
"values": ["1","2"]
}
}
}
查询结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "emp",
"_type": "base_info",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 22,
"sex": 0,
"salary": 18888,
"detail": "才华横溢、天赋异禀"
}
},
{
"_index": "emp",
"_type": "base_info",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"age": 18,
"sex": 1,
"salary": 6000,
"detail": "普通人"
}
}
]
}
}