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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM