The use of template for elasticsearch learning

Where is the starting point of es providing the template function? As a NoSQL database, ES does not set schema before data storage, that is, does not limit data fields. This is a good scenario for log type data. However, This practice of not setting the schema is sometimes too free. In some business scenarios, we need to pre-set the word segmentation method of the field. Of course, mappings can be used to solve this problem. But before the business access, you must inform and build an index first. , it's a bit unintelligent to think about. Is there a more flexible approach? templates

The use of templates is very simple, but if you want to use them well, you must have a whole set of processes:

  1. create template

    curl -XPUT localhost:9200/_template/template_1 -d '
    {
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replications":2
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
    }
    '
  2. View template

    curl -XGET localhost:9200/_template/template_1?pretty
  3. If there is an error in the creation of templates, delete the template

    curl -XDELETE localhost:9200/_template/template_1
  4. After the template is built, to test whether it meets the expectations, add a piece of data
    $ curl -XPUT 'http://localhost:9200/template_test/tweet/1' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
    }'
  5. Check the status of the cluster, if the shard replica is set incorrectly, the cluster may become yellow

    curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
  6. View index structure and data samples
    curl -XGET 'http://localhost:9200/twitter/_settings,_mappings?pretty'
    curl -XGET 'http://localhost:9200/template_test/tweet/1'

After these latter verifications, most problems can generally be avoided.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325333163&siteId=291194637