文檔:https://learnku.com/docs/elasticsearch73/7.3/data-in-documents-and-indices/6446
查詢全部
{
"query": {
"match_all": {}
}
}
- 查詢結果說明
- took:耗費了幾毫秒
- timed_out:是否超時,這里是沒有
- _shards:數據拆成了5個分片,所以對於搜索請求,會打到所有的primary shard
- hits.total:查詢結果的數量,多少個document
- hits.max_score: score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高
- hits.hits:包含了匹配搜索的document的詳細數據
單個條件查詢
## 方法1
GET goods/fruit/_search/?q=name:xiangjiao ## 查詢名字為xiangjiao的
## 方法2
GET goods/fruit/_search
{
"query": {
"match": {
"name": "xiangjiao"
}
}
}
多個條件查詢
{
"query": {
"bool": {
"must": [
{
"match": {
"taskid": "123"
}
},
{
"match": {
"time": "2021-05-20"
}
}
]
}
}
}
按條件查詢和排序
- 查詢 name=pingguo 使用價格進行正向排序
GET goods/fruit/_search
{
"query": {
"match": {
"name": "pingguo"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
分頁查詢
- 注意: 這里的檢索結果是倒排索引,不是按照id排序的,是按照倒排的方式來進行檢索的,再強調下,不是根據id排序
- 每頁顯示兩條數據
GET /goods/fruit/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
只顯示指定字段
- 檢索出來的內容也就只包含了name和price字段的內容
GET /goods/fruit/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"],
"from": 0,
"size": 2
}
多條件匹配查詢
- 查詢 name=xiangjiao 並且 price=25
GET /goods/fruit/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "xiangjiao"
}
},
{
"match": {
"price": "25"
}
}
]
}
}
}
- 查詢 name=xiangjiao 或者 price=45
GET /goods/fruit/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "xiangjiao"
}
},
{
"match": {
"price": "45"
}
}
]
}
}
}
過濾查詢
- 注意:filter於must/must_not/should是並列關系,同屬於bool的子屬性
- lt:小於, lte:小於等於, gt:大於, gte:大於等於
- 查詢
name=xiangjiao 或者 價格在 10~40
之間的水果
GET /goods/fruit/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "xiangjiao"
}
}
],
"filter": {
"range": {
"price": {
"gte": 10,
"lte": 40
}
}
}
}
}
}
全文檢索
- 查詢 name=pingguo 或者 name=xiangjiao的
GET /goods/fruit/_search
{
"query": {
"match": {
"name": "pingguo xiangjiao"
}
}
}