第一章 概述-無
第二章 開始使用
2.1 基本檢索
2.1.1 計算文檔數量
GET books/_count
2.1.2 檢索單個ID文檔
GET books/_doc/1
2.1.3 檢索多個ID文檔
GET books/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
2.1.4 檢索所有文檔
GET books/_search
2.1.5 抑制元數據: "_source": false
-
寫法1
GET books/_search { "_source": false, "query": { "ids": { "values": [1,2,3] } } } -
寫法2
GET books/_search?_source=false { "query": { "ids": { "values": [1,2,3] } } }2.1.6 多字段搜索: multi_match
GET books/_search { "query": { "multi_match": { "query": "java", "fields": ["title", "author"] } } }2.1.7 提升結果分數:"title^2"
GET books/_search { "query": { "multi_match": { "query": "java", "fields": ["title^2", "author"] } } }2.1.8 短語搜索: match_phrase
2.1.9 高亮結果:highlight>fields>列名
GET books/_search { "query": { "multi_match": { "query": "java", "fields": ["title^2", "author"] } }, "highlight": { "fields": { "title": {} } } }2.1.10 詞項:term
GET books/_search { "query": { "term": { "title.keyword": { "value": "Effective Java" } } } }2.1.11 範圍查詢:range
GET books/_search { "query": { "range": { "amazon_rating": { "gte": 1.0, "lte": 2.0 } } } }2.2 bool查詢(複合查詢)
複合查詢有以下幾種:
- bool查詢->最常用
- constant_score常數分查詢
- function_score函數分查詢
- boosting查詢
- dis_max查詢(分離最大化)
bool查詢可以使用4種子句
- must
- must_not
- filter 只過濾,不參與分數計算
- should 不要求強制匹配,可以配合 minimum_should_match最少要滿足的條件數
2.3 聚合
聚合分類:
- 指標聚合:metric aggregation
- 桶聚合: bucket aggregation
- 管道聚合: pipe aggregation: 其他聚合結果進行聚合
2.3.1 指標聚合
count/sum/min/max/avg
GET books/_search
{
"size": 0,
"aggs": {
"amazing_rating_stat": {
"stats": {
"field": "amazon_rating"
}
}
}
}
result:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"amazing_rating_stat": {
"count": 7,
"min": 1.7000000476837158,
"max": 4.800000190734863,
"avg": 3.357142857142857,
"sum": 23.5
}
}
}
2.3.2 bucket聚合-1-histogram
數據:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_id": "1",
"_source": {
"amazon_rating": 4.7
}
},
{
"_id": "2",
"_source": {
"amazon_rating": 4.8
}
},
{
"_id": "3",
"_source": {
"amazon_rating": 4.2
}
},
{
"_id": "z4vu5ZgBKp_2wfj9FsRL",
"_source": {
"amazon_rating": 1.7
}
},
{
"_id": "0Ivu5ZgBKp_2wfj96cSF",
"_source": {
"amazon_rating": 2.7
}
},
{
"_id": "0Yvv5ZgBKp_2wfj9ysT3",
"_source": {
"amazon_rating": 2.7
}
},
{
"_id": "0ovw5ZgBKp_2wfj9IMQW",
"_source": {
"amazon_rating": 2.7
}
}
]
}
}
聚合dsl查詢
GET books/_search
{
"size": 0,
"aggs": {
"amazing_rating_histogram": {
"histogram": {
"field": "amazon_rating",
"interval": 2
}
}
}
}
結果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"amazing_rating_histogram": {
"buckets": [
{
"key": 0,
"doc_count": 1
},
{
"key": 2,
"doc_count": 3
},
{
"key": 4,
"doc_count": 3
}
]
}
}
}
2.3.3 bucket聚合-2-range bucket
GET books/_search
{
"size": 0,
"aggs": {
"amazing_rating_range": {
"range": {
"field": "amazon_rating",
"ranges": [
{
"to": 2
},
{
"from": 2,
"to": 3
},
{
"from": 3,
"to": 5
}
]
}
}
}
}
結果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"amazing_rating_range": {
"buckets": [
{
"key": "*-2.0",
"to": 2,
"doc_count": 1
},
{
"key": "2.0-3.0",
"from": 2,
"to": 3,
"doc_count": 3
},
{
"key": "3.0-5.0",
"from": 3,
"to": 5,
"doc_count": 3
}
]
}
}
}