ElasticSearch基本介绍(四)搜索(Query、Filter、Aggregation)

准备


导入测试用数据集

# 批量插入测试数据
POST /test_index/_bulk
{"index":{"_id":1}}
{"name":"zs","realname":"张三","age":22,"birthday":"2018-12-27","salary":1000.0,"address":"北京市昌平区沙阳路55号"}
{"index":{"_id":2}}
{"name":"ls","realname":"李四","age":24,"birthday":"2017-10-20","salary":5000.0,"address":"北京市朝阳区三里屯街道21号"}
{"index":{"_id":3}}
{"name":"ww","realname":"王五","age":28,"birthday":"2016-03-15","salary":4300.0,"address":"北京市海淀区中关村大街新中关商城2楼511室"}
{"index":{"_id":4}}
{"name":"zl","realname":"赵六","age":30,"birthday":"2003-04-19","salary":12300.0,"address":"北京市海淀区中关村软件园9号楼211室"}
{"index":{"_id":5}}
{"name":"tq","realname":"田七","age":35,"birthday":"2001-08-11","salary":1403.0,"address":"北京市海淀区西二旗地铁辉煌国际大厦负一楼"}

查询(Query)


1. 查看所有并按照年龄降序排列

DSL实现:

GET /test_index/_search
{
  "query":{
    "match_all":{}  # 查询所有
  },
  "sort":{
    "age":"desc"  # 按年龄倒序排列
  }
}

2. 查询第2页的用户(每页显示2条)

DSL实现:

GET /test_index/_search
{
  "query":{
    "match_all":{}  # 查询所有
  },
  "sort":{
    "age":"asc"   # 按年龄倒序排列
   },
  "from":2,         # 从(nowPage-1)*pageSize检索
  "size":2          # 查 pageSize条
}

3. 查询address在海淀区的所有用户,并高亮

基于全文检索的查询(分析检索关键词 匹配索引库 返回结果)

DSL实现:

GET /test_index/_search
{
  "query": {
    "match": {  #   match查询会分词 如:海淀区 会分词为 海 | 淀 | 区
      "address":"海淀区"
    }
  },
  "highlight": {
    "fields": { #  需要高亮的字段列表
      "address": {}     
    }
  }
}

4. 查询name是zs关键字的用户

基于Term词元查询

DSL实现:

GET /test_index/_search
{
  "query":{
    "term": {
      "name": {
        "value": "zs"
      }
    }
  }
}

5. 查询年龄在20~30岁之间的用户

基于范围查询

DSL实现:

GET /test_index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,   # 大于等于  大于用 gt
        "lte": 30    # 小于等于  小于用 lt
      }
    }
  }
}

6. 查询真实姓名以张开头的用户

基于前缀(prefix)查询

DSL实现:

GET /test_index/_search
{
  "query": {
    "prefix": {
      "realname": {
        "value": "李"
      }
    }
  }
}

7. 查询名字以s结尾的用户

基于通配符(wildcard)的查询

  • ?匹配一个字符
  • *匹配0~n个字符

DSL实现:

GET /test_index/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "*s"
      }
    }
  }
}

8. 查询id为1,2,3的用户

基于Ids的查询

DSL实现:

GET /test_index/_search
{
  "query": {
    "ids": {
      "values": [1,2,3]
    }
  }
}

9. 模糊查询realname中包含张关键字的用户

基于Fuzzy的查询

DSL实现:

GET /test_index/_search
{
  "query": {
    "fuzzy": {
      "realname": {"value": "张"}
    }
  }
}

10. 查询age在15-30岁之间并且name必须通配z*

基于Boolean的查询(多条件查询)

  • must:查询结果必须符合该查询条件(列表)。
  • should:类似于or的查询条件。
  • must_not:查询结果必须不符合查询条件(列表)。

DSL实现:

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [    #年龄在15~30岁之间并且必须名字通配z*
        {
          "range": {
            "age": {
              "gte": 15,
              "lte": 30
            }
          }
        },
        {
          "wildcard": {
            "name": {
              "value": "z*"
            }
          }
        }
      ],
      "must_not": [  # 正则查询 name必须不能以s结尾
        {
          "regexp": {
            "name": ".*s"
          }
        }
      ] 
    }
  }
}

过滤器(Filter)


准确来说,ES中的查询操作分为两种:查询(query)和过滤(filter)。

查询即是之前提到的query查询,查询默认会计算每个返回文档的得分,然后根据得分排序。

而过滤(filter)只会筛选出符合的文档,并不计算得分,且它可以缓存文档。

所以,单从性能考虑,过滤比查询更快。

换句话说,过滤适合在大范围筛选数据,而查询则适合精确匹配数据。

一般应用时,应先使用过滤操作过滤数据,然后使用查询匹配数据。

过滤器使用

GET /test_index/_search
{
   "query":{
      "bool": {
        "must": [
          {"match_all": {}}
        ],
        "filter": {     # 过滤年龄大于等于25岁的用户
          "range": {
            "age": {
              "gte": 25
            }
          }
        }
      }
   }
}

注意: 过滤查询运行时先执行过滤语句,后执行普通查询

过滤器的类型

1. term、terms Filter

termterms的含义与查询时一致。term用于精确匹配,terms用于多词条匹配

GET /test_index/_search
{
   "query":{
      "bool": {
        "must": [
          {"match_all": {}}
        ],
        "filter": {
          "terms": {
            "name": [
              "zs",
              "ls"
            ]
          }
        }
      }
   }
}
2. range filter
3. exists filter

exists过滤指定字段没有值的文档

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {    # 排除salary为null的结果
        "exists": {
          "field": "salary"
        }
      }
    }
  },
  "sort": [
    {
      "_id": {
        "order": "asc"
      }
    }
  ]
}

相反操作(查询出salary为null的结果)

GET /test_index/_search
{ 
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "salary"
          }
        }
      ]
    }
  }
}
4. ids filter

需要过滤出若干指定_id的文档,可使用标识符过滤器(ids)

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": "昌平区"
          }
        }
      ],
      "filter": {
        "ids": {   # id 过滤器
          "values": [
            1,
            2,
            3
          ]
        }
      }
    }
  }
}
5. 其余使用方式可查阅官网

聚合(Aggregations)


官方文档 6.x search-aggregations

聚合提供了功能可以分组并统计你的数据。

理解聚合最简单的方式就是可以把它粗略的看做SQLGROUP BY操作和SQL聚合函数

ES中常用的聚合:

  • metric(度量)聚合:度量类型聚合主要针对的number类型的数据,需要ES做比较多的计算工作,类似于关系型数据库的组函数操作
  • bucketing(桶)聚合:划分不同的“桶”,将数据分配到不同的“桶”里。非常类似sql中的group语句的含义,类似于关系型数据库的分组操作
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容