小伙伴们,你们好呀,我是老寇,跟我一起学习es 7.6.2
注:请点击我,获取源码
视频地址
老寇-es通过关键字高亮显示
一、搜索入门1.无条件搜索命令
GET /index/_search
GET /message/_search
2.传参搜索命令
GET /index/_search?q=filed:value
GET /message/_search?q=id:1424966164936024065
问题扩展: + 和 - 区别(见如下举例说明)
GET /message/_search?q=+id:1424966164936024065 #查询id=1424966164936024065的数据 GET /message/_search?q=-id:1424966164936024065 #查询id!=1424966164936024065的数据
3.分页搜索命令
GET /index/_search?size=x&from=x
GET /message/_search?size=10&from=0 注:类似sql > select * from message 0,10
问题扩展:分页过深,对性能有哪些影响?
1.消耗网络带宽,搜的过深,各分片(shard)要把数据传递给协调节点(coordinating node),这个过程中有大量数据传输,消耗网络 2.消耗内存,各节点(shard)要把数据传给协调节点(coordinating node),这个传递回来的数据,被协调节点(coordinating node)保存在内存中,这样会大量消耗内存 3.消耗cpu,协调节点(coordinating node)要把传回来的数据进行排序,这个排序过程很消耗cpu 因此,出于对深度分页(deep paging)的性能考虑,能少用就尽量少用二、DSL入门
es所独有的搜索语言(ps:有点类似sql语言),可以在请求体携带搜索条件,功能强大
1.查询全部
举个栗子
GET /message/_search
{
"query": {
"match_all": {}
}
}
注:看到这里,小伙伴们就要问了,es的get请求为什么可以携带请求体?这是因为es对此做出处理,至于是怎么处理的,感兴趣的小伙伴,可以去查阅资料
2.数据排序
举个栗子
GET /message/_search
{
"query": {
"match": {
"desc": "群聊"
}
},
"sort": [
{
"createDate": {
"order": "desc"
}
}
]
}
3.分页查询
举个栗子
GET /message/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
4.返回指定字段
举个栗子
GET /message/_search
{
"query": {
"match_all": {}
},
"_source": ["username","data"]
}
三、Query DSL语法
1.DSL 命令
{
query_name: {
argument:value
......
}
}
或者
{
query_name:{
field_name:{
argument:value
......
}
}
}
举个栗子
GET /message/_search
{
"query": {
"match": {
"desc": "群聊"
}
}
}
2.多条件组合搜索
举个栗子
GET /message/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"username": "admin"
}
}
],
"should": [
{
"match": {
"desc": "群聊"
}
}
],
"must_not": [
{
"match": {
"desc": "私聊"
}
}
]
}
}
}
GET /message/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"sendId": "1363109342432645122"
}
}
],
"should": [
{
"match": {
"username": "admin"
}
},
{
"bool": {
"must": [
{
"match": {
"data": "无名,天地之始,有名,万物之母。"
}
}
]
}
}
]
}
}
}



