查詢結果解釋
- took - 耗費多少毫秒
- time_out - 是否超時
- _shards - 數據分片情況
- hits.total - 查詢結果的數量
- hits.max_score - document 對於一個 search 的相關度匹配分數,越相關分數越高
- hits.hits - 匹配 document 的詳細數據
query string search
- 語法
GET /index/type/_search
查詢全部商品
GET /ecommerce/product/_search
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
...
]
}
}
條件查詢排序
查詢商品名稱中包含
yagao
的商品,而且按照售價降序排序
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
query DSL 【常用】
Domain Specified Language:特定領域的語言
- 語法
GET /index/type/_search
{
"query": {
"match_all": {}
}
}
請求體放在
request body
裏面 ,可以用 json 格式來構建各種複雜的語法,比query string search
強大
查詢所有商品
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
}
}
條件查詢排序
查詢商品名稱中包含
yagao
的商品,而且按照售價降序排序
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"sort": [
{
"price": "desc"
}
]
}
分頁查詢
當前總共3條商品數據,假設每頁顯示1條,現在顯示第2頁,所以只查出來第2個商品
GET /ecommerce/product/_search
{
"query":{
"match_all": {}
},
"from": 1,
"size": 1
}
查詢指定屬性
指定只查詢商品的 name、price 屬性
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": [
"name",
"price"
]
}
query filter
範圍查詢
搜索商品名稱包含 yaogao ,而且售價大於 25 元的商品
GET /ecommerce/product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yagao"
}
}
],
"filter": {
"range": {
"price": {
"gt": 25
}
}
}
}
}
}
- gt : 大於
- lt : 小於
- eq : 等於
- ne : 不等於
- gte : 大於等於
- lte : 小於等於
full-text search
全文檢索 - 會將輸入的
關鍵字
拆解,去倒排索引裏面一一匹配,只要匹配上任意一個拆解後的單詞,就可以作爲結果返回
- 先新增一條數據
PUT /ecommerce/product/4
{
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tag": [
"meibai"
]
}
- 查詢 producer
GET /ecommerce/product/_search
{
"query":{
"match": {
"producer":"yagao producer"
}
}
}
- 查詢結果展示
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.2825179,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 1.2825179,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tag": [
"meibai"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.09037233,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.09037233,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.09037233,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"name"
]
}
}
]
}
}
當
yagao producer
作爲查詢條件時會被拆分成yagao
、producer
兩個查詢條件,但是查詢結果的_score
會體現跟原始查詢條件的匹配度
phrase search
短語搜索 - 跟全文檢索相反,搜索
關鍵字
必須在指定的字段文本中完全匹配
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
highlight search
高亮搜索 - 被匹配的關鍵字將會在查詢結果中高亮標註(默認
<em>
)
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "producer"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}