ElasticSearch入門簡介
ElasticSearch是基於Apache Lucene的分布式搜索引擎, 提供面向文檔的搜索服務。本文以6.2.3版本為例介紹ElasticSearch的應用。
本文首先介紹ElasticSearch中的索引和文檔的概念,並在系列的其它文章進行更進一步的介紹。
目錄:
- 創建文檔
- 訪問文檔
- 更新文檔
- 刪除文檔
可以在官網下載壓縮包, 在解壓目錄中執行bin/elasticsearch
來啟動服務, 或者使用包管理器來安裝啟動.
ES默認端口為9200, 本地啟動ES後向http://localhost:9200
發送GET請求可以查看ES的基本信息:
GET ‘localhost:9200‘ { "name" : "hiTUe19", "cluster_name" : "elasticsearch_finley", "cluster_uuid" : "cfKnyFL1Rx6URmrmAuMBFw", "version" : { "number" : "5.1.2", "build_hash" : "c8c4c16", "build_date" : "2017-01-11T20:18:39.146Z", "build_snapshot" : false, "lucene_version" : "6.3.0" }, "tagline" : "You Know, for Search" }
ElasticSearch采用三層數據結構來管理數據:
- 索引(
index
): 索引是最高層的數據結構,可以定義獨立的搜索索引和分片存儲策略 - 類型(
type
): 每個index可以擁有多個type, 用於存儲不同類型的文檔 - 文檔:文檔是最基本的數據結構,存儲和搜索都是圍繞文檔展開的
ElasticSearch中的文檔是一個Json對象,搜索的結果是文檔的集合因此被稱為面向文檔的搜索。
與三層數據結構相對應,我們可以使用三個字段來唯一標識文檔:
_index
: 代表文檔所在的索引。索引名必須小寫, 不能以下劃線開頭, 不能包含逗號._type
: 代表文檔所在的類型集。type名可以為大小寫, 不能以下劃線開頭, 不能包含逗號._id
: 用於唯一標識某個type中的文檔
空查詢請求可以列出某個數據結構中所有文檔:
- 列出所有文檔:
GET /_search
- 列出索引
blog
下的所有文檔:GET /blog/_search
- 列出類型
/blog/user
下的所有文檔:GET /blog/user/_search
創建文檔
IndexAPI
可以用於創建文檔:
$ POST ‘localhost:9200/blog/user/‘ content-type: application/json body: { "id": 1, "nickname": "finley" } response: { "_index": "blog", "_type": "user", "_id": "AV5WoO0MdsHuOObNBTWU", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
使用POST請求創建文檔, 在url中指定_index
和_type
, 在請求體中使用json格式提交文檔數據。
若_index
或_type
不存在ES會自動創建。上述請求中文檔的_id
字段由ElasticSearch創建,我們也可以自己指定_id
:
POST localhost:9200/blog/user/2/
content-type: application/json
{
"id": 2,
"nickname": "easy"
}
response:
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
訪問文檔
使用GET請求訪問文檔,需要提供_index
, _type
和_id
三個參數唯一標識文檔。
GET localhost:9200/blog/user/2/
response:
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 2,
"found": true,
"_source": {
"id": 2,
"nickname": "easy"
}
}
更新文檔
因為修改文檔後難以更新索引,因此ElasticSearch修改文檔的操作是通過刪除原文檔後重新添加新文檔來進行的。
使用IndexAPI
對已存在的文檔發送POST請求則會更新文檔:
POST localhost:9200/blog/user/2/
content-type: application/json
{
"nickname": "easy",
"gender": "male"
}
response:
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
註意_version
, created
, result
字段顯示文檔已被更新。通過GET請求查看更新後的文檔:
GET localhost:9200/blog/user/2/
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 2,
"found": true,
"_source": {
"nickname": "easy2",
"gender": ”male“
}
}
註意到原文檔中的_id
字段已經不見了,文檔完全由我們發送的上一個POST請求定義。
修改文檔也可以通過PUT方法:
PUT localhost:9200/blog/user/2/
content-type: application/json
{
"id": 2,
"nickname": "easy3"
}
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
再次通過GET請求確認文檔已被修改:
GET localhost:9200/blog/user/2/
{
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 3,
"found": true,
"_source": {
"id": 2
"nickname": "easy3",
}
}
刪除文檔
刪除文檔需要發送DELETE
請求:
DELETE localhost:9200/blog/user/2/
response:
{
"found": true,
"_index": "blog",
"_type": "user",
"_id": "2",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
ElasticSearch入門簡介