Elasticsearch基础条件查询

条件查询

query:查询

match:匹配

match_all:匹配所有

csharp 复制代码
#第一种
GET /shopping/_search?q=名字:张三

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

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

from开始计算公式:(页码-1) * 每页数据条数

from:表示从第几行开始

size:表示查询多少条文档

csharp 复制代码
#查询从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:降序

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

bool:条件

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

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

should:查询类似or,或者

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

filter:过滤

range:范围

gte:大于

lte:小于

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

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

csharp 复制代码
GET /shopping/_search
{
  "query": {
    "match": {
      "名字": "张"
    }
  }
}

GET /shopping/_search
{
  "query": {
    "match": {
      "名字": "三"
    }
  }
}
完全匹配

match_phrase:完全匹配

csharp 复制代码
GET /shopping/_search
{
  "query": {
    "match_phrase": {
      "名字": "张三"
    }
  }
}
高亮查询

highlight:高亮字段

其实就是特殊的内容进行样式的设定

csharp 复制代码
#对名字高亮显示
GET /shopping/_search
{
  "query": {
    "match_phrase": {
      "名字": "张三"
    }
  },
  "highlight": {
    "fields": {
      "名字": {}
    }
  }
}
聚合查询

aggs:聚合操作

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

properties:特性

sex:性别

keyword:关键字

csharp 复制代码
#创建索引,并定义映射
PUT /user
PUT /user/_mapping
{
  "properties" : {
    "name" : {
      "type" : "text",
      "index" : true
    },
    "sex": {
      "type" : "keyword", //关键字,完全匹配
      "index" : true
    },
    "phone": {
      "type": "keyword", //关键字,完全匹配
      "index" : false
    }
  }
}
csharp 复制代码
#user索引创建数据
PUT /user/_create/1001
{
  "name": "小米",
  "sex": "man",
  "phone": 123456789
}
csharp 复制代码
#查询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"
    }
  }
}
相关推荐
格图素书8 分钟前
大数据在电力行业的应用案例解析-【电力技术】(零)大数据在电力行业的典型落地案例(序)
大数据·单例模式
百胜软件@百胜软件18 分钟前
对话文斌:E3+PRO的“AI大脑”——『胜券商品』如何让数据智能触手可及?
大数据·人工智能
码农小白AI1 小时前
AI报告文档审核助力排气烟度精准管控:IACheck守护绿色动力环境与合规发展新底线
大数据·人工智能
炼丹炉大数据1 小时前
炼丹炉:宠物电商数据工具首选
大数据·数据分析·宠物
ctrigger1 小时前
人力资源和社会保障部研究起草《人力资源社会保障部关于修改〈职称评审管理暂行规定〉的决定(征求意见稿)》
大数据
珠海西格2 小时前
四可装置如何监测组件衰减与逆变器效率?
大数据·运维·服务器·分布式·能源
瑞和数智2 小时前
案例分享 | 瑞和数智助力某农商行打造标签管理平台
大数据·人工智能·科技·金融
科技前瞻观察2 小时前
技术自主、量产突围、产业链协同:宇树科技、优艾智合领衔具身智能TOP20领跑全球
大数据·人工智能·科技
电商API&Tina2 小时前
比价 / 选品专用:京东 + 淘宝 核心接口实战(可直接复制运行)
大数据·数据库·人工智能·python·json·音视频
intcube3 小时前
从“数”到“智”——智达方通EPM如何推动企业韧性增长与创新?
大数据·人工智能·全面预算管理·财务规划·商业智能