Elasticsearch基础条件查询

条件查询

query:查询
match:匹配
match_all:匹配所有

#第一种
GET /shopping/_search?q=名字:张三

#第二种
GET /shopping/_search
{
  "query": {
    "match": {
      "名字": "张三"
    }
  }
}

#全量查询 match_all
GET /shopping/_search
{
  "query": {
    "match_all": {
    }
  }
}
分页查询

from开始计算公式:(页码-1) * 每页数据条数
from:表示从第几行开始
size:表示查询多少条文档

#查询从0行开始
GET /shopping/_search
{
  "query": {
    "match_all": {
    }
  },
  "from": 0,
  "size": 2
}

#数据源过滤,只查找_source包含名字的行
GET /shopping/_search
{
  "query": {
    "match_all": {
    }
  },
  "from": 0,
  "size": 2,
  "_source": ["名字"]
}
查询排序

order:排序
desc:降序

# 降序排序,按照年龄降序搜索名字
GET /shopping/_search
{
  "query": {
    "match_all": {
    }
  },
  "_source": ["名字"],
  "sort": {
    "年龄":{
      "order" : "desc"
    }
  }
}
多条件查询

bool:条件
must:类似and,必须 多条件同时成立

#条件同时成立,名字为张三和年龄为36岁
GET /shopping/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match": {
            "名字": "张三"
          }
        },
        {
          "match": {
            "年龄": 36
        }
        }
      ]
    }
  }
}

should:查询类似or,或者

#条件为搜索名字为张三或李四
GET /shopping/_search
{
  "query": {
    "bool":{
      "should": [
        {
          "match": {
            "名字": "张三"
          }
        },
        {
          "match": {
            "名字": "李四"
        }
        }
      ]
    }
  }
}
范围查询

filter:过滤
range:范围
gte:大于
lte:小于

#条件查询名字张三或李四年龄大于35岁到40岁之间
GET /shopping/_search
{
  "query": {
    "bool":{
      "should": [
        {
          "match": {
            "名字": "张三"
          }
        },
        {
          "match": {
            "名字": "李四"
        }
        }
      ],
      "filter": [
        {
          "range": {
            "年龄": {
              "gte": 35,
              "lte": 40
            }
          }
        }
      ]
    }
  }
}
全文检索

在es中,有文字的一部分也能正常查询到数据,es会将内容分词在倒排索引中匹配,比如“张三”,匹配“张”或者“三”都会进行匹配

GET /shopping/_search
{
  "query": {
    "match": {
      "名字": "张"
    }
  }
}

GET /shopping/_search
{
  "query": {
    "match": {
      "名字": "三"
    }
  }
}

在这里插入图片描述

完全匹配

match_phrase:完全匹配

GET /shopping/_search
{
  "query": {
    "match_phrase": {
      "名字": "张三"
    }
  }
}
高亮查询

highlight:高亮字段
其实就是特殊的内容进行样式的设定

#对名字高亮显示
GET /shopping/_search
{
  "query": {
    "match_phrase": {
      "名字": "张三"
    }
  },
  "highlight": {
    "fields": {
      "名字": {}
    }
  }
}

在这里插入图片描述

聚合查询

aggs:聚合操作

#将所有年龄分组分别统计出来
GET /shopping/_search
{
  "aggs":{ //聚合操作
    "age_group": { //统计结果名称,命名随意
      "terms": { //分组操作
        "field": "年龄"  //分组字段
      }
    }
  }
}

在这里插入图片描述

GET /shopping/_search
{
  "aggs":{ //聚合操作
    "age_group": { //统计结果名称,命名随意
      "terms": { //分组操作
        "field": "年龄"  //分组字段
      }
    }
  },
  "size": 0 //取消原始数据,只保留统计后数据
}
#统计结果为年龄的平均值
GET /shopping/_search
{
  "aggs":{ //聚合操作
    "age_agv": { //统计结果名称,命名随意,
      "avg": { //分组操作
        "field": "年龄"  //分组字段
      }
    }
  },
  "size": 0
}
映射关系

properties:特性
sex:性别
keyword:关键字

#创建索引,并定义映射
PUT /user
PUT /user/_mapping
{
  "properties" : {
    "name" : {
      "type" : "text",
      "index" : true
    },
    "sex": {
      "type" : "keyword", //关键字,完全匹配
      "index" : true
    },
    "phone": {
      "type": "keyword", //关键字,完全匹配
      "index" : false
    }
  }
}
#user索引创建数据
PUT /user/_create/1001
{
  "name": "小米",
  "sex": "man",
  "phone": 123456789
}
#查询name模糊匹配值存在,因为创建时type为text
GET /user/_search
{
  "query": {
    "match": {
      "name": "小"
    }
  }
}

#查询sex模糊匹配值为空,因为创建时type为keyword
GET /user/_search
{
  "query": {
    "match": {
      "sex": "ma"
    }
  }
}

#查询phone匹配为空,因为创建时index为false,不能被索引查询
GET /user/_search
{
  "query": {
    "match": {
      "phone": "123456"
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值