“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The new document

Documents, that is, data of a certain type in the index library, are indexed according to rules for future searches. The analogy is for each row of data in a database.

Add and randomly generate ids

A POST request allows you to add document data to an existing index library.

Grammar:

POST/index library name/type name {"key":"value"
}
Copy the code

Example:

POST /lagou/goods/
{
    "title":"Xiaomi Phone"."images":"http://image.lagou.com/12479122.jpg"."price":2699.00
}
Copy the code

Response:

{
    "_index": "lagou"."_type": "goods"."_id": "tURGznQB29tVfg_iWHfl"."_version": 1."result": "created"."_shards": {
        "total": 3."successful": 1."failed": 0
    },
    "_seq_no": 0."_primary_term": 2
}
Copy the code

You can see that the result is “created”.

In addition, it should be noted that there is an _ID field in the response result, which is the unique identifier of the document data. The future increment, deletion, modification and check will rely on this ID as the unique identifier.

It can be seen that the id value is: tURGznQB29tVfg_iWHfl. Here we did not specify id when adding, so ES helped us randomly generate the ID.

To view the document

According to the REST style, the new is POST, the query should be GET, but the query generally requires conditions, in this case, we will bring the id of the data just generated.

View the data with Kibana:

GET /lagou/goods/tURGznQB29tVfg_iWHfl
Copy the code

View the results:

{
    "_index": "lagou"."_type": "goods"."_id": "tURGznQB29tVfg_iWHfl"."_version": 1."found": true."_source": {
        "title": "Xiaomi Phone"."images": "http://image.lagou.com/12479122.jpg"."price": 2699}}Copy the code
  • _source: Source document information, all data in it.
  • _ID: unique identifier of this document
  • Automatically generated ID, length of 20 characters, URL-safe, Base64 encoding, GUID (globally unique identifier), distributed system parallel generation is impossible to conflict
  • In actual development, it is not recommended to use the ID generated by ES, because it is too long and string type, which is inefficient in retrieval. Suggestion: Use the unique ID in the data table as the document ID of ES

Add documents and customize ids

If we want to specify the id when we add it ourselves, we can do this:

POST/index library name/type /id value {... }Copy the code

Example:

POST /lagou/goods/2
{
    "title":"Rice phone"."images":"http://image.lagou.com/12479122.jpg"."price":2899.00
}
Copy the code

Data obtained:

{
    "_index": "lagou"."_type": "goods"."_id": "2"."_score": 1."_source": {
        "title": "Rice phone"."images": "http://image.lagou.com/12479122.jpg"."price": 2899}}Copy the code

Modify the data

PUT: Modifies a document

POST: Adds a document

Change the request mode to PUT. But you must specify an ID,

  • If the document corresponding to the ID exists, modify it
  • If the document corresponding to the ID does not exist, add it

For example, if id = 3 and id = 3, it should be added:

PUT /lagou/goods/3
{
    "title":"Super Meter Phone"."images":"http://image.lagou.com/12479122.jpg"."price":3899.00."stock": 100."saleable":true
}
Copy the code

Results:

{
    "_index": "lagou"."_type": "goods"."_id": "3"."_version": 1."result": "created"."_shards": {
        "total": 2."successful": 1."failed": 0
    },
    "_seq_no": 1."_primary_term": 1
}
Copy the code

You can see it’s created, it’s new.

Let’s execute the request again, but with the data changed:

PUT /lagou/goods/3
{
    "title":"Super Meter Phone"."images":"http://image.lagou.com/12479122.jpg"."price":3299.00."stock": 100."saleable":true
}
Copy the code

View the results:

{
    "_index": "lagou"."_type": "goods"."_id": "3"."_version": 2."result": "updated"."_shards": {
        "total": 2."successful": 1."failed": 0
    },
    "_seq_no": 2."_primary_term": 1
}
Copy the code

The updated data is updated

Delete the data

DELETE using the DELETE request, again, you need to DELETE by ID:

grammar

DELETE/index base name/type name/ID valueCopy the code

Example:

Intelligent judgment

When we added data just now, the fields we added were defined in the type in advance. If the fields we added were not defined in advance, can we succeed?

Elasticsearch is actually very smart, you don’t need to set up any mapping for the index library, it can also dynamically add data mapping based on the type of data you enter.

Test it out:

POST /lagou/goods/3
{
    "title":"Super Meter Phone"."images":"http://image.lagou.com/12479122.jpg"."price":3299.00."stock": 200."saleable":true."subTitle":"Rice"
}
Copy the code

We have added three additional fields: stock, saleable, subtitle and subtitle.

The results:

{
    "_index": "lagou"."_type": "goods"."_id": "3"."_version": 3."result": "updated"."_shards": {
        "total": 2."successful": 1."failed": 0
    },
    "_seq_no": 3."_primary_term": 1
}
Copy the code

Success! Take a look at the index library mapping:

Stock, saleable, and subtitle are mapped successfully.

Subtitle is String data, ES can’t judge intelligently, it will store two fields. Such as:

  • Subtitle: Text type
  • Subtitle. keyword: Indicates the keyword type

This kind of intelligent mapping, the underlying principle is dynamic template mapping, if we want to modify the rules of this intelligent mapping, in fact, just modify the dynamic template can!

Dynamic mapping template

Syntax for dynamic templates:

  1. The template name is optional
  2. Match conditions. Any undefined field that meets the condition will be mapped according to this rule
  3. Mapping rule: Mapping rule after a match is successful

For example, we can automatically map all unmapped string data to keyword:

PUT lagou3
{
    "mappings": {
        "goods": {
            "properties": {
                "title": {
                    "type": "text"."analyzer": "ik_max_word"}},"dynamic_templates": [{"strings": {
                        "match_mapping_type": "string"."mapping": {
                            "type": "keyword"."index":false."store":true}}}]}}Copy the code

In this case, we have made two mapping configurations:

  • Title field: unified mapping to text type, and formulate a word divider
  • Other fields: all fields of string type are treated as keyword.

In this way, unknown string data is not mapped as both text and keyword, but as keyword!

Let’s try adding a new data:

POST /lagou3/goods/1
{
    "title":"Super Meter Phone"."images":"http://image.lagou.com/12479122.jpg"."price":3299.00
}
Copy the code

We only configured the title. Now let’s see what types images and price are mapped to:

GET /lagou3/_mapping
Copy the code

Results:

{
    "lagou3": {
        "mappings": {
            "goods": {
                "dynamic_templates": [{"strings": {
                            "match_mapping_type": "string"."mapping": {
                                "type": "keyword"}}}]."properties": {
                    "images": {
                        "type": "keyword"
                    },
                    "price": {
                        "type": "float"
                    },
                    "title": {
                        "type": "text"."analyzer": "ik_max_word"
                    }
                }
            }
        }
    }
}
Copy the code

You can see that images are mapped to keyword instead of text and keyword, indicating that our dynamic template is working!