Filter DSL 過濾
term 過濾
term主要用于精確匹配哪些值,比如數(shù)字,日期,布爾值或 not_analyzed 的字符串(未經(jīng)分析的文本數(shù)據(jù)類型):
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
完整的例子, hostname 字段完全匹配成 saaap.wangpos.com 的數(shù)據(jù):
{
"query": {
"term": {
"hostname": "saaap.wangpos.com"
}
}
}
terms 過濾
terms 跟 term 有點(diǎn)類似,但 terms 允許指定多個匹配條件。 如果某個字段指定了多個值,那么文檔需要一起去做匹配
{
"terms": {
"tag": [ "search", "full_text", "nosql" ]
}
}
完整的例子,所有http的狀態(tài)是 302 、304 的, 由于ES中狀態(tài)是數(shù)字類型的字段,所有這里我們可以直接這么寫。:
{
"query": {
"terms": {
"status": [
304,
302
]
}
}
}
range 過濾
range過濾允許我們按照指定范圍查找一批數(shù)據(jù):
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
范圍操作符包含:
gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于
一個完整的例子, 請求頁面耗時大于1秒的數(shù)據(jù),upstream_response_time 是 nginx 日志中的耗時,ES中是數(shù)字類型。
{
"query": {
"range": {
"upstream_response_time": {
"gt": 1
}
}
}
}
exists 和 missing 過濾
exists 和 missing 過濾可以用于查找文檔中是否包含指定字段或沒有某個字段,類似于SQL語句中的IS_NULL條件
{
"exists": {
"field": "title"
}
}
這兩個過濾只是針對已經(jīng)查出一批數(shù)據(jù)來,但是想?yún)^(qū)分出某個字段是否存在的時候使用。
bool 過濾
bool 過濾可以用來合并多個過濾條件查詢結(jié)果的布爾邏輯,它包含一下操作符:
1.must :: 多個查詢條件的完全匹配,相當(dāng)于 and。
2.must_not :: 多個查詢條件的相反匹配,相當(dāng)于 not。
3.should :: 至少有一個查詢條件匹配, 相當(dāng)于 or。
這些參數(shù)可以分別繼承一個過濾條件或者一個過濾條件的數(shù)組:
{
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": { "term": { "tag": "spam" }},
"should": [
{ "term": { "starred": true }},
{ "term": { "unread": true }}
]
}
}
Query DSL 查詢
match_all 查詢
可以查詢到所有文檔,是沒有查詢條件下的默認(rèn)語句。
{
"match_all": {}
}
此查詢常用于合并過濾條件。 比如說你需要檢索所有的郵箱,所有的文檔相關(guān)性都是相同的,所以得到的_score為1.
match 查詢
match查詢是一個標(biāo)準(zhǔn)查詢,不管你需要全文本查詢還是精確查詢基本上都要用到它
如果你使用 match 查詢一個全文本字段,它會在真正查詢之前用分析器先分析match一下查詢字符:
{
"match": {
"tweet": "About Search"
}
}
如果用match下指定了一個確切值,在遇到數(shù)字,日期,布爾值或者not_analyzed 的字符串時,它將為你搜索你給定的值:
{ "match": { "age": 26 }}
{ "match": { "date": "2014-09-01" }}
{ "match": { "public": true }}
{ "match": { "tag": "full_text" }}
提示: 做精確匹配搜索時,你最好用過濾語句,因?yàn)檫^濾語句可以緩存數(shù)據(jù)。
match查詢只能就指定某個確切字段某個確切的值進(jìn)行搜索,而你要做的就是為它指定正確的字段名以避免語法錯誤。
multi_match 查詢
multi_match查詢允許你做match查詢的基礎(chǔ)上同時搜索多個字段,在多個字段中同時查一個:
{
"multi_match": {
"query": "full text search",
"fields": [ "title", "body" ]
}
}
bool 查詢
bool 查詢與 bool 過濾相似,用于合并多個查詢子句。不同的是,bool 過濾可以直接給出是否匹配成功, 而bool 查詢要計算每一個查詢子句的 _score (相關(guān)性分值)。
1.must:: 查詢指定文檔一定要被包含。
2.must_not:: 查詢指定文檔一定不要被包含。
3.should:: 查詢指定文檔,有則可以為文檔相關(guān)性加分。
以下查詢將會找到 title 字段中包含 "how to make millions",并且 "tag" 字段沒有被標(biāo)為 spam。 如果有標(biāo)識為 "starred" 或者發(fā)布日期為2014年之前,
那么這些匹配的文檔將比同類網(wǎng)站等級高:
以下查詢將會找到 title 字段中包含 "how to make millions",并且 "tag" 字段沒有被標(biāo)為 spam。 如果有標(biāo)識為 "starred" 或者發(fā)布日期為2014年之前,那么這些匹配的文檔將比同類網(wǎng)站等級高:
{
"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子句,那至少應(yīng)該有一個should子句。但是 如果有must子句,那么沒有should子句也可以進(jìn)行查詢。
上面內(nèi)容來自: http://es.xiaoleilu.com/054_Query_DSL/70_Important_clauses.html
ElasticSearch 查詢(match和term)
http://www.cnblogs.com/yjf512/p/4897294.html
wildcards 查詢
使用標(biāo)準(zhǔn)的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 查詢
假設(shè)您只想匹配以W開頭,緊跟著數(shù)字的郵政編碼。使用regexp查詢能夠讓你寫下更復(fù)雜的模式:
GET /my_index/address/_search
{
"query": {
"regexp": {
"postcode": "W[0-9].+"
}
}
}
這個正則表達(dá)式的規(guī)定了詞條需要以W開頭,緊跟著一個0到9的數(shù)字,然后是一個或者多個其它字符。
下面例子是所有以 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)
當(dāng)你需要尋找鄰近的幾個單詞時,你會使用match_phrase查詢
GET /my_index/my_type/_search
{
"query": {
"match_phrase": {
"title": "quick brown fox"
}
}
}
和match查詢類似,match_phrase查詢首先解析查詢字符串來產(chǎn)生一個詞條列表。然后會搜索所有的詞條,
但只保留含有了所有搜索詞條的文檔,并且詞條的位置要鄰接。一個針對短語quick fox的查詢不會匹配
我們的任何文檔,因?yàn)闆]有文檔含有鄰接在一起的quick和box詞條。
match_phrase查詢也可以寫成類型為phrase的match查詢:
"match": {
"title": {
"query": "quick brown fox",
"type": "phrase"
}
}