Moment For Technology

Elasticsearch- Core (5)- Mapping operations

Posted on Dec. 2, 2022, 5:44 p.m. by Shane Stewart
Category: The back-end Tag: The back-end elasticsearch

1. Create a mapping

  1. If you have an index library, you have a database in the database
  2. Next, you need to create a mapping in an index, similar to a table structure in a database.
  3. To create a database table, you need to set the field name, type, length, constraint, etc. The same goes for an index library. You need to know what fields are in this type and what constraints each field has. This is called mapping.
  4. If you do not specify a mapping, the mapping will be automatically added for the given type by default

1.1 Mapping during creation

  1. You can specify the mapping when you create the index
    • "Mappings. Properties" is a fixed structure that specifies the mapping attribute to be created
PUT index
{
  "mappings" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"}}}}Copy the code
  1. After the creation is complete, the creation information is returned
{
  "index" : {
    "aliases": {},"mappings" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"}}},"settings" : {
      "index" : {
        "creation_date" : "1623072136760"."number_of_shards" : "1"."number_of_replicas" : "1"."uuid" : "aatM8ty0QQuZWXo-99lvnA"."version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"}}}}Copy the code

1.2 Mapping Modification

  1. Maps can be added to existing indexes
  2. PUT /_mapping is required
  3. You only need to specify properties. "Mappings" is not required
PUT index/_mapping
{
  "properties" : {
    "id" : {
      "type" : "long"
    },
    "title" : {
      "type" : "text"}}}Copy the code
  1. Modify the results
{
  "acknowledged" : true
}
Copy the code

1.3 Mapping during addition

  1. If not specified, it will be automatically determined when data is added
  2. When a common index is created, no mapping information is available
{
  "index" : {
    "aliases": {},"mappings": {},"settings" : {
      "index" : {
        "creation_date" : "1623072418993"."number_of_shards" : "1"."number_of_replicas" : "1"."uuid" : "682iz4t_TRa-1Nc7bfm-ng"."version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"}}}}Copy the code
  1. Add data to the index
POST index/_doc/1
{
  "id": 3."title": "Huawei Phone"."category": "Huawei"
}
Copy the code
  1. The mapping of the corresponding values will be automatically identified
{
  "index" : {
    "aliases": {},"mappings" : {
      "properties" : {
        "category" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256}}},"id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256}}}}},"settings" : {
      "index" : {
        "creation_date" : "1623072418993"."number_of_shards" : "1"."number_of_replicas" : "1"."uuid" : "682iz4t_TRa-1Nc7bfm-ng"."version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"}}}}Copy the code

2. View mappings

  1. View index complete information, including mapping information
GET index
Copy the code
{
  "index" : {
    "aliases": {},"mappings" : {
      "properties" : {
        "category" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256}}},"id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256}}}}},"settings" : {
      "index" : {
        "creation_date" : "1623072418993"."number_of_shards" : "1"."number_of_replicas" : "1"."uuid" : "682iz4t_TRa-1Nc7bfm-ng"."version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"}}}}Copy the code
  1. To query index information
GET index/_mapping
Copy the code
{
  "index" : {
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256}}},"id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"."fields" : {
            "keyword" : {
              "type" : "keyword"."ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.