前言

本文隶属于专栏《1000个问题搞定大数据技术体系》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构和文献引用请见1000个问题搞定大数据技术体系

正文

数据准备

在kibana提供的界面上进行操作。

POST /school/student/_bulk
{ "index": { "_id": 1 }}
{ "name" : "liubei", "age" : 20 , "sex": "boy", "birth": "1996-01-02" , "about": "i like diaocan he girl" }
{ "index": { "_id": 2 }}
{ "name" : "guanyu", "age" : 21 , "sex": "boy", "birth": "1995-01-02" , "about": "i like diaocan" }
{ "index": { "_id": 3 }}
{ "name" : "zhangfei", "age" : 18 , "sex": "boy", "birth": "1998-01-02" , "about": "i like travel" }
{ "index": { "_id": 4 }}
{ "name" : "diaocan", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and sport" }
{ "index": { "_id": 5 }}
{ "name" : "panjinlian", "age" : 25 , "sex": "girl", "birth": "1991-01-02" , "about": "i like travel and wusong" }
{ "index": { "_id": 6 }}
{ "name" : "caocao", "age" : 30 , "sex": "boy", "birth": "1988-01-02" , "about": "i like xiaoqiao" }
{ "index": { "_id": 7 }}
{ "name" : "zhaoyun", "age" : 31 , "sex": "boy", "birth": "1997-01-02" , "about": "i like travel and music" }
{ "index": { "_id": 8 }}
{ "name" : "xiaoqiao", "age" : 18 , "sex": "girl", "birth": "1998-01-02" , "about": "i like caocao" }
{ "index": { "_id": 9 }}
{ "name" : "daqiao", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and history" }

1、使用match_all做查询

GET /school/student/_search?pretty
{
    "query": {
        "match_all": {}
    }
}

问题:通过match_all匹配后,会把所有的数据检索出来,但是往往真正的业务需求并非要找全部的数据,而是检索出自己想要的;并且对于es集群来说,直接检索全部的数据,很容易造成GC现象。所以,我们要学会如何进行高效的检索数据

2、通过关键字段进行查询

GET /school/student/_search?pretty
{
    "query": {
         "match": {"about": "travel"}
     }
}

如果此时想查询喜欢旅游的,并且不能是男孩的,怎么办?

在这里插入图片描述

这种方式是错误的,因为一个match下,不能出现多个字段值

[match] query doesn’t support multiple fields,需要使用复合查询

3、bool的复合查询

当出现多个查询语句组合的时候,可以用bool来包含。

bool合并聚包含:mustmust_not或者should, should表示or的意思

例子:查询非男性中喜欢旅行的人

GET /school/student/_search?pretty
{
"query": {
   "bool": {
      "must": { "match": {"about": "travel"}},
      "must_not": {"match": {"sex": "boy"}}
     }
  }
}

4、bool的复合查询中的should

should表示可有可无的(如果should匹配到了就展示,否则就不展示)

例子:查询喜欢旅行的,如果有男性的则显示,否则不显示

GET /school/student/_search?pretty
{
"query": {
   "bool": {
      "must": { "match": {"about": "travel"}},
      "should": {"match": {"sex": "boy"}}         
     }
  }
}

5、term匹配

使用term进行精确匹配

比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型)

语法

{ "term": { "age": 20 }}
{ "term": { "date": "2018-04-01" }}
{ "term": { "sex": “boy” }}
{ "term": { "about": "trivel" }}

6、使用terms匹配多个值

GET /school/student/_search?pretty
{
"query": {
   "bool": {
      "must": { "terms": {"about": ["travel","history"]}}          
     }
  }
}

term主要是用于精确的过滤

比如说:”我爱你”

在match下面匹配可以为包含:我、爱、你、我爱等等的解析器

在term语法下面就精准匹配到:”我爱你”

7、Range过滤

Range过滤允许我们按照指定的范围查找一些数据:

操作范围 含义
gt 大于
gte 大于等于
lt 小于
lte 小于等于

例子:查找出大于20岁,小于等于25岁的学生

GET /school/student/_search?pretty
{
"query": {
   "range": {
    "age": {"gt":20,"lte":25}
         }
      }
}

8、exists和 missing过滤

exists和missing过滤可以找到文档中是否包含某个字段或者是没有某个字段

例子:查找字段中包含age的文档

GET /school/student/_search?pretty
{
"query": {
   "exists": {
    "field": "age"  
         }
      }
}

9、bool的多条件过滤

用bool也可以像之前match一样来过滤多行条件:

关键字 含义 相当于
must 多个查询条件的完全匹配 and
must_not 多个查询条件的相反匹配 not
should 至少有一个查询条件匹配 or

例子:过滤出about字段包含travel并且年龄大于20岁小于30岁的同学

GET /school/student/_search?pretty
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "about": {
            "value": "travel"
          }
        }},{"range": {
          "age": {
            "gte": 20,
            "lte": 30
          }
        }}
      ]
    }
  }
}

10、查询与过滤条件合并

通常复杂的查询语句,我们也要配合过滤语句来实现缓存,用filter语句就可以来实现

例子:查询出喜欢旅行的,并且年龄是20岁的文档

GET /school/student/_search?pretty
{
  "query": {
   "bool": {
     "must": {"match": {"about": "travel"}},     
     "filter": [{"term":{"age": 20}}]
     }
  }
}
上一篇 下一篇