ES Restful API GET, POST, PUT, DELETE, HEAD meaning:
1) GET: gets the current state of the request object.
2) POST: changes the current state of the object.
3) PUT: create an object.
4) DELETE: destroy the object.
5) HEAD: request to get the basic information of the object.
The new document in ES (under Index/type) is equivalent to inserting a row of data in Mysql (under the Table of a Database).
1, New document (similar to 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"]
}
The creation is successful as follows:
{
"_index": "blog",
"_type": "ariticle",
"_id": "1 -d",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
2. Retrieve documents (similar to mysql search search select * operation)
http://localhost:9200/blog/ariticle/1/ GET
The search results are as follows:
{
"_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"
]
}
}
Prompt if not found:
{
"_index": "blog",
"_type": "ariticle",
"_id": "11",
"found": false
}
Query all documents as follows:
Search for specific details,
Query example 1: query the cotent column to contain version 1.0 information.
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"
- ]
- }
- }
- ]
- }
}
Query example 2: query the data information of the "enhance" field in the title of the book:
[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" ]
}
} ]
}
}
Query example 3: query data information with ID value of 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. Update document (similar to mysql update operation)
http://localhost:9200/blog/ariticle/1/_update/ POST
{"script":"ctx._source.content = \"new version 2.0 20160714\""}
The results show that:
{
"_index": "blog",
"_type": "ariticle",
"_id": "1",
"_version": 2,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
Query & verify the updated results: (by comparison, the version number has been updated.)
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"
- ]
- }
}
Note that to update the document, you need to add the following under 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. Delete document (similar to mysql delete operation)
http://localhost:9200/blog/ariticle/8/ Return result
{
- "found": true,
- "_index": "blog",
- "_type": "ariticle",
- "_id": "8",
- "_version": 2,
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- }
}