kibana花式查询

在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" }

3.4.1、使用match_all做查询

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

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

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

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

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

【这种方式是错误的,因为一个match下,不能出现多个字段值[match] query doesn't support multiple fields】,需要使用复合查询

 

3.4.3、bool的复合查询

当出现多个查询语句组合的时候,可以用bool来包含。bool合并聚包含:must,must_not或者should, should表示or的意思

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

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

3.4.4、bool的复合查询中的should

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

例子:

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

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

3.4.5、term匹配

使用term进行精确匹配(比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型))

语法

{ "term": { "age": 20 }}

{ "term": { "date": "2018-04-01" }}

{ "term": { "sex": “boy” }}

{ "term": { "about": "trivel" }}

例子:

查询喜欢旅行的

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

3.4.6、使用terms匹配多个值

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

term主要是用于精确的过滤比如说:”我爱你”

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

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

3.4.7、Range过滤

Range过滤允许我们按照指定的范围查找一些数据:操作范围:gt::大于,gae::大于等于,lt::小于,lte::小于等于

例子:

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

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

3.4.8、exists和 missing过滤

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

例子:

查找字段中包含age的文档

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

3.4.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
        }
      }}
    ]
  }
}
}

3.4.10、查询与过滤条件合并

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

例子:

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

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

 

posted @ 2017-05-22 22:00  niutao  阅读(3755)  评论(0编辑  收藏  举报