Elasticsearch的文檔、索引和rest api

基本概念

文檔(document)

  • elasticsearch 是面向文檔的,文檔是全部可搜索數據的最小單位html

    例如:數據庫

    • 日誌文件中的日誌項
    • 一部電影中的具體信息
    • 一首歌/一個PDF文檔的具體內容
  • 文檔會被序列化爲JSON格式,保存在Elasticsearch中json

    • JSON對象由字段組成
    • 每一個字段都有相應的字段類型(字符串/數值/布爾/日期/二機制/範圍類型)
  • 每一個文檔都有一個Unique IDapi

    • 能夠本身指定ID
    • 或者經過Elasticsearch自動生成

JSON文檔

  • 一篇文檔包含了一系列的字段,相似於數據庫中的一條記錄
  • JSON 文檔,格式靈活,不須要預先定義格式
    • 字段的類型能夠指定或者經過Elasticsearch自動推算
    • 支持數據/支持嵌套
movieId,title,genres
1,Toy Story(1995),AdvenTure|Animation|Children|Comedy|Fantasy
複製代碼
  • csv file經過es轉爲json
{
    "year" : 1995,
    "@version" : 1,
    "genres" : [
        "AdvenTure","Animation",
        "Children","Comedy","Fantasy"
    ],
    "id" : "1",
    "title" : "Tony Story"
}

複製代碼

文檔的元數據

{
    "_index" : "movies""_type" : "_doc""_id" : "1""_score" : "14.626""_source" : {
    	"year" : 1995,
    	"@version" : 1,
    	"genres" : [
        	"AdvenTure","Animation",
        	"Children","Comedy","Fantasy"
    	],
    	"id" : "1",
    	"title" : "Tony Story"
	}
}
複製代碼
  • 元數據,用於標註文檔的相關信息
    • _index : 文檔所屬的索引名
    • _type : 文檔所屬的類型名
    • _id : 文檔惟一ID
    • _source : 文檔的原始Json數據
    • _all : 整合全部字段到該字段(已被廢除)
    • _version : 文檔的版本信息
    • _score : 相關性打分

索引

{
    "movies" : {
        "settings" : {
            "index" : {
                "create_date" : "15526261177",
                "number_of_shards" : "2",
                "number_of_replicas" : "0",
                "uuid" : "",
                "verison" : {
                    "created" : "302302"
                },
                "provided_name" : "movies"
            }
        }
    }
}
複製代碼
  • Index-索引是文檔的容器,是一類文檔的結合
    • Index 體現了邏輯空間的概念:每一個索引都有本身的Mapping定義,用於定義包含的文檔的字段名和字段類型
    • Shard提現了物理空間的概念:索引中的數據分散在shard上
  • 索引的mapping 和Settings
    • Mapping 定義文檔字段的類型
    • Setting 定義不一樣的數據分佈

索引的不一樣語意

索引

  • 一個Elasticsearch 集羣中,能夠建立不少個不一樣的索引
  • 保存一個文檔到Elasticsearch的過程也叫索引(indexing)
    • es中,建立一個倒排索引的過程
  • 一個B樹索引,一個倒排索引

課程Demo

Index 相關 APIbash

#查看索引相關信息
GET kibana_sample_data_ecommerce

#查看索引的文檔總數
GET kibana_sample_data_ecommerce/_count

#查看前10條文檔,瞭解文檔格式
POST kibana_sample_data_ecommerce/_search
{
}

#_cat indices API
#查看indices
GET /_cat/indices/kibana*?v&s=index

#查看狀態爲綠的索引
GET /_cat/indices?v&health=green

#按照文檔個數排序
GET /_cat/indices?v&s=docs.count:desc

#查看具體的字段
GET /_cat/indices/kibana*?pri&v&h=health,index,pri,rep,docs.count,mt

#How much memory is used per index?
GET /_cat/indices?v&h=i,tm&s=tm:desc

複製代碼

Type

  • 7.0以前,一個index能夠設置多個types
  • 7.0以後,一個索引只能建立一個Type-"_doc"

抽象和類比

RDBMS Elasticsearch
Table Index(Type)
Row Document
Column Field
Schema Mapping
SQL DSL

傳統關係型數據庫和Elasticsearch的區別app

  • Elasticsearch - Schemaless /相關性/高性能全文檢索
  • RDMS - 事務性/Join

Rest API-被各類語言調用

一些基本的APIless

  • Indices
    • 建立Index
      • PUT movies
    • 查看全部Index
      • _cat/indices

部分操做

在kibana的 開發工具 中運行elasticsearch

// 查看索引相關信息
GET kibana_sample_data_ecommerce

// 查看索引的文檔總數
GET kibana_sample_data_ecommerce/_count

// 查看前10條文檔,瞭解文檔格式
POST kibana_sample_data_ecommerce/_search
{
  
}

//_cat indeices API
// 查看indices
GET /_cat/indices/kibana*?v&s=index

// 查看狀態爲綠的索引
GET /_cat/indices?v&health=green

// 查看文檔個數排序
GET /_cat/indices?v&s=docs.count:desc

// 查看具體的字段
GET /_cat/indices/kibana*?pri&h=health,index,pri,rep,docs.count,mt

// 每一個索引所佔的內存空間
GET /_cat/indices?v&h=i,tm&s=tm:desc

複製代碼

相關閱讀

相關文章
相關標籤/搜索