elk學習筆記-es-文檔及索引操作

常見術語

文檔 Document

  • 用戶存儲在es中的數據文檔,相當於mysql數據表中的一行數據
索引 index
  • 由具有相同字段的文檔列表組成,相當於mysql數據庫中的表,table
節點 Node
  • 一個elasticsearch 的運行實例,是集羣的構成單元。
集羣 Cluster
  • 由一個或多個節點組成,對外提供服務
Document

Json Object,有字段(field)組成,常見數據類型如下:

  • 字符串:text,keyword
  • 數值型:long,integer,short,byte,double,float,scaled_float
  • 布爾:Boolean
  • 日期:date
  • 二進制:binary
  • 範圍類型:integer_range,float_range,long_range,double_range,date_range

每個文檔都有一個唯一的ID標識

  • 自行指定
  • es 自動生成

元數據(MetaData),用於標註文檔相關信息

  • _index:文檔所在索引名
  • _type:文檔所在的類型名
  • _id:文檔唯一id
  • _uid:組合id,由_type和_id組成(6.x _type不再起作用,同_id一樣)
  • _source:文檔的原始Json數據,可以從這裏獲取每個字段的內容
  • _all:整合所有字段內容到該字段,默認禁用

正排索引

  • 文檔ID到文檔內容,單詞的關聯關係

elk學習筆記-es-文檔及索引操作

倒排索引
  • 單詞到文檔ID的關聯關係
    elk學習筆記-es-文檔及索引操作

    倒排索引-查詢流程

  • 通過倒排索引獲得"搜索引擎"對應的文檔Id有1和3
  • 通過正排索引查詢1和3的完整內容
  • 返回用戶最終結果

倒排索引-單詞詞典

單詞詞典(Term Dictionary)是倒排索引的重要組成

  • 記錄所有文檔的單詞,一般都比較大
  • 記錄單詞倒排列表的關聯信息

倒排索引-倒排列表

倒排列表( Posting List )記錄了單詞對應的文檔集合,由倒排索引項( Posting )組成

倒排索引項( Posting )主要包含如下信息:

  • 文檔Id ,用於獲取原始信息
  • 單詞頻率( TF, Term Frequency) , 記錄該單詞在該文檔中的出現次數,用於後續相關性算分
  • 位置( Position) ,記錄單詞在文檔中的分詞位置(多個) , 用於做詞語搜索
  • 偏移(Offset),記錄單詞在文檔的開始和結束位置,用於做高亮顯示

Index

索引中存儲具有相同結構的文檔(Document)

  • 每個索引都有自己的mapping 定義,用於定義字段名和類型

一個集羣可以有多個索引,比如:

  • nginx 日誌存儲的時候可以按照日期每天生成一個索引來存儲
    nginx-log-2019-01-01
    nginx-log-2019-01-02
    nginx-log-2019-01-03

創建索引與寫入數據

Rest API ()

Elasticsearch 集羣對外提供RESTful API

  • REST(REpresentational State Transfer)表現層狀態轉移(對資源進行操作狀態會發生變化)
  • URL指定資源,如Index,Document
  • Http Method 指定資源操作類型,如GET,POST,PUT,DELETE
兩種交互方式
  • Curl 命令行
    elk學習筆記-es-文檔及索引操作
  • Kibana DevTools

elk學習筆記-es-文檔及索引操作

索引 API

es有專門的Index API,用於創建,更新,刪除索引配置等

  • 創建索引API
PUT /test_index

elk學習筆記-es-文檔及索引操作

  • 查看現有索引
    GET_cat/indices

    elk學習筆記-es-文檔及索引操作

    文檔 Document API

es有專門的 Document API

  • 創建文檔
  • 查詢文檔
  • 更新文檔
  • 刪除文檔

創建文檔:

指定文檔ID創建文檔:

PUT /test_index/doc/1
{
  "username":"kibana",
  "version":6.1
}

elk學習筆記-es-文檔及索引操作

不指定ID創建文檔:
POST /test1_index/doc
{
  "username":"kibana",
  "version":6.1
}

elk學習筆記-es-文檔及索引操作

查詢文檔

  • 指定要查詢文檔的ID
    GET /test_index/doc/1  

    elk學習筆記-es-文檔及索引操作

  • 搜索所有文檔:_search
    GET /test_index/doc/_search  //不指定條件查找
    GET /test_index/doc/_search
    {
    "query": {
    "term":{
      "_id":"1" //指定條件,查找ID爲1的文檔
    }
    }
    }

    elk學習筆記-es-文檔及索引操作

批量創建文檔API

es允許一次操作多個文檔(增刪改查,create創建文檔,如果文檔已經存在就會報錯。index創建文檔,如果存在就會覆蓋。)

  • endpoint 爲 _bulk,如下:
    POST _bulk
    {"index":{"_index":"test_index","_type":"doc","_id":"3"}}
    {"username":"zabbix","version":4}
    {"delete":{"_index":"test_index","_type":"doc","_id":"1"}}
    {"update":{"_id":"4","_index":"test_index","_type":"doc"}}
    {"doc":{"es":"5.0"}}

    輸出:

    {
    "took": 979, //查詢耗時,單位ms
    "errors": false,    //返回結果,正確或錯誤
    "items": [   //每個操作返回的結果
    {
      "index": {
        "_index": "test_index",
        "_type": "doc",
        "_id": "3",
        "_version": 1,
        "result": "created",    //創建
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "delete": {
        "_index": "test_index",
        "_type": "doc",
        "_id": "1",
        "_version": 2,
        "result": "deleted",    //刪除
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "update": {
        "_index": "test_index",
        "_type": "doc",
        "_id": "4",
        "_version": 2,
        "result": "updated",    //更改
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 1,
        "status": 200
      }
    }
    ]
    }

    批量查詢文檔

  • endpoint 爲_mget,如下:(並且可以在一個_mget下獲取不同索引的文檔)
GET /_mget    //查找在test_index索引,id爲4和1的文檔.
{
  "docs":[    //指明要查詢的文檔id
    {
      "_index":"test_index",
      "_type":"doc",
      "_id":"4"
    },
    {
      "_index":"test_index",
      "_type":"doc",
      "_id":"2"
    }
  ]
}
返回
{
  "docs": [
    {
      "_index": "test_index",
      "_type": "doc",
      "_id": "4",
      "_version": 2,
      "found": true,
      "_source": {
        "username": "es",
        "version": 6.1,
        "es": "5.0"
      }
    },
    {
      "_index": "test_index",
      "_type": "doc",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "username": "zabbix",
        "version": 4.2
      }
    }
  ]
}

歡迎加入
elk學習筆記-es-文檔及索引操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章