官术网_书友最值得收藏!

  • ElasticSearch Cookbook
  • Alberto Paro
  • 408字
  • 2021-04-02 10:09:59

Using explicit mapping creation

If we consider the index as a database in the SQL world, the mapping is similar to the table definition.

Getting ready

You need a working ElasticSearch cluster, a test index (refer to the Creating an index recipe in Chapter 4, Standard Operations), and basic knowledge of JSON.

How to do it...

For explicit mapping creation, we will perform the following steps:

  1. You can explicitly create a mapping by adding a new element in ElasticSearch.

    On bash:

    #create an index
    curl -XPUT http://127.0.0.1:9200/test
    #{"ok":true,"acknowledged":true}
    
    #put a record
    curl -XPUT http://127.0.0.1:9200/test/mytype/1 -d '{"name":"Paul", "age":35}'
    # {"ok":true,"_index":"test","_type":"mytype","_id":"1","_version":1}
    
    #get the mapping and pretty print it
    curl –XGET http://127.0.0.1:9200/test/mytype/_mapping?pretty=true
    
  2. The result mapping auto-created by ElasticSearch should be as follows:
    {
      "mytype" : {
        "properties" : {
          "age" : {
            "type" : "long"
          },
          "name" : {
            "type" : "string"
          }
        }
      }
    }

How it works...

The first command line creates the test index, decides where to put the document and the type/mapping.

The second command line inserts a document in the index. We'll see the index creation and record indexing in the next chapter.

During the document index phase, ElasticSearch controls if the mytype type exists, otherwise, it creates an empty one.

ElasticSearch reads all the default properties for the field of the mapping and starts to process them as follows:

  • If the field is already present in the mapping and the value of the field is valid, it processes the field
  • If the field is already present in the mapping, but the value of the field is of a different type, it processes the field as a new type, migrating the previous field format in a multifield
  • If the field is not present, it tries to auto-detect the type of field, it updates the mappings (or adding a new field mapping or converting the actual field in a multifield

There's more...

All the document types go in the same Lucene index, so there isn't full separation between them.

Every document has a unique identifier called UID, stored in the special uid field of the document. It's calculated by adding the type of document to the _id field.

The _id field can be provided at index time or can be assigned automatically by ElasticSearch if it's missing.

ElasticSearch transparently manages the propagation of a type mapping to all the nodes of a cluster, so that all the shards are aligned to processes of that particular type.

主站蜘蛛池模板: 彝良县| 万安县| 富川| 金川县| 罗平县| 乐山市| 陆丰市| 丽江市| 临湘市| 高清| 兰西县| 嘉祥县| 辽宁省| 卓尼县| 乐平市| 时尚| 泸州市| 禹州市| 大名县| 勐海县| 宿迁市| 夏河县| 太康县| 布尔津县| 越西县| 舟曲县| 滨州市| 波密县| 无极县| 寿宁县| 酉阳| 乌拉特中旗| 沿河| 卢湾区| 余干县| 绥芬河市| 东阳市| 巴南区| 昆明市| 平江县| 宣武区|