Query DSL
match_all
匹配所有文档,默认分页查询,只显示后十条数据,如下所示在索引中的数据有11条数据,在进行match_all查询时只返回了十条数据.
"total": {
"value": 11,
"relation": "eq"
}
GET /es_db/_search
{
"query": {
"match_all": {
}
}
}
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "es_db",
"_id": "10",
"_score": 1,
"_source": {
"name": "开人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
},
{
"_index": "es_db",
"_id": "8",
"_score": 1,
"_source": {
"name": "张星",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
},
{
"_index": "es_db",
"_id": "6",
"_score": 1,
"_source": {
"name": "赵虎",
"sex": 1,
"age": 32,
"address": "长沙麓谷兴工国际产业园",
"remark": "java architect"
}
},
{
"_index": "es_db",
"_id": "5",
"_score": 1,
"_source": {
"name": "张龙",
"sex": 0,
"age": 19,
"address": "长沙麓谷企业广场",
"remark": "java architect assistant"
}
},
{
"_index": "es_db",
"_id": "7",
"_score": 1,
"_source": {
"name": "李虎",
"sex": 1,
"age": 32,
"address": "广州番禺节能科技园",
"remark": "java architect"
}
},
{
"_index": "es_db",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"sex": 0,
"age": 26,
"address": "广州白云山公园",
"remark": "php developer"
}
},
{
"_index": "es_db",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"sex": 1,
"age": 25,
"address": "广州天河公园",
"remark": "java developer"
}
},
{
"_index": "es_db",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"sex": 1,
"age": 28,
"address": "广州荔湾大厦",
"remark": "java assistant"
}
},
{
"_index": "es_db",
"_id": "4",
"_score": 1,
"_source": {
"name": "赵六",
"sex": 0,
"age": 22,
"address": "长沙橘子洲",
"remark": "python assistant"
}
},
{
"_index": "es_db",
"_id": "11",
"_score": 1,
"_source": {
"name": "企业人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
}
]
}
}
如果想要指定分页查询可以使用from和size参数,如下所示,from表示从第几条记录开始分页,size表示每页展示几条数据
GET /es_db/_search
{
"query": {
"match_all": {
}
},
"from": 0,
"size": 11
}
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "es_db",
"_id": "10",
"_score": 1,
"_source": {
"name": "开人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
},
{
"_index": "es_db",
"_id": "8",
"_score": 1,
"_source": {
"name": "张星",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
},
{
"_index": "es_db",
"_id": "6",
"_score": 1,
"_source": {
"name": "赵虎",
"sex": 1,
"age": 32,
"address": "长沙麓谷兴工国际产业园",
"remark": "java architect"
}
},
{
"_index": "es_db",
"_id": "5",
"_score": 1,
"_source": {
"name": "张龙",
"sex": 0,
"age": 19,
"address": "长沙麓谷企业广场",
"remark": "java architect assistant"
}
},
{
"_index": "es_db",
"_id": "7",
"_score": 1,
"_source": {
"name": "李虎",
"sex": 1,
"age": 32,
"address": "广州番禺节能科技园",
"remark": "java architect"
}
},
{
"_index": "es_db",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"sex": 0,
"age": 26,
"address": "广州白云山公园",
"remark": "php developer"
}
},
{
"_index": "es_db",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"sex": 1,
"age": 25,
"address": "广州天河公园",
"remark": "java developer"
}
},
{
"_index": "es_db",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"sex": 1,
"age": 28,
"address": "广州荔湾大厦",
"remark": "java assistant"
}
},
{
"_index": "es_db",
"_id": "4",
"_score": 1,
"_source": {
"name": "赵六",
"sex": 0,
"age": 22,
"address": "长沙橘子洲",
"remark": "python assistant"
}
},
{
"_index": "es_db",
"_id": "11",
"_score": 1,
"_source": {
"name": "企业人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
},
{
"_index": "es_db",
"_id": "9",
"_score": 1,
"_source": {
"name": "泥马勒戈壁",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
}
}
]
}
}
_source
返回字段限制,如下所示,如果只想显示name和index字段可以以以下方式进行查询
GET /es_db/_search
{
"_source": ["name", "age"],
"query": {
"match_all": {
}
}
}
查询结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "es_db",
"_id": "10",
"_score": 1,
"_source": {
"name": "开人员",
"age": 32
}
},
{
"_index": "es_db",
"_id": "8",
"_score": 1,
"_source": {
"name": "张星",
"age": 32
}
},
{
"_index": "es_db",
"_id": "6",
"_score": 1,
"_source": {
"name": "赵虎",
"age": 32
}
},
{
"_index": "es_db",
"_id": "5",
"_score": 1,
"_source": {
"name": "张龙",
"age": 19
}
},
{
"_index": "es_db",
"_id": "7",
"_score": 1,
"_source": {
"name": "李虎",
"age": 32
}
},
{
"_index": "es_db",
"_id": "3",
"_score": 1,
"_source": {
"name": "王五",
"age": 26
}
},
{
"_index": "es_db",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三",
"age": 25
}
},
{
"_index": "es_db",
"_id": "2",
"_score": 1,
"_source": {
"name": "李四",
"age": 28
}
},
{
"_index": "es_db",
"_id": "4",
"_score": 1,
"_source": {
"name": "赵六",
"age": 22
}
},
{
"_index": "es_db",
"_id": "11",
"_score": 1,
"_source": {
"name": "企业人员",
"age": 32
}
}
]
}
}
排序
sort: {
"排序字段": {
"sort": "倒序/正序"
}
}
如下所示,对sex进行排序查询
GET /es_db/_search
{
"query": {
"match_all": {
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
术语级别查询
所谓术语级别查询就是不经过分词器直接进行查询,搜索的字段大多为非text类型。
term query
精确匹配,例如想要查询年龄、姓名字段进行一比一匹配,如果查询字段经过了分词,那么使用term查询则会查不到该数据,如下所示要查sex为0的结果。
GET /es_db/_search
{
"query": {
"term": {
"sex": {
"value": 0
}
}
}
}
address字段是name类型,它经过分词,如果直接使用term查询则会查不到数据,例如查姓名为王五的数据会提示查不到,但是我们数据确实有王五这条记录。
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "es_db",
"_id": "5",
"_score": null,
"_source": {
"name": "张龙",
"sex": 0,
"age": 19,
"address": "长沙麓谷企业广场",
"remark": "java architect assistant"
},
"sort": [
19
]
},
{
"_index": "es_db",
"_id": "4",
"_score": null,
"_source": {
"name": "赵六",
"sex": 0,
"age": 22,
"address": "长沙橘子洲",
"remark": "python assistant"
},
"sort": [
22
]
},
{
"_index": "es_db",
"_id": "1",
"_score": null,
"_source": {
"name": "张三",
"sex": 1,
"age": 25,
"address": "广州天河公园",
"remark": "java developer"
},
"sort": [
25
]
},
{
"_index": "es_db",
"_id": "3",
"_score": null,
"_source": {
"name": "王五",
"sex": 0,
"age": 26,
"address": "广州白云山公园",
"remark": "php developer"
},
"sort": [
26
]
},
{
"_index": "es_db",
"_id": "2",
"_score": null,
"_source": {
"name": "李四",
"sex": 1,
"age": 28,
"address": "广州荔湾大厦",
"remark": "java assistant"
},
"sort": [
28
]
},
{
"_index": "es_db",
"_id": "10",
"_score": null,
"_source": {
"name": "开人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
},
"sort": [
32
]
},
{
"_index": "es_db",
"_id": "8",
"_score": null,
"_source": {
"name": "张星",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
},
"sort": [
32
]
},
{
"_index": "es_db",
"_id": "6",
"_score": null,
"_source": {
"name": "赵虎",
"sex": 1,
"age": 32,
"address": "长沙麓谷兴工国际产业园",
"remark": "java architect"
},
"sort": [
32
]
},
{
"_index": "es_db",
"_id": "7",
"_score": null,
"_source": {
"name": "李虎",
"sex": 1,
"age": 32,
"address": "广州番禺节能科技园",
"remark": "java architect"
},
"sort": [
32
]
},
{
"_index": "es_db",
"_id": "11",
"_score": null,
"_source": {
"name": "企业人员",
"sex": 1,
"age": 32,
"address": "武汉东湖高新区未来智汇城",
"remark": "golang developer"
},
"sort": [
32
]
}
]
}
}
GET /es_db/_search
{
"query": {
"term": {
"name": {
"value": "王五"
}
}
}
}
前缀匹配
从倒排索引表中过滤出符合指定其前缀的结果并将匹配结果进行返回。
如下所示查询name字段的值以开开头的文档
GET /es_db/_search
{
"query": {
"prefix": {
"name": {
"value": "开"
}
}
}
}
判断文档是否存在指定字段
判断文档是否存在指定字段,如果存在则返回,如下所示,判断cest是否存在。
GET /es_db/_search
{
"query": {
"exists": {
"field": "cest"
}
}
}
ids
过滤出数组中匹配的id结果。如下所示将id等于5和7的文档过滤出来。
GET /es_db/_search
{
"query": {
"ids": {
"values": [5, 7]
}
}
}
gte lte
根据大小进行过滤,如下所示,获取年龄大于10小于20的文档
GET /es_db/_search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
日期可以根据当前时间+-进行过滤,如下所示,过滤2年前到当前时间的记录
GET /es_db/_search
{
"query": {
"range": {
"date": {
"gte": "now-2y"
}
}
}
}
前缀查询
从倒排索引表中过滤出指定前缀的结果,将结果符合的条件管理的记录进行返回,如下所示,查询name以开开头的记录
GET /es_db/_search
{
"query": {
"prefix": {
"name": {
"value": "开"
}
}
}
}
通配符
类似MySQL的模糊查询,和前缀查询一样,这个模糊查询也是查的倒排索引表
全文检索
对关键字进行分词后再进行查询操作。可以根据不同的匹配度返回不同的分数,匹配度越高分数越高。
虽然道路是曲折的,但前途是光明的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律