Representation of ES (Elastic Search) Documents and Addendum, Delete and Change Check

Time:2019-8-15

 1、 Documents in ES

ES is document oriented, which means that it can store the whole object or document. However, it not only stores, but also indexes the content of each document so that it can be searched. In ES, you can index, search, sort and filter documents rather than data in rows and columns.

ES uses Javascript Object Notation, or JSONAs a document serialization format. JSON is now supported by most languages and has become the standard format in NoSQL.

  

  _index:Index libraries, similar to relational databases“Database”- It’s where we store and index associated data.

  _type:Types, similar to relational databasesTable. It can be capitalized or lowercase and cannot contain underscores or commas. We will use employee as the type name.

  _id:When combined with _index and _type, you can use ELastic searchUnique identifier (similar to primary key) a document. When you create a document, you can customize _id or allow Elastic search to automatically generate it for you.

It also includes:

  _uid:Document Unique Identification (_type#_id)

  _source:Document raw data

  _all:Connection strings for all fields

 

2. Addition, deletion and alteration of documents

Let’s take the employee object as an example. The first thing we need to do is to store employee data. Each document represents an employee. The behavior of storing data in ES is called indexing. Documents belong to a type, which exists in the index. We can simply compare the corresponding relationship between traditional databases and ES:

Relational database (MYSQL) – > database DB – > table TABLE – > row ROW – > column Column

Elastic search – > Index library Indices – > Types (tables) – > Documents (rows) – > Fields (columns)

ES clusters can contain multiple indices (databases), each of which can contain multiple types (tables), each of which contains multiple documents (rows), and then each document contains multiple fields (columns).

Creating Index Documents

    ①Use your ownIDEstablish:

 

    PUT{index}/{type}/{id}

 

    {

 

 
    
“field”: “value”,

 

     

 

    }

 

    ②ESBuilt-inIDEstablish:

 

    POST{index}/{type}/

 

    {

 

 
    
“field”: “value”,

 

      

 

    }

 

    ①②ESResponse content:

 

    {

 

      ”_index”: “wang”,

 

      ”_type”: “employee”,

 

      ”_id”: xxxxxx,

 

      ”_version”: 1, //Document version number

 

      ”created”: true //Are there any new additions?

 

    }

 

 

 

    ③ Get the specifiedIDDocumentation

 

      GET wang/employee/123?pretty

 

(3) Contents returned:

 

     {

 

      ”_index” : “wang“,

 

      ”_type” : “employee”,

 

      ”_id” : “123”,

 

      ”_version” : 1,

 

      ”found” : true,

 

      ”_source” : {

 

  
       
“email”: “[email protected]”,

 

          “fullName”: “Zhang San“,

 

          

 

  
       
“joine_date”: “2016-06-01”

 

      }

 

     } 

Returns part of the field of the document:

 

    GETReturns the entire document by default and passesGET /wang/employee/123?_source=fullName,email

 

Return only document content, not metadata:

 

    GET wang/employee/123/_source

  

  

 

Modify Documents

 

Update the entire document

 

withPUT
{index}/{type}/{id}

 

In response, we can see thatElasticsearchhold _version  Increased.

 

  {

 

    …

 

    ”_version” : 2,

 

    ”created”: false

 

  }

 

  created 
Identify as false  Because the same index and the same type already exist.IDDocumentation.

 

Internally,ElasticsearchMarked oldThe document adds a complete new document for deletion.Old versions of documents don’t disappear immediately, but you can’t access them either.ElasticsearchIt cleans up deleted documents as you continue to index more data.

 

Locally Update Documents

 

Accept a local document parameter docIt merges into existing documents, objects are merged together, existing scalar fields are overwritten, and new fields are added.

 

  POST wang/employee/123/_update

 

  {

 

doc{

 

“email”
: [email protected]”,

 

“salary”:
1000

 

}

 

  }

 

  emailIt will be overwritten by updates.salaryIt will be added.

 

thisAPI It seems that Allow you to modify parts of the document, but in factElasticsearch

 

Following exactly the same process as before, the process is as follows:

 

    1. Retrieving from Old DocumentsJSON

 

    2. Modify it

 

    3. Delete old documents

 

    4. Index new documents

 

 

Script update document

 

It can also be done by using simple scripts. This example uses a script toageplus5

 

  POST wang/emploee/123/_update

 

  {

 

    ”script” : “ctx._source.age +=
5”

 

  }

 

In the example above,
ctx._source
Point to the currently updated document.

 

Note that the current update operation can only be applied to one document at a time.

 

 

remove document

 

  DELETE {index}/{type}/{id}

 

The return of the existing document:

 

  {

 

    ”found” : true,

 

    ”_index” : “wang“,

 

    ”_type” : “blog”,

 

    ”_id” : “123”,

 

    ”_version” : 3

 

  }

 

Non-existent return:

 

  {

 

    ”found” : false,

 

    ”_index” : “wang“,

 

    ”_type” : “blog”,

 

    ”_id” : “123”,

 

    ”_version” : 4

 

  }

Note: Although the document does not exist, it does_versionStill increased. This is part of the internal record, which ensures that different operations between multiple nodes can be in the correct order.

 

  

Batch exercisesdobulk  API

 

Using a single request to implement multiple documentscreateindexupdate or delete

 

    BulkRequest body format:

 

    { action: { metadata }}\n

 

    { request body }\n

 

    { action: { metadata }}\n

 

    { request body }\n

 

Each line must be
“\n” 
The end of the symbol, including the last line. These are markers that are used as valid separations for each line.

 

    createCreate a document when it does not exist.

 

    indexCreate a new document or replace an existing document.

 

    updateUpdate the document locally.

 

    deleteDelete a document.

 

For example:

 

    POST _bulk

 

    { “delete”: { “_index”:
“wang”, “_type”: “employee”, “_id”:
“123” }}

 

    { “create”: { “_index”:
wang“, “_type”: “blog”, “_id”:
“123” }}

 

    { “title”: “I am off work” }

 

    { “index”: { “_index”:
wang“, “_type”: “blog” }}

 

    { “title”: “I’m really off duty.” }

 

Be careful:deleteThere is no need for the requester, and the last line has to return.

 

3. Simple Query of Documents


3.1  
Through documentationIDObtain

 

slightly

 

    GET /test/employee/1

 

3.2  
Batch acquisition

 

    mget 
API
The parameter is one docsArray, each node of the array defines a document’s _index  _type  _id  Metadata. If you only want to retrieve one or more deterministic fields, you can also define one _source    Parameters:

 

mode1GET _mget

 

    {

 

      ”docs” : [

 

{

 

“_index”
: “itsource”,

 

“_type”
: “blog”,

 

“_id” :
2

 

},

 

{

 

“_index”
: “itsource”,

 

“_type”
: “employee”,

 

“_id” :
1,

 

“_source”:
“email,age”

 

}

 

      ]

 

    }

 

mode2Under the same type of index library

 

  GET
itsource/blog/_mget

 

  {

 

    ”ids”
: [ “2”, “1” ]

 

  }

 

3.3  Empty SearchQuery all

 

Without specifying any query conditions, only all documents in the cluster index are returned:

 

 

 

   GET test/employee/_search Query allemployee

 

  GET
_search

 

 

3.4  
Paging search

 

andSQLUse LIMIT from,sizeKeyword returns the same result with only one page.Elasticsearchaccept from  and size  Parameters:

 

    size  : Number of entries per page, default 10

 

    from  : Skip the initial number of results, default 0

 

If you want to show it on every page5Results, page number from1reach3The request is as follows:

 

    GET crm/emp/_search?size=5
#
query5strip

 

    GET crm/emp/_search?size=5&from=5
#
query6-10

 

    GET crm/emp/_search?size=5&from=10
#
query11-15

 

3.5  
Query string search

 

A search can be pureuriExecute the query. Using search in this mode, not all options are exposed. It can be easily and quickly carried out. curl Test.

  

  SqlQuery:

Query age is25Employees aged 20

    GET
itsource/employee/_search?q=age:25

IfqThe latter parameter is not specifiedFiledsDefault query_allFields (links to all fields of an implied document)

Similar query syntax referenceluceneFor example:

Combination query:

   +Represents and,Multiple Conditions Do and Operate====>MUST

   Spatial representation or,Multiple Conditions Do or Operate====>SHOULD

   Representation of non,Multiple Conditions for Non-operations====>MUST_NOT

 

  +name:john +tweet:mary

  +name:(mary john)
+date:>2014-09-10 +(aggregations geo)

  age[20 TO 30] 

 

  Select name,age from t_emp where age between and 10 order by
age desc;

  

 

 

 

 

 

 

 

 

 

 

Creating Index Documents

Use your ownIDEstablish:

PUT{index}/{type}/{id}

{

 
“field”: “value”,

 

}

ESBuilt-inIDEstablish:

POST{index}/{type}/

{

 
“field”: “value”,

 

}

①②ESResponse content:

{

“_index”: “itsource”,

“_type”: “employee”,

“_id”: xxxxxx,

“_version”: 1, //Document version number

“created”: true //Are there any new additions?

}

 

Get the specifiedIDDocumentation

GET itsource/employee/123?pretty

(3) Contents returned:

{

“_index” : “itsource”,

“_type” : “employee”,

“_id” : “123”,

“_version” : 1,

“found” : true,

“_source” : {

  
“email”: “[email protected]”,

   “fullName”: “Wen Bing“,

  

  
“joine_date”: “2016-06-01”

}

}

Returns part of the field of the document:

GETReturns the entire document by default and passesGET /itsource/employee/123?_source=fullName,email

Return only document content, not metadata:

GET itsource/employee/123/_source

    Check only if the document exists(Query header information)

curl -i -X HEAD http://localhost:9200/itsource/employee/123


Modify Documents

Update the entire document

withPUT
{index}/{type}/{id}

In response, we can see thatElasticsearchhold _version  Increased.

{

“_version” : 2,

“created”: false

}

created 
Identify as
false 
Because the same index and the same type already exist.IDDocumentation.

Internally,ElasticsearchMarked oldThe document adds a complete new document for deletion.Old versions of documents don’t disappear immediately, but you can’t access them either.ElasticsearchIt cleans up deleted documents as you continue to index more data.

Locally Update Documents

Accept a local document parameter
doc
It merges into existing documents, objects are merged together, existing scalar fields are overwritten, and new fields are added.

POST itsource/employee/123/_update

{

doc{

“email” : “[email protected]”,

“salary”: 1000

}

}

emailIt will be overwritten by updates.salaryIt will be added.

thisAPI It seems that Allow you to modify parts of the document, but in factElasticsearch

Following exactly the same process as before, the process is as follows:

1. Retrieving from Old DocumentsJSON

2. Modify it

3. Delete old documents

4. Index new documents

Script update document

It can also be done by using simple scripts. This example uses a script toageplus5

POST itsource/emploee/123/_update

{

“script” : “ctx._source.age
+= 5”

}

In the example above,
ctx._source
Point to the currently updated document.

Note that the current update operation can only be applied to one document at a time.

remove document

DELETE {index}/{type}/{id}

The return of the existing document:

{

“found” : true,

“_index” : “website”,

“_type” : “blog”,

“_id” : “123”,

“_version” : 3

}

Non-existent return:

{

“found” : false,

“_index” : “website”,

“_type” : “blog”,

“_id” : “123”,

“_version” : 4

}

Note: Although the document does not exist, it does_versionStill increased. This is part of the internal record, which ensures that different operations between multiple nodes can be in the correct order.

Batch exercisesdobulk  API

Using a single request to implement multiple documentscreateindexupdate or delete

BulkRequest body format:

{ action: { metadata }}\n

{ request body }\n

{ action: { metadata }}\n

{ request body }\n

Each line must be
“\n” 
The end of the symbol, including the last line. These are markers that are used as valid separations for each line.

createCreate a document when it does not exist.

indexCreate a new document or replace an existing document.

updateUpdate the document locally.

deleteDelete a document.

For example:

POST _bulk

{ “delete”: { “_index”:
“itsource”, “_type”: “employee”,
“_id”: “123” }}

{ “create”: { “_index”:
“itsource”, “_type”: “blog”, “_id”:
“123” }}

{ “title”: “My blog” }

{ “index”: { “_index”:
“itsource”, “_type”: “blog” }}

{ “title”: “My Second Blog” }

 

Be careful:deleteThere is no need for the requester, and the last line has to return.