ES index library operation & document operation


2. Index library operation

The index library is similar to the database table, and the mapping mapping is similar to the table structure.

If we want to store data in es, we must first create "library" and "table".


2.1.mapping mapping properties

Mapping is a constraint on documents in the index library. Common mapping attributes include:

  • type: field data type, common simple types are:
    • String: text (text that can be segmented), keyword (exact value, such as: brand, country, ip address)
    • Value: long, integer, short, byte, double, float,
    • Boolean: boolean
    • date: date
    • Object: object
  • index: Whether to create an index, the default is true
  • analyzer: which tokenizer to use
  • properties: subfields of this field

For example the following json document:

{
    
    
    "age": 21,
    "weight": 52.1,
    "isMarried": false,
    "info": "黑马程序员Java讲师",
    "email": "[email protected]",
    "score": [99.1, 99.5, 98.9],
    "name": {
    
    
        "firstName": "云",
        "lastName": "赵"
    }
}

Corresponding to each field mapping (mapping):

  • age: The type is integer; participate in the search, so the index needs to be true; no tokenizer is required
  • weight: The type is float; participate in the search, so the index needs to be true; no tokenizer is required
  • isMarried: The type is boolean; participate in the search, so the index needs to be true; no tokenizer is required
  • info: The type is a string, word segmentation is required, so it is text; to participate in the search, so the index needs to be true; the word segmenter can use ik_smart
  • email: The type is a string, but word segmentation is not required, so it is a keyword; it does not participate in the search, so the index needs to be false; no word segmentation is required
  • score: Although it is an array, we only look at the type of the element, which is float; participate in the search, so the index needs to be true; no tokenizer is required
  • name: The type is object, and multiple sub-attributes need to be defined
    • name.firstName; the type is a string, but word segmentation is not required, so it is a keyword; it participates in the search, so the index needs to be true; no word segmentation is required
    • name.lastName; the type is a string, but word segmentation is not required, so it is a keyword; it participates in the search, so the index needs to be true; no word segmentation is required

2.2. CRUD of the index library

Here we uniformly use Kibana to write DSL to demonstrate.


2.2.1. Create index library and mapping


Basic syntax:

  • Request method: PUT
  • Request path: /index library name, which can be customized
  • Request parameter: mapping mapping

Format:

PUT /索引库名称
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "字段名":{
    
    
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
    
    
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
    
    
        "properties": {
    
    
          "子字段": {
    
    
            "type": "keyword"
          }
        }
      },
      // ...略
    }
  }
}

Example:

PUT /heima
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "info":{
    
    
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email":{
    
    
        "type": "keyword",
        "index": "falsae"
      },
      "name":{
    
    
        "properties": {
    
    
          "firstName": {
    
    
            "type": "keyword"
          }
        }
      },
      // ... 略
    }
  }
}

2.2.2. Query index library

Basic syntax :

  • Request method: GET

  • Request path: /index library name

  • Request parameters: none

Format :

GET /索引库名

Example :


2.2.3. Modify the index library

Although the inverted index structure is not complicated, once the data structure changes (for example, the tokenizer is changed), the inverted index needs to be recreated, which is a disaster. Therefore, once the index library is created, the mapping cannot be modified .

Although the existing fields in the mapping cannot be modified, it is allowed to add new fields to the mapping because it will not affect the inverted index.

Grammar description :

PUT /索引库名/_mapping
{
    
    
  "properties": {
    
    
    "新字段名":{
    
    
      "type": "integer"
    }
  }
}

Example :


2.2.4. Delete the index library

grammar:

  • Request method: DELETE

  • Request path: /index library name

  • Request parameters: none

Format:

DELETE /索引库名

Test in kibana:


2.2.5. Summary

What are the index library operations?

  • Create an index library: PUT /index library name
  • Query index library: GET / index library name
  • Delete the index library: DELETE / index library name
  • Add field: PUT /index library name/_mapping

3. Document operation


3.1. New document

grammar:

POST /索引库名/_doc/文档id
{
    
    
    "字段1": "值1",
    "字段2": "值2",
    "字段3": {
    
    
        "子属性1": "值3",
        "子属性2": "值4"
    },
    // ...
}

Example:

POST /heima/_doc/1
{
    
    
    "info": "黑马程序员Java讲师",
    "email": "[email protected]",
    "name": {
    
    
        "firstName": "云",
        "lastName": "赵"
    }
}

response:


3.2. Query documents

According to the rest style, the new addition is post, and the query should be get, but the query generally requires conditions. Here we bring the document id.

grammar:

GET /{
    
    索引库名称}/_doc/{
    
    id}

View data through kibana:

GET /heima/_doc/1

View Results:


3.3. Delete document

Deletion uses the DELETE request. Similarly, it needs to be deleted according to the id:

grammar:

DELETE /{
    
    索引库名}/_doc/id值

Example:

# 根据id删除数据
DELETE /heima/_doc/1

result:


3.4. Modify the document

There are two ways to modify:

  • Full modification: directly overwrite the original document
  • Incremental modification: modify some fields in the document

3.4.1. Full modification

Full modification is to overwrite the original document, its essence is:

  • Delete a document based on the specified id
  • Add a new document with the same id

Note : If the id does not exist when deleting according to the id, the addition in the second step will also be executed, and the operation will change from modification to addition.

grammar:

PUT /{
    
    索引库名}/_doc/文档id
{
    
    
    "字段1": "值1",
    "字段2": "值2",
    // ... 略
}

Example:

PUT /heima/_doc/1
{
    
    
    "info": "黑马程序员高级Java讲师",
    "email": "[email protected]",
    "name": {
    
    
        "firstName": "云",
        "lastName": "赵"
    }
}

3.4.2. Incremental modification

Incremental modification is to modify only some fields in the document matching the specified id.

grammar:

POST /{
    
    索引库名}/_update/文档id
{
    
    
    "doc": {
    
    
         "字段名": "新的值",
    }
}

Example:

POST /heima/_update/1
{
    
    
  "doc": {
    
    
    "email": "[email protected]"
  }
}

3.5. Summary

What are the document operations?

  • Create document: POST /{index library name}/_doc/document id { json document }
  • Query documents: GET /{index library name}/_doc/document id
  • Delete document: DELETE /{index library name}/_doc/document id
  • Modify the document:
    • Full modification: PUT /{index library name}/_doc/document id { json document }
    • Incremental modification: POST /{index library name}/_update/document id { “doc”: {field}}

Study Notes from Dark Horse Programmer

By – Suki 2023/4/8

Guess you like

Origin blog.csdn.net/Eumenides_Suki/article/details/130025721