ElasticSearch 常用的查詢過濾語句


query 和  filter 的區別請看: http://www.cnblogs.com/ghj1976/p/5292740.html 

 

Filter DSL

 

term 過濾

term主要用於精確匹配哪些值,比如數字,日期,布爾值或 not_analyzed 的字符串(未經分析的文本數據類型):

{ "term": { "age":    26           }}
{ "term": { "date":   "2014-09-01" }}
{ "term": { "public": true         }}
{ "term": { "tag":    "full_text"  }}

完整的例子, hostname 字段完全匹配成 saaap.wangpos.com 的數據:

{
  "query": {
    "term": {
      "hostname": "saaap.wangpos.com"
    }
  }
}

 

terms 過濾

terms 跟 term 有點類似,但 terms 允許指定多個匹配條件。 如果某個字段指定了多個值,那么文檔需要一起去做匹配:

{
    "terms": {
        "tag": [ "search", "full_text", "nosql" ]
        }
}

完整的例子,所有http的狀態是 302 、304 的, 由於ES中狀態是數字類型的字段,所有這里我們可以直接這么寫。:

{
  "query": {
    "terms": {
      "status": [
        304,
        302
      ]
    }
  }
}

 

range 過濾

range過濾允許我們按照指定范圍查找一批數據:

{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}

范圍操作符包含:

  • gt :: 大於
  • gte:: 大於等於
  • lt :: 小於
  • lte:: 小於等於

一個完整的例子, 請求頁面耗時大於1秒的數據,upstream_response_time 是 nginx 日志中的耗時,ES中是數字類型。

{
  "query": {
    "range": {
      "upstream_response_time": {
        "gt": 1
      }
    }
  }
}

 

exists 和 missing 過濾

exists 和 missing 過濾可以用於查找文檔中是否包含指定字段或沒有某個字段,類似於SQL語句中的IS_NULL條件.

{
    "exists":   {
        "field":    "title"
    }
}

這兩個過濾只是針對已經查出一批數據來,但是想區分出某個字段是否存在的時候使用。

 

bool 過濾

bool 過濾可以用來合並多個過濾條件查詢結果的布爾邏輯,它包含一下操作符:

  • must :: 多個查詢條件的完全匹配,相當於 and。
  • must_not :: 多個查詢條件的相反匹配,相當於 not。
  • should :: 至少有一個查詢條件匹配, 相當於 or。

這些參數可以分別繼承一個過濾條件或者一個過濾條件的數組:

{
    "bool": {
        "must":     { "term": { "folder": "inbox" }},
        "must_not": { "term": { "tag":    "spam"  }},
        "should": [
                    { "term": { "starred": true   }},
                    { "term": { "unread":  true   }}
        ]
    }
}

 

Query DSL

 

match_all 查詢

可以查詢到所有文檔,是沒有查詢條件下的默認語句。

{
    "match_all": {}
}

此查詢常用於合並過濾條件。 比如說你需要檢索所有的郵箱,所有的文檔相關性都是相同的,所以得到的_score為1.

 

match 查詢

match查詢是一個標准查詢,不管你需要全文本查詢還是精確查詢基本上都要用到它。

如果你使用 match 查詢一個全文本字段,它會在真正查詢之前用分析器先分析match一下查詢字符:

{
    "match": {
        "tweet": "About Search"
    }
}

如果用match下指定了一個確切值,在遇到數字,日期,布爾值或者not_analyzed 的字符串時,它將為你搜索你給定的值:

{ "match": { "age":    26           }}
{ "match": { "date":   "2014-09-01" }}
{ "match": { "public": true         }}
{ "match": { "tag":    "full_text"  }}

提示: 做精確匹配搜索時,你最好用過濾語句,因為過濾語句可以緩存數據。

match查詢只能就指定某個確切字段某個確切的值進行搜索,而你要做的就是為它指定正確的字段名以避免語法錯誤。

 

multi_match 查詢

multi_match查詢允許你做match查詢的基礎上同時搜索多個字段,在多個字段中同時查一個:

{
    "multi_match": {
        "query":    "full text search",
        "fields":   [ "title", "body" ]
    }
}

 

bool 查詢

bool 查詢與 bool 過濾相似,用於合並多個查詢子句。不同的是,bool 過濾可以直接給出是否匹配成功, 而bool 查詢要計算每一個查詢子句的 _score (相關性分值)。

  • must:: 查詢指定文檔一定要被包含。
  • must_not:: 查詢指定文檔一定不要被包含。
  • should:: 查詢指定文檔,有則可以為文檔相關性加分。

以下查詢將會找到 title 字段中包含 "how to make millions",並且 "tag" 字段沒有被標為 spam。 如果有標識為 "starred" 或者發布日期為2014年之前,那么這些匹配的文檔將比同類網站等級高:

{
    "bool": {
        "must":     { "match": { "title": "how to make millions" }},
        "must_not": { "match": { "tag":   "spam" }},
        "should": [
            { "match": { "tag": "starred" }},
            { "range": { "date": { "gte": "2014-01-01" }}}
        ]
    }
}

提示: 如果bool 查詢下沒有must子句,那至少應該有一個should子句。但是 如果有must子句,那么沒有should子句也可以進行查詢。

上面內容來自: http://es.xiaoleilu.com/054_Query_DSL/70_Important_clauses.html 

 

ElasticSearch 查詢(match和term)
http://www.cnblogs.com/yjf512/p/4897294.html

wildcards 查詢

使用標准的shell通配符查詢

參考: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

以下查詢能夠匹配包含W1F 7HW和W2F 8HW的文檔:

GET /my_index/address/_search
{
    "query": {
        "wildcard": {
            "postcode": "W?F*HW"
        }
    }
}

又比如下面查詢 hostname 匹配下面shell通配符的:

{
  "query": {
    "wildcard": {
      "hostname": "wxopen*"
    }
  }
}

regexp 查詢

假設您只想匹配以W開頭,緊跟着數字的郵政編碼。使用regexp查詢能夠讓你寫下更復雜的模式:

GET /my_index/address/_search
{
    "query": {
        "regexp": {
            "postcode": "W[0-9].+"
        }
    }
}

這個正則表達式的規定了詞條需要以W開頭,緊跟着一個0到9的數字,然后是一個或者多個其它字符。

下面例子是所有以 wxopen 開頭的正則

{
  "query": {
    "regexp": {
      "hostname": "wxopen.*"
    }
  }
}

參考: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html

prefix 查詢

以什么字符開頭的,可以更簡單地用 prefix,如下面的例子:

{
  "query": {
    "prefix": {
      "hostname": "wxopen"
    }
  }
}

參考 : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html 

 

更多的查詢命令,可以看: https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html#term-level-queries

 

短語匹配(Phrase Matching)

當你需要尋找鄰近的幾個單詞時,你會使用match_phrase查詢:

GET /my_index/my_type/_search
{
    "query": {
        "match_phrase": {
            "title": "quick brown fox"
        }
    }
}
和match查詢類似,match_phrase查詢首先解析查詢字符串來產生一個詞條列表。然后會搜索所有的詞條,
但只保留含有了所有搜索詞條的文檔,並且詞條的位置要鄰接。一個針對短語quick fox的查詢不會匹配
我們的任何文檔,因為沒有文檔含有鄰接在一起的quick和box詞條。

match_phrase查詢也可以寫成類型為phrase的match查詢:

"match": {
    "title": {
        "query": "quick brown fox",
        "type":  "phrase"
    }
}
參考: http://blog.csdn.net/dm_vincent/article/details/41941659
 


免責聲明!

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



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