elasticsearch(4)查詢DSL
阿新 • • 發佈:2019-01-10
全部搜尋
GET /index/type/_search
{
"query": {
"match_all": {}
}
}
例如:
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
}
}
分頁搜尋
GET /index/type/_search
{
"query": {
xxx
},
"from": 0,
"size": 10
}
例如:
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"from" : 0,
"size": 10
}
搜尋返回指定欄位
GET /ecommerce/product/_search
{
"query": {
xxx
},
"_source": ["key1","key2"]
}
例如:
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"]
}
指定欄位分詞搜尋
GET /index/type/_search
{
"query": {
"match": {
"key1" : "value1"
}
}
}
例如:
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "牙膏"
}
}
}
查詢排序
GET /index/type/_search
{
"query": {
xxx
},
"sort": [
{
"key1": {
"order": "asc|desc"
}
}
]
}
例如:
GET /ecommerce/product/_search
{
"query" : {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
查詢filter
filter不涉及積分計算,因此filter的效率比普通查詢的效率要高。
例如:
GET /ecommerce/product/_search
{
"query": {
"bool": {
"must": {
"match":{
"name":"牙膏"
}
},
"filter": {
"range": {
"price": {
"gt": 10,
"lte": 30
}
}
}
}
}
}
查詢phrase
跟match全文檢索相反,全文檢索會將搜尋輸入分詞拆解,去倒排索引中一一匹配,只要有一個分詞能在倒排索引中匹配上,就可以作為結果返回。
phrase不會將搜尋輸入分詞,必須在指定欄位值中完全包含搜尋輸入才算搜尋匹配,作為結果返回。
例如:在match全文檢索模式下,高露潔
可以匹配上高露潔牙膏
和佳潔士牙膏
;在phrase模式下,可以匹配上高露潔牙膏
,但匹配不上佳潔士牙膏
。
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"name": "高露潔"
}
}
}
查詢高亮
GET /index/type/_search
{
"query": {
xxx
},
"highlight": {
"fields": {
"key1": {}
}
}
}
例如:
requset
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "高露潔"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
response
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.8630463,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.8630463,
"_source": {
"name": "高露潔牙膏",
"price": 20
},
"highlight": {
"name": [
"<em>高</em><em>露</em><em>潔</em>牙膏"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "佳潔士牙膏",
"price": 30
},
"highlight": {
"name": [
"佳<em>潔</em>士牙膏"
]
}
}
]
}
}