Chapter 3 - Simple API
Oaks from little acorns
ES provides several ways of operating data, one of the more common way is for RESTful API.
Simple Experience
Postman using an HTTP request (curl course also be used in the command line).
Index Index
Creating an index
Creating a name demo
index:
PUT http://localhost:9200/demo
ES response:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "demo"
}
When you create an index, you can specify the number of primary fragmentation and fragmentation copies:
PUT http://localhost:9200/demo
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
}
}
ES response:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "demo"
}
Check the specified index
GET http://localhost:9200/demo
ES response:
{
"demo": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1561110747038",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "kjPqDUt6TMyywg1P7qgccw",
"version": {
"created": "5060499"
},
"provided_name": "demo"
}
}
}
}
ES query the index
ES query the index case:
GET http://localhost:9200/_cat/indices?v
ES response:
health | status | index | uuid | in | rep | docs.count | docs.deleted | store.size | pri.store.size |
---|---|---|---|---|---|---|---|---|---|
yellow | open | demo | wqkto5CCTpWNdP3HGpLfxA | 5 | 1 | 0 | 0 | 810b | 810b |
yellow | open | .kibana | pwKW9hJyRkO7_pE0MNE05g | 1 | 1 | 1 | 0 | 3.2kb | 3.2kb |
You can see the current ES in a total of two indexes, one is we just created demo
, and the other is indexed kibana created .kibana
. Some information in the table represents some of the state index.
health: health status, not all of the primary Red represents fragments are available, i.e., part of the main fragments available . a front tile yellow slice unavailable backup available, often health of single ES, Greens represents all the primary and the backup slice slices are available. (The official description of the health status of the cluster, https://www.elastic.co/guide/en/elasticsearch/guide/master/cluster-health.html )
status: Index State, Open means open the document data may be read or written in the index, the index case Close Close represents memory occupied will be released, but at this time the index is not read and write operations.
index: Index
uuid: identifies Index
pri: primary index points number of sheets
rep: number of fragments copy of the index, an expressed copy of a slice (primary slice how many there are that many backup slice, denoted here five slices prepared).
docs.count: the number of documents
docs.deleted: the number of documents deleted
store.size: index size
pri.store.size: master slice size occupied
Delete Index
Delete demo
index, delete the index is equivalent to delete the library running, please exercise caution.
DELETE http://localhost:9200/demo
ES response:
{
"acknowledged": true
}
The Type type (and also defines the mapping Mapping Type field)
Creating type
In the foregoing basic terms we mentioned type Type similar relational database table structure definition table mapping Mapping. Need to meet Mapping Mapping When you create a type Type.
Create an index demo
type is example_type
, contains two fields: created
type date, message
type keyword:
method one:
PUT http://localhost:9200/demo/_mapping/example_type
{
"properties":{
"created":{
"type":"date"
},
"message":{
"type":"keyword"
}
}
}
Run the operation again query the index, has been found to be the type of Type is created, unfortunately, once defined if the type Type (or mapping Mapping), it can not be deleted but modified in order to ensure the smooth progress of two ways to create this tutorial type, so here is executed DELETE http://localhost:9200/demo
to delete the index. Do not delete the index after creating the index, following in this way is to create a Type while creating indexes and define Mapping
Second way:
PUT http://localhost:9200/demo
{
"mappings":{
"example_type":{
"properties":{
"created":{
"type":"date"
},
"message":{
"type":"keyword"
}
}
}
}
}
At this execution GET http://localhost:9200/demo
, we can see that we have created the first index and table structure is created in the ES, the next insert that data (ie documents).
Document Document
Insert Document
System Definition_id
POST http://localhost:9200/demo/example_type
{
"created":1561135459000,
"message":"test1"
}
ES response:
{
"_index": "demo",
"_type": "example_type",
"_id": "AWt67Ql_Tf0FgxupYlBX",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
Query document
ElasticSearch core functionality - search.
POST http://localhost:9200/demo/example_type/_search?pretty
ES response:
{
"took": 183,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "example_type",
"_id": "AWt67Ql_Tf0FgxupYlBX",
"_score": 1,
"_source": {
"created": 1561135459000,
"message": "test1"
}
}
]
}
}
Queries about the document is the core ElasticSearch, the later chapters will detail some basic simple queries and more advanced complex queries, insert here only as a validation of the data, not too much expansion.
Modify the document
According to the document _id
modification
POST http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX/_update
{
"doc":{
"message":"updated"
}
}
ES response:
{
"_index": "demo",
"_type": "example_type",
"_id": "AWt67Ql_Tf0FgxupYlBX",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
Delete Document
Delete _id
as AWt67Ql_Tf0FgxupYlBX documents
DELETE http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX
ES response:
{
"found": true,
"_index": "demo",
"_type": "example_type",
"_id": "AWt67Ql_Tf0FgxupYlBX",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
No public concern: CoderBuff, reply "es" get "ElasticSearch6.x combat tutorial" full version of PDF, reply "lottery" participation "from Lucene to Elasticsearch: full-text search real" books sweepstakes (7.17-7.21).