1. Document core metadata
1.1 _index metadata
- Represents the index in which a document is stored
- Similar data are placed in one index, while non-similar data are placed in different indexes: Product Index (which contains all commodities), Sales Index (which contains all commodity sales data), and Inventory Index (which contains all inventory related data). For example, product, sales, human resource (Employee), all in a large index, such as company index, not appropriate
- Ndex contains many similar documents: If you put three documents, the fields of each document are completely different, so it’s not similar, so it doesn’t fit in an index
- Index names must be lowercase, cannot start with an underscore, and cannot contain commas
1.2 _type metadata
- Represents what category document belongs to in index.
- An index is usually divided into multiple types to logically classify slightly different types of data in the index: Because a batch of the same data may have many of the same fields, but there may still be some slight differences, there may be a few different fields, for example, goods, for example, may be divided into electronic goods, fresh goods, daily chemical goods, and so on.
- The type name can be uppercase or lowercase, but cannot start with an underscore or contain a comma
1.3 _ID Metadata
-
A unique identifier that represents a document and, along with index and type, uniquely identifies and locates a document
-
We can manually specify the ID of the document (put /index/type/ ID), or we can leave it blank and let es automatically create an ID for us
-
Manually specify the document ID
In general, when importing some data from some other system into ES, this is the way to use the unique identifier of the existing data in the system as the ID of the Document in ES. For example, for example, we are currently developing an e-commerce website for search function, or an OA system for employee search function. At this point, the data will first be stored in the website system or IT system internal database, there will be a primary key (growth, UUID, or business number) of the database. If you are importing data into ES, it is a good time to use the primary key that the data already exists in the database.
-
Automatic id generation
Once the data is generated, it might not have an ID, it might just put es in there, and then it might not be a good idea to manually specify the form of the document ID
Automatically generated ID, length of 20 characters, URL security, Base64 encoding, GUID, distributed system parallel generation is unlikely to conflict
-
-
Code sample
-
Manually specify a unique identifier
PUT /test_index/test_type/1 { "test_field": "create id by myself" } Copy the code
-
Automatic id generation
POST /test_index/test_type { "test_field": "create id by es" } Copy the code
-
1.5_source metadata and custom return results
_source metadata: That is, the JSON string we used in the request body when creating a document is returned to us unchanged by default when we get it.
PUT /test_index/test_type/2
{
"name": "Tom",
"age": 12,
"gender": "M"
}
Copy the code
Execute the GET request and return data:
GET test_index/test_type/2
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"name": "Tom",
"age": 12,
"gender": "M"
}
}
Copy the code
Customizing return results
-
Customize the result returned by specifying which fields are returned in _source
GET /test_index/test_type/2? _source=name,age { "_index": "test_index", "_type": "test_type", "_id": "2", "_version": 1, "found": true, "_source": { "name": "Tom", "age": 12 } }Copy the code
2. Document full replacement, forced creation and deletion
2.1 Full replacement
- The syntax is the same as creating a document; if the document ID does not exist, it is created; If the document ID already exists, then the full replacement operation replaces the JSON string content of the document
- Document is immutable, so if you want to change the contents of the document, the first way to do this is to completely replace it, just re-index the document, replace everything in it
2.2 Forcibly Creating a Vm
-
If you need to create a new document and do not want to replace it, you need to force the document creation.
-
Grammar:
PUT /index/type/id? op_type=create PUT /index/type/id/_createCopy the code
Either can work, but an error is reported if the document ID already exists
2.3 delete
-
grammar
DELETE /index/type/id Copy the code
-
It will not be physically deleted immediately, but only mark the document as deleted. When there is more and more data, ES will automatically delete the document marked as Deleted in the background at an appropriate time