調試工具
Postman
RESTful API
有關 RESTful API的介紹 請查閱
RESTful API 在 Elasticsearch 中的應用
動作 | 解釋 |
---|---|
PUT | 更新或者新增文檔 |
GET | 檢索文檔 |
DELETE | 刪除文檔 |
HEAD | 校驗是否存在該文檔 (校驗狀態碼: 200 存在,404不存在) |
Elasticsearch 與 關系型數據庫的對應關系
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
元數據(Metadata)
身份元數據(Identity meta-fields)
身份元數據顧名思義就是能夠唯一標識Document的,Elasticsearch中主要有四個身份元數據
1、_index:文檔所屬的index,這個index相當于關系型數據庫中的數據庫概念,它是存儲和索引關聯數據的地方;
2、_uid:其由_type和_id組成;
3、_type:文檔所屬的mapping type,相當于關系型數據庫中的表的概念;
4、_id:文檔的id,這個可以由Elasticsearch自動生成,也可以在寫入Document的時候由程序指定。它與_index和_type組合時,就可以在Elasticsearch中唯一標識一個文檔。
文檔源元數據(Document source meta-fields)
文檔源元數據主要有兩個:
1、_source:
這個字段標識文檔的主體信息,也就是我們寫入在中的數據;
2、_size:
這個字段存儲著_source字段中信息的大小,單位是byte;不過這需要我們安裝mapper-size插件。
索引元數據(Indexing meta-fields)
1、_all:
這個字段索引了所有其他字段的值;
2、_field_names:
存儲著文檔中所有值為非空的字段信息,這在快速查找/過濾值存在或者值為空的情況下非常有用;
3、_timestamp:
存儲著當前文檔的時間戳信息,可以由程序指定,也可以由ElasticSearch自動生成,其值會影響文檔的刪除(如果啟用了TTL機制);
4、_ttl:
標識著當前文檔存儲的時長,超過了這個時長文檔將會被標識為delete,之后會被ElasticSearch刪除。
路由元數據(Routing meta-fields)
1、_parent:用于創建兩個映射的父子之間的關系;
2、_routing:自定義路由值,可以路由某個文檔到具體的分片(shard)。
其他元數據
_meta:特定于應用程序的元數據。
檢索單個文檔
地址: GET /megacorp/employee/1
GET請求
UR組成: index(庫名) / type(表名) / ID
請求結果
{
"_index": "megacorp", // 庫名
"_type": "employee", // 表名
"_id": "1", // id
"_version": 8, // 版本號
"found": true, // 是否找到該文檔
"_source": { // 存儲的原始文檔數據
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
輕量搜索
關鍵詞 【_search】
請求地址: GET /megacorp/employee/_search
請求數據:
{
"took": 2,
"timed_out": false,
// 分片信息 ? 具體位未知 ~~~
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
// 數據
"hits": {
// 總條數
"total": 3,
// 排名最高的分數值
"max_score": 1,
// 數據值
"hits": [
{
// 庫名
"_index": "megacorp",
// 表名
"_type": "employee",
// ID
"_id": "2",
// 排名分數
"_score": 1,
// 文檔數據
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": [
"forestry"
]
}
}
]
}
}