ElasticSearch 经常使用的查询过滤语句

Filter DSL

term 过滤

term主要用于精确匹配哪些值,好比数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型): html

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

完整的例子, hostname 字段彻底匹配成 saaap.wangpos.com 的数据:正则表达式


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

terms 过滤

terms 跟 term 有点相似,但 terms 容许指定多个匹配条件。 若是某个字段指定了多个值,那么文档须要一块儿去作匹配:shell


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

完整的例子,全部http的状态是 302 、304 的, 因为ES中状态是数字类型的字段,全部这里咱们能够直接这么写。:缓存


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

range 过滤

range过滤容许咱们按照指定范围查找一批数据:elasticsearch


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

范围操做符包含:

  • 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子句也能够进行查询。

若是must_not中要匹配多个词,内部须要增长must。

上面内容来自: 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
-----------------
感谢做者!
相关文章
相关标签/搜索