前言
Domain Specific Language
領域專用語言
Elasticsearch provides a full Query DSL based on JSON to define queries
- Elasticsearch 提供了基於 JSON 的 DSL 來定義查詢。
- DSL 由葉子查詢子句和複合查詢子句兩種子句組成。
- 本文適配 Elasticsearch 7.x 版本
- 推薦學習阮一鳴《Elasticsearch 核心技術與實戰》
- Elasticsearch 查詢層級圖
![ES查詢層級圖 ES查詢層級圖](http://static.javashuo.com/static/loading.gif)
Query 上下文和 Filter 上下文
葉子查詢子句
Leaf query clauses,葉子查詢子句
Full text queries,全文查詢
全文查詢關鍵字
intervals
match
match_bool_prefix
match_phrase
match_phrase_prefix
multi_match
query_string
simple_query_string
GET /_search
{
"query": {
"match": {
"message": {
"query": "this is a test"
}
}
}
}
term 級查詢
term 級查詢關鍵字
exists
fuzzy
ids
prefix
range
regexp
term
terms
terms_set
type
wildcard
GET my_index/_search?pretty
{
"query": {
"term": {
"full_text": "Quick Brown Foxes!"
}
}
}
GET _search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20,
"boost": 2
}
}
}
}
Compound query clauses,複合查詢子句
- 布爾查詢的子句類型有四種: must、filter、should、must_not
must
算分
返回的文檔必須知足 must 子句的條件
多個查詢條件的徹底匹配,至關於 AND
filter
不算分的 must
should
算分
在一個 bool 查詢中,若是沒有 must 或者 filter,
有一個或者多個should子句,那麼只要知足一個就能夠返回。
minimum_should_match 參數定義了至少知足幾個子句
至少有一個查詢條件匹配,至關於 OR
must_not
不算分
返回的文檔必須不知足 must_not 定義的條件
多個查詢條件的相反匹配,至關於 NOT
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter": {
"term" : { "tag" : "tech" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tag" : "wow" } },
{ "term" : { "tag" : "elasticsearch" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
GET /_search
{
"query": {
"boosting": {
"positive": {
"term": {
"text": "apple"
}
},
"negative": {
"term": {
"text": "pie tart fruit crumble tree"
}
},
"negative_boost": 0.5
}
}
}
GET /_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"user": "kimchy"
}
},
"boost": 1.2
}
}
}
GET /_search
{
"query": {
"dis_max": {
"queries": [
{
"term": {
"title": "Quick pets"
}
},
{
"term": {
"body": "Quick pets"
}
}
],
"tie_breaker": 0.7
}
}
}
GET /_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"boost": "5",
"random_score": {},
"boost_mode": "multiply"
}
}
}
本文出自
walker snapshot