Elasticsearch增、刪、改、查操作深入詳解
引言:
對於剛接觸ES的童鞋,經常搞不明白ES的各個概念的含義。尤其對“索引”二字更是與關係型資料庫混淆的不行。本文通過對比關係型資料庫,將ES中常見的增、刪、改、查操作進行圖文呈現。能加深你對ES的理解。同時,也列舉了kibana下的圖形化展示。
ES Restful API GET、POST、PUT、DELETE、HEAD含義:
1)GET:獲取請求物件的當前狀態。
2)POST:改變物件的當前狀態。
3)PUT:建立一個物件。
4)DELETE:銷燬物件。
5)HEAD:請求獲取物件的基礎資訊。
Mysql與Elasticsearch核心概念對比示意圖
以上表為依據,
ES中的新建文件(在Index/type下)相當於Mysql中(在某Database的Table)下插入一行資料。
1、新建文件(類似mysql insert插入操作)
http://localhost:9200/blog/ariticle/1 put
{
"title":"New version of Elasticsearch released!",
"content":"Version 1.0 released today!",
"tags":["announce","elasticsearch","release"]
}
建立成功如下顯示:
{
- "_index": "blog",
- "_type": "ariticle",
- "_id": "1 -d",
- "_version": 1,
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "created": true
}
2、檢索文件(類似mysql search 搜尋select*操作)
http://localhost:9200/blog/ariticle/1/GET
檢索結果如下:
{
- "_index": "blog",
- "_type": "ariticle",
- "_id": "1",
- "_version": 1,
- "found": true,
- "_source": {
- "title": "New version of Elasticsearch released!",
- "content": "Version 1.0 released today!",
- "tags": [
- "announce"
- ,
- "elasticsearch"
- ,
- "release"
- ]
- }
}
如果未找到會提示:
{
- "_index": "blog",
- "_type": "ariticle",
- "_id": "11",
- "found": false
}
查詢全部文件如下:
具體某個細節內容檢索,
查詢舉例1:查詢cotent列包含版本為1.0的資訊。
http://localhost:9200/blog/
_search?pretty&q=content:1.0
{
- "took": 2,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 0.8784157,
- "hits": [
- {
- "_index": "blog",
- "_type": "ariticle",
- "_id": "6",
- "_score": 0.8784157,
- "_source": {
- "title": "deep Elasticsearch!",
- "content": "Version 1.0!",
- "tags": [
- "deep"
- ,
- "elasticsearch"
- ]
- }
- }
- ]
- }
}
查詢舉例2:查詢書名title中包含“enhance”欄位的資料資訊:
[root@5b9dbaaa1a ~]# curl -XGET 10.200.1.121:9200/blog/ariticle/_search?pretty -d ‘
> { "query" : {
> "term" :
> {"title" : "enhance" }
> }
> }'
{
"took" : 189,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.8784157,
"hits" : [ {
"_index" : "blog",
"_type" : "ariticle",
"_id" : "4",
"_score" : 0.8784157,
"_source" : {
"title" : "enhance Elasticsearch!",
"content" : "Version 4.0!",
"tags" : [ "enhance", "elasticsearch" ]
}
}, {
"_index" : "blog",
"_type" : "ariticle",
"_id" : "5",
"_score" : 0.15342641,
"_source" : {
"title" : "enhance Elasticsearch for university!",
"content" : "Version 5.0!",
"tags" : [ "enhance", "elasticsearch" ]
}
} ]
}
}
查詢舉例3:查詢ID值為3,5,7的資料資訊:
[root@5b9dbaaa148a ~]# curl -XGET 10.200.1.121:9200/blog/ariticle/_search?pretty -d ‘
{ "query" : {
"terms" :
{"_id" : [ "3", "5", "7" ] }
}
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.19245009,
"hits" : [ {
"_index" : "blog",
"_type" : "ariticle",
"_id" : "5",
"_score" : 0.19245009,
"_source" : {
"title" : "enhance Elasticsearch for university!",
"content" : "Version 5.0!",
"tags" : [ "enhance", "elasticsearch" ]
}
}, {
"_index" : "blog",
"_type" : "ariticle",
"_id" : "7",
"_score" : 0.19245009,
"_source" : {
"title" : "deep Elasticsearch for university!",
"content" : "Version 2.0!",
"tags" : [ "deep", "elasticsearch", "university" ]
}
}, {
"_index" : "blog",
"_type" : "ariticle",
"_id" : "3",
"_score" : 0.19245009,
"_source" : {
"title" : "init Elasticsearch for university!",
"content" : "Version 3.0!",
"tags" : [ "initialize", "elasticsearch" ]
}
} ]
}
}
3、更新文件(類似mysql update操作)
http://localhost:9200/blog/ariticle/1/_update/POST
{“script”:”ctx._source.content = \”new version 2.0 20160714\”“}
更新後結果顯示:
{
- “_index”: “blog”,
- “_type”: “ariticle”,
- “_id”: “1”,
- “_version”: 2,
- “_shards”: {
- ”total”: 2,
- “successful”: 1,
- “failed”: 0
- }
}
查詢&驗證更新後結果:(對比可知,版本號已經更新完畢)
http://localhost:9200/blog/ariticle/1/
{
- "_index": "blog",
- "_type": "ariticle",
- "_id": "1",
- "_version": 2,
- "found": true,
- "_source": {
- "title": "New version of Elasticsearch released!",
- "content": "new version 2.0 20160714",
- "tags": [
- "announce"
- ,
- "elasticsearch"
- ,
- "release"
- ]
- }
}
`![這裡寫圖片描述](https://img-blog.csdn.net/20160717132407353)``
注意更新文件需要在elasticsearch_win\config\elasticsearch.yml下新增以下內容:
script.groovy.sandbox.enabled: true
script.engine.groovy.inline.search: on
script.engine.groovy.inline.update: on
script.inline: on
script.indexed: on
script.engine.groovy.inline.aggs: on
index.mapper.dynamic: true
4、刪除文件(類似mysql delete操作)
http://localhost:9200/blog/ariticle/8/回結果
{
- "found": true,
- "_index": "blog",
- "_type": "ariticle",
- "_id": "8",
- "_version": 2,
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- }
}