• Elasticsearch基础条件查询


    条件查询

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

    #第一种
    GET /shopping/_search?q=名字:张三
    
    #第二种
    GET /shopping/_search
    {
      "query": {
        "match": {
          "名字": "张三"
        }
      }
    }
    
    #全量查询 match_all
    GET /shopping/_search
    {
      "query": {
        "match_all": {
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    分页查询

    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": ["名字"]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    查询排序

    order:排序
    desc:降序

    # 降序排序,按照年龄降序搜索名字
    GET /shopping/_search
    {
      "query": {
        "match_all": {
        }
      },
      "_source": ["名字"],
      "sort": {
        "年龄":{
          "order" : "desc"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    多条件查询

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

    #条件同时成立,名字为张三和年龄为36岁
    GET /shopping/_search
    {
      "query": {
        "bool":{
          "must": [
            {
              "match": {
                "名字": "张三"
              }
            },
            {
              "match": {
                "年龄": 36
            }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    should:查询类似or,或者

    #条件为搜索名字为张三或李四
    GET /shopping/_search
    {
      "query": {
        "bool":{
          "should": [
            {
              "match": {
                "名字": "张三"
              }
            },
            {
              "match": {
                "名字": "李四"
            }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    范围查询

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

    #条件查询名字张三或李四年龄大于35岁到40岁之间
    GET /shopping/_search
    {
      "query": {
        "bool":{
          "should": [
            {
              "match": {
                "名字": "张三"
              }
            },
            {
              "match": {
                "名字": "李四"
            }
            }
          ],
          "filter": [
            {
              "range": {
                "年龄": {
                  "gte": 35,
                  "lte": 40
                }
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    全文检索

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

    GET /shopping/_search
    {
      "query": {
        "match": {
          "名字": "张"
        }
      }
    }
    
    GET /shopping/_search
    {
      "query": {
        "match": {
          "名字": "三"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    完全匹配

    match_phrase:完全匹配

    GET /shopping/_search
    {
      "query": {
        "match_phrase": {
          "名字": "张三"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    高亮查询

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

    #对名字高亮显示
    GET /shopping/_search
    {
      "query": {
        "match_phrase": {
          "名字": "张三"
        }
      },
      "highlight": {
        "fields": {
          "名字": {}
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    聚合查询

    aggs:聚合操作

    #将所有年龄分组分别统计出来
    GET /shopping/_search
    {
      "aggs":{ //聚合操作
        "age_group": { //统计结果名称,命名随意
          "terms": { //分组操作
            "field": "年龄"  //分组字段
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

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

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

    #创建索引,并定义映射
    PUT /user
    PUT /user/_mapping
    {
      "properties" : {
        "name" : {
          "type" : "text",
          "index" : true
        },
        "sex": {
          "type" : "keyword", //关键字,完全匹配
          "index" : true
        },
        "phone": {
          "type": "keyword", //关键字,完全匹配
          "index" : false
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    #user索引创建数据
    PUT /user/_create/1001
    {
      "name": "小米",
      "sex": "man",
      "phone": 123456789
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    #查询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"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    智哪儿专访:小匠物联如何帮助颈部按摩仪品类完成智能化升级
    潮玩宇宙源码开发:开启全新的数字潮流时代
    OMO模式成为教育行业“标配“
    Leetcode 1758. 生成交替二进制字符串的最少操作数
    numpy线性代数模块linalg总结
    给微信小程序添加隐私协议
    基于深度学习的监控场景下是否佩戴安全帽检测(yolov5)
    sublime 文件编辑器使用快捷键
    C++(Qt)软件调试---GCC编译参数学习-程序检测(13)
    外包“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer...
  • 原文地址:https://blog.csdn.net/weixin_43757402/article/details/134422481