ElasticSearch - index library and document related command operations

Table of contents

1. ElasticSearch index library operation

1.1. mapping attribute

1.2. Index library related operations

1.2.1. Create index library

1.2.2. Adding and deleting index libraries

1.2.3. Modify the index library

1.3. Document operation

1.3.1. Add documents

1.3.2. Query and delete documents

1.3.3. Modify documents

1. Full modification: old documents will be deleted first and new documents will be added.

2. Incremental modification: Modify the execution field value.

What happens if the document fully modified through PUT is different from the original document? 


1. ElasticSearch index library operation


1.1. mapping attribute

mappinig is a constraint on documents in the index database. Common mapping attributes are as follows:

  • type: Indicates the field data type. Common types are as follows
    • String: text (text that can be segmented), keyword (exact value, text that does not need to be segmented. For example: country name, brand, company name, IP address...).
    • Numeric value: long, integer, short, byte, double, float
    • Boolean: boolean
    • Date date
    • Object: object
  • index: Whether to create an index. Default true.
  • analyzer: What kind of word segmenter is used for word segmentation (such as the IK word segmenter mentioned before).
  • properties: subfields of this field.

For example:

PUT /heima
{
  "mappings": {
    "properties": {
      //这里是自定义的 info 信息
      "info":{ 
        "type": "text", //表示 info 中的信息可以进行分词.
        "analyzer": "ik_smart" //使用 IK 分词器分词
        //这里没有定义 index ,表示默认为词条创建索引
      },
      "email":{
        "type": "keyword", //表示 email 不需要分词,是一个关键字
        "index": "false" //不创建索引
      },
      "name":{
        "properties": {
          "firstName": {
            "type": "keyword"
          }
        }
      },
      // ......
    }
  }
}

1.2. Index library related operations

1.2.1. Create index library

In ES, index libraries and documents are operated through Restful requests. The content of the request is represented by DSL statements.

The syntax for creating an index library and mapping is as follows:

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

Assume that the following index library is created

PUT /userinfo
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email":{
        "type": "keyword",
        "index": "false"
      },
      "name":{
        "properties": {
          "firstName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

1.2.2. Adding and deleting index libraries

View the index library syntax:

GET/索引库名

Example:

GET /userinfo

Syntax for deleting an index library:

DELETE /索引库名

Example:

DELETE /userinfo

After deleting and then GET, you will find that the index library cannot be found, as follows

1.2.3. Modify the index library

Once created, the index library and mapping cannot be modified , only new fields can be added based on the original ones .

The syntax is as follows:

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

Examples are as follows:

PUT /userinfo/_mapping
{
  "properties": {
    "age":{
      "type": "integer"
    }
  }
}

For example, here I first create the userinfo index library.

Then add the age field:

 

Then query the userinfo index database

If the PUT is an existing field, such as age, change the type to keyword, and it will tell you that the integer type in the mapper cannot be changed to keyword.

1.3. Document operation

1.3.1. Add documents

This is similar to adding data to a table in MySQL. You need to create the table first and then add data.

The same is true for document operations. You need to create an index library before adding documents.

The syntax for adding a document is as follows:

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

Examples are as follows:

POST /userinfo/_doc/1
{
    "info": "大家好,我是练习了两年半的偶像练习生",
    "email": "[email protected]",
    "name": {
        "firstName": "c",
        "lastName": "xk"
    }
}

Assume that the following index library is created:

Add documentation below:

  •  _index: Indicates which index library you are adding documents to.
  • _type: indicates what type is added to the type, _doc indicates the document.
  • _id: is the specified id of the added document.
  • version: This is a version number, based on the version number implemented by spin lock. Every time this document is operated on, the version number will be + 1.
  • result: created means the creation is successful.

1.3.2. Query and delete documents

Query document syntax:

GET /索引库名/_doc/文档id 

Example:

GET /userinfo/_doc/1 

Delete document syntax:

DELETE /索引库名/_doc/文档id 

Example:

DELETE /userinfo/_doc/1 

1.3.3. Modify documents

There are two ways to modify the document

1. Full modification: old documents will be deleted first and new documents will be added.

The syntax is as follows:

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

Examples are as follows:

PUT /userinfo/_doc/1
{
    "info": "我是练习两年半的偶像练习生",
    "email": "[email protected]",
    "name": {
        "firstName": "c",
        "lastName": "xk"
    }
}

For example, the following document originally existed

Now PUT the following operation

Then the GET query is the modified document

2. Incremental modification: Modify the execution field value.

Mainly note that the index name is followed by _update 

The syntax is as follows:

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

Examples are as follows:

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

Assume that the original document is as follows

Modify the value of the info field as follows

Get documents via GET

What happens if the document fully modified through PUT is different from the original document? 

For example, the following document exists

A new field age is added here, and the results are as follows

In fact, the way es is processed here is to modify the document according to the operation of "delete old documents and add new documents".

Guess you like

Origin blog.csdn.net/CYK_byte/article/details/133245090