elasticsearch7.6.x 整合springboot2(一)

一、RestFul風格說明

一種軟件架構風格,而不是標準,只是提供了一組設計原則和約束條件。它主要用於客戶端和服務器交
互類的軟件。基於這個風格設計的軟件可以更簡潔,更有層次,更易於實現緩存等機制。

PUT localhost:9200/索引名稱/類型名稱/文檔id 創建文檔(指定文檔id)

POST localhost:9200/索引名稱/類型名稱 創建文檔(隨機文檔id)

POST localhost:9200/索引名稱/類型名稱/文檔id/_update 修改文檔

DELETE localhost:9200/索引名稱/類型名稱/文檔id 刪除文檔

GET localhost:9200/索引名稱/類型名稱/文檔id 查詢文檔通過文檔id

POST localhost:9200/索引名稱/類型名稱/_search 查詢所有數據

二、索引的基本操作增刪改查

1、創建一個索引

類型名:將來就沒有了

PUT /索引名/類型名/文檔id
{請求體}

在kibana裏面插入

PUT /test1/type/1
{
 "name": "科比", 
 "age": "41"
}

在這裏插入圖片描述
在這裏插入圖片描述
如上圖 在head裏面看到科比這條數據就被插入進去了

2、用mapping 設置規則,規則裏面有properties屬性
test2: 類似數據庫中的表名
mappings:類似規則
properties : 類似屬性
name:類似字段名
type:類似字段屬性

PUT /test2
{
 "mappings": {
   "properties": {
     "name": {
       "type": "text"
     },
     "age": {
       "type": "long"
     },
     "birthday": {
       "type": "date"
     }
   }
 }
}

如圖創建規則
在這裏插入圖片描述
3、創建完規則以後獲取test2

GET test2

可以看到設置的索引信息在這裏插入圖片描述
4、再來創建一個test3

PUT /test3/_doc/1
{
  "name": "杜蘭特",
  "age": 13,
  "birth": "1997-01-05"
}

在這裏插入圖片描述
GET test3

在這裏插入圖片描述
如果自己的文檔字段沒有指定,那麼es就會自動默認配置字段類型
擴展:
通過命令elasticsearch索引情況
GET _cat/ 可以獲得es當前信息
GET _cat/health 查看健康狀況
GET _cat/indices 查看索引庫

5、修改

第一種方法:
提交試用PUT進行修改

PUT /test3/_doc/1
{
  "name": "杜蘭特科比",
  "age": 13,
  "birth": "1997-01-05"
}

在這裏插入圖片描述
可以看到updated已經修改成功了

第二種方法: POST修改
加上
_update的方式進行修改

POST /test3/_doc/1/_update
{
  "doc": {
    "name": "法外狂徒張三"
  }
}

6、刪除

刪除索引:

DELETE test1

在這裏插入圖片描述
刪除索引下面的文檔:

DELETE test3/_doc/1
在這裏插入圖片描述
在這裏插入圖片描述
如上圖 發現已經沒有數據了

三、索引的複雜查詢

1、增加後查詢只查詢一個屬性

PUT /test3/_doc/2
{
  "name": "科比",
  "age": 13,
  "birth": "1997-01-05"
}

GET /test3/_doc/_search
{
  "query": {
    "match": {
      "name": "科比"
    }
  }
}

在這裏插入圖片描述
如上圖:
hits :索引和文檔的信息 查詢的結果總數
然後就是查詢的具體文檔
數據中的東西都可以遍歷出來
total 意思是有2條
relation 關係是 equal 匹配
通過max_score來判斷誰更加符合結果

2、增加後查詢按條件查詢


GET /test3/_doc/_search
{
  "query": {
    "match": {
      "name": "科比"
    }
  },
  "_source": ["name","age"]
}

過濾
_source
字段
意思是隻看需要字段的結果

在這裏插入圖片描述
Java操作es 所有方法和對象就是這裏面的key

排序
sort
字段

asc desc對年齡進行排序

GET /test3/_doc/_search
{
  "query": {
    "match": {
      "name": "科比"
    }
  },
  "_source": ["name","age"],
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }]
}

分頁
from
size

from是從第幾頁開始
size是大小

GET /test3/_doc/_search
{
  "query": {
    "match": {
      "name": "科比"
    }
  },
  "_source": ["name","age"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }],
    
    "from": 0,
    "size": 2
}

數據下標還是從0開始
/search/{current}/{pagesize}

布爾查詢 返回 true or false
must 意思是所有條件都必須符合
should 意思是滿足一個條件就可以
must_not 意思是不是非必須 反向操作

GET /test3/_doc/_search

{
  "query": {
    "bool": {
      "must": [
        {
          "match":
          {
            "name": "杜蘭特"
          }
        },
        {
          "match":
          {
            "age": 3 
          }      
          }
        ]
      }
    }
  }

過濾器
gt 大於
gte大於等於
lt小於
lte小於等於

FIELD 可以替換爲自己想要查詢的字段

GET /test3/_doc/_search

{
  "query": {
    "bool": {
      "should": [
        {
          "match":
          {
            "name": "杜蘭特"
          }
        },
        {
          "match":
          {
            "age": 50
          }
           
          }
  
        ],
        "filter": {
          "range": {
            "FIELD": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      }
    }
  }

匹配多個條件


PUT /test3/user/1
{
  "name": "杜蘭特",
  "age": 35,
  "desc": "跳投無人能防",
  "tags": ["男人","技術","三分王"]
}

GET /test3/_search

{
  "query": {
    "term": {
      "name": "杜"
      
    }
    
  }
  }

term可以精確查詢

高亮查詢

搜索的結果相關

GET /test3/user/_search

{
  "query": {
    "match": {
      "name": "杜蘭特"
    }
  },
     "highlight": {
       "fields": { 
       "name": {}
     }
  }
  }

自定義高亮條件

GET /test3/user/_search

{
  "query": {
    "match": {
      "name": "杜蘭特"
    }
  },
     "highlight": {
       "pre_tags": "<p class='key' style='color:red'>",
       "post_tags": "</p>", 
       "fields": { 
       "name": {}
     }
  }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章