1. 參考文檔
https://www.elastic.co/guide/cn/elasticsearch/guide/current/aggregations.html
2. query
2.1. term
查詢,完全匹配,即不進行分詞器分析
{
"query": {
"term": {
"<field>": "<value>"
}
}
}
2.2. match
查詢,模糊匹配,根據你給定的字段進行分詞器分析,只包含一部分關鍵字就行
{
"query": {
"match": {
"<field>": "<value>"
}
}
}
2.3. match_all
查詢指定索引下的所有文檔
{
"query": {
"match_all": {}
}
}
通過 match_all
過濾出所有字段,然後通過 partial
再過濾出包含 preview
的字段和排除 title,price
的字段
{
"query": {
"match_all": {}
},
"partial_fields": {
"partial": {
"include": ["preview"],
"exclude": ["title,price"]
}
}
}
2.4. match_phrase
短語查詢,slop
定義的是關鍵詞之間隔多少個未知單詞
{
"query": {
"match_phrase": {
"query": "aaa,bbb",
"slop": 2
}
}
}
2.5. multi_match
查詢,可以指定多個字段
查詢 filed1
和 filed2
這兩個字段都包含 value
關鍵字的文檔
{
"query": {
"multi_match": {
"query": "<value>",
"fileds": ["<field1>", "<field2>"]
}
}
}
2.6. bool
布爾查詢
must
: 條件必須滿足,相當於sql
語句的and
should
: 條件可以滿足也可以不滿足,相當於sql
語句的or
must_not
: 條件不需要滿足,相當於sql
語句的not
{
"query": {
"bool": {
"should": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"must": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"must_not": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
}
}
}
2.7. filter
查詢同時,通過 filter
條件在不影響打分的情況下篩選出想要的數據
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"term": {
"<field>": "<value>"
}
}
}
}
}
filter
之 bool
過濾查詢
must
: 條件必須滿足,相當於sql
語句的and
should
: 條件可以滿足也可以不滿足,相當於sql
語句的or
must_not
: 條件不需要滿足,相當於sql
語句的not
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"bool": {
"should": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"must": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"must_not": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
}
}
}
}
}
沒有 bool
, 也可以直接使用 and、or、not
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"and": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"or": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
"not": [
{"term": {"<field>": "<value>"}},
{"term": {"<field>": "<value>"}}
],
}
}
}
}
filter
之 range
範圍查詢
gt
: 大於lt
: 小於gte
: 大於等於lte
: 小於等於
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"range": {
"<field>": {
"gt": "<value>",
"gte": "<value>",
"lt": "<value>",
"lte": "<value>",
}
}
}
}
}
}
2.8. boost
固定分數查詢 我們查詢到的每一個文檔都有一個_score
參數,這是匹配度打分
constant_score
: 固定分數查詢關鍵字(它支持filter
, 不支持match)
boost
: 指定固定分數字段
{
"query": {
"constant_score": {
"filter": {
"match": {
"<field>": "<value>"
}
},
"boost": 1
}
}
}
3. agg
聚合分析
3.1. terms
分組,對應 sql
語句中的 group by
{
"aggs": {
"<tag_name>": {
"terms": {
"field": "<value>"
}
}
}
}
3.2. cardinality
去重,對應 sql
語句中的 distinct
{
"aggs": {
"<tag_name>": {
"cardinality": {
"field": "<value>"
}
}
}
}
3.3. avg
求平均值
{
"aggs": {
"<tag_name>": {
"avg": {
"field": "<value>"
}
}
}
}
3.4. max
求平均值
{
"aggs": {
"<tag_name>": {
"max": {
"field": "<value>"
}
}
}
}
3.5. min
求平均值
{
"aggs": {
"<tag_name>": {
"min": {
"field": "<value>"
}
}
}
}
3.6. sum
求平均值
{
"aggs": {
"<tag_name>": {
"sum": {
"field": "<value>"
}
}
}
}
3.7. range
按照指定區間分組
{
"aggs": {
"<tag_name>": {
"field": "<value>",
"range": [
{"from": 0, "to": 20},
{"from": 20, "to": 40},
{"from": 40, "to": 60}
]
}
}
}
3.8. date_histogram
按時間統計
min_doc_count
: 強制返回所有buckets
,即使buckets
可能爲空extended_bounds
: 只返回你的數據中最小值和最大值之間的buckets
{
"aggs": {
"<tag_name>": {
"date_histogram": {
"<field>": "<value>",
"interval": "month",
"format": "yyyy-MM-dd",
"min_doc_count" : 0,
"extended_bounds" : {
"min" : "2014-01-01",
"max" : "2014-12-31"
}
}
}
}
}
4. collapse
使用collapse字段後,查詢結果中[hits]中會出現[fields]字段,其中包含了去重後的user_id
ES5.3版本之後才發佈的
聚合&摺疊只能針對keyword類型有效;