ES (ElasticSearch) 基礎入門01
資料
工具組合ES(檢索後臺)+ kibana(交互前端)
部署
參考官網部署
啓動
- 啓動ES
./bin/elasticsearch
#瀏覽器訪問
IP:9200
- 啓動kibana
./bin/kibana
#瀏覽器訪問
IP:5601/app/kibana
入門操作
基本操作方式
- 標準方式
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
- VERB: 適當的 HTTP 方法 或 謂詞 : GET
、
POST、
PUT、
HEAD 或者DELETE
。- PROTOCOL: http 或者 https
(如果你在 Elasticsearch 前面有一個
https 代理)- HOST: Elasticsearch 集羣中任意節點的主機名,或者用 localhost 代表本地機器上的節點。
- PORT: 運行 Elasticsearch HTTP 服務的端口號,默認是 9200 。
- PATH: API 的終端路徑(例如 _count 將返回集羣中文檔數量)。Path 可能包含多個組件,例如:_cluster/stats 和 _nodes/stats/jvm 。
- QUERY_STRING: 任意可選的查詢字符串參數 (例如 ?pretty 將格式化地輸出 JSON 返回值,使其更容易閱讀)
- BODY: 一個 JSON 格式的請求體 (如果請求需要的話)
curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
# 顯示信息頭 -i
curl -i -XGET 'localhost:9200/'
- 縮略方式
# 縮略請求(kibana中使用該方式)
GET /_count
{
"query": {
"match_all": {}
}
}
文檔操作
- 添加文檔
- 每個僱員索引一個文檔,包含該僱員的所有信息。
- 每個文檔都將是 employee 類型 。
- 該類型位於 索引 megacorp 內。
- 該索引保存在我們的 Elasticsearch 集羣中。
# megacorp:索引名稱
# employee:類型名稱
# 1:特定僱員的ID
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
- 檢索文檔
HTTP GET 請求並指定文檔的地址——索引庫、類型和ID。
GET /megacorp/employee/1
# 以下爲返回內容
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
- 刪除文檔
DELETE /megacorp/employee/1
搜索
- 簡易搜索(Query-string)
# 搜索所有僱員
GET /megacorp/employee/_search
# 查詢字符串 (_query-string_) 搜索
# 請求路徑中使用 _search 端點,並將查詢本身賦值給參數 q=
GET /megacorp/employee/_search?q=last_name:Smith
- 表達式搜索(查詢表達式)
領域特定語言 (DSL), 指定了使用一個 JSON 請求。我們可以像這樣重寫之前的查詢所有 Smith 的搜索
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
- 複雜搜索
關鍵字 | 意義 |
---|---|
bool | ??? |
must | ??? |
match | 匹配 |
filter | 過濾 |
range | 排序 |
gt | 大於 |
搜索姓氏爲 Smith 的僱員,但這次我們只需要年齡大於 30 的
GET /megacorp/employee/_search
{
"query":{
"bool":{
"must":{
"match":{
"last_name": "smith"
}
},
"filter":{
"range":{
"age":{"gt": 30}
}
}
}
}
}
- 全文搜索
搜索下所有喜歡攀巖(rock climbing)的僱員
# 請求
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
# 返回結果部分截取
{
...
"hits": {
"total": 2,
"max_score": 0.16273327,
"hits": [
{
...
"_score": 0.16273327,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
...
"_score": 0.016878016,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
- 搜索結果中包含相關性信息,上述結果中的_score字段就是相關性的指標。
- 這是完全區別於傳統關係型數據庫的一個概念,數據庫中的一條記錄要麼匹配要麼不匹配。
- 短語搜索
關鍵字 | 用途 |
---|---|
match_phrase | 匹配短語 |
用於精確匹配一系列單詞或者短語,如精確搜索相同形式和內容的“rock climbing” 匹配同時包含 “rock” 和 “climbing” ,並且 二者以短語 “rock climbing” 的形式緊挨着的僱員記錄
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
- 高亮搜索
關鍵字 | 用途 |
---|---|
hightlight | 高亮顯示 |
匹配同時包含 “rock” 和 “climbing” ,並且 二者以短語 “rock climbing” 的形式緊挨着的僱員記錄, 新的 highlight 參數
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
返回結果與之前一樣,與此同時結果中還多了一個叫做 highlight 的部分。這個部分包含了 about 屬性匹配的文本片段,並以 HTML 標籤 <em></em> 封裝:
聚合分析
關鍵字 | 說明 |
---|---|
aggs | 聚合 |
terms | ??? |
avg | 求均值 |
- 單獨聚合
挖掘出僱員中最受歡迎的興趣愛好
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
- 組合聚合
如果想知道叫 Smith 的僱員中最受歡迎的興趣愛好,可以直接添加適當的查詢來組合查詢
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
- 分級彙總
比如,查詢特定興趣愛好員工的平均年齡
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}