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.一、使用match_all作查询

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

问题:经过match_all匹配后,会把全部的数据检索出来,可是每每真正的业务需求并不是要找所有的数据,而是检索出本身想要的;而且对于es集群来讲,直接检索所有的数据,很容易形成GC现象。因此,咱们要学会如何进行高效的检索数据spa

3.4.二、经过关键字段进行查询

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

若是此时想查询喜欢旅游的,而且不能是男孩的,怎么办?ip

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

 

3.4.三、bool的复合查询

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

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

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

3.4.四、bool的复合查询中的should

should表示无关紧要的(若是should匹配到了就展现,不然就不展现)class

例子:集群

查询喜欢旅行的,若是有男性的则显示,不然不显示date

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

3.4.五、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.六、使用terms匹配多个值

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

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

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

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

3.4.七、Range过滤

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

例子:

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

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

3.4.八、exists和 missing过滤

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

例子:

查找字段中包含age的文档

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

3.4.九、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.十、查询与过滤条件合并

一般复杂的查询语句,咱们也要配合过滤语句来实现缓存,用filter语句就能够来实现

例子:

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

GET /school/student/_search?pretty
{
"query": {
  "bool": {
    "must": {"match": {"about": "travel"}},    
    "filter": [{"term":{"age": 20}}]
    }
}
}
相关文章
相关标签/搜索