简体   繁体   中英

Elasticsearch dynamic templating

I'm trying to get ES's (using I'm using ES v1.4.1) dynamic templating to work on my local machine and for some reason the "mappings" are not being included? I first create the index with a simple

PUT /bigtestindex (I'm using Sense plugin, not curl), 

then I follow that with

PUT /_template/bigtestindex_1
{
  "template": "big*",
  "settings": {
   "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1   
   },
   "analysis": {
      "filter": {
         "autocomplete_filter": {
            "type": "edge_ngram",
            "min_gram": "1",
            "max_gram": "20",
            "token_chars": [
              "letter",
              "digit"
              ]
         }
      },
      "analyzer": {
         "autocomplete": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
               "lowercase",
               "asciifolding",
               "autocomplete_filter"
            ]     
        },
        "whitespace_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "asciifolding"
            ]
          }
        }
       },
      "mappings": {
       "doc": {
          "properties": {
             "anchor": {
                "type": "string"
             },
             "boost": {
                "type": "string"
             },
             "content": {
                "type": "string",
                "analyzer": "whitespace_analyzer"
             },
             "digest": {
                "type": "string"
             },
             "host": {
                "type": "string"
             },
             "id": {
                "type": "string"
             },
             "metatag.description": {
                "type": "string",
                "analyzer": "standard"
             },
             "metatag.keywords": {
                "type": "string",
                "analyzer": "standard"
             },
             "segment": {
                "type": "string"
             },
             "title": {
             "type": "string",
             "index": "not_analyzed",
             "fields": {
                  "autocomplete": {
                  "type": "string",
                  "index_analyzer": "autocomplete",
                  "search_analyzer": "whitespace_analyzer"
                }
              }
            },
             "tstamp": {
                "type": "date",
                "format": "dateOptionalTime"
             },
             "url": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          }
        }
      }
    }

I'm not receiving any errors and the syntax looks to be correct but when I do something like

GET /bigtestindex/_mappings

in Sense, I get

    {
   "bigtestindex": {
      "mappings": {}
   }
}

It seems my Sense command was a bit off, should have been

PUT /bigtestindex/_template/bigtesttemplate_1 (creates index and template in one command

OR

PUT /_template/bigtesttemplate_1  (creates just template) thanks to @avr for pointing out my incorrect command (needed some fresh eyes)

instead of

PUT /bigtestindex/_template/bigtesttemplate_1

discovered this after trying several things, hth someone else

UPDATE As @avr stated, you do need to create the template first and then the index, you can create the index and the template in the same PUT statement as well.

It has everything to do with making sure your JSON is setup properly to match the right API endpoints. "mappings" should be separate from settings ie

{
"settings" {
...
},
 "mappings" {
...
 }
}

NOT

{
"settings" {
...
"mappings" {
 }
}

"mappings" should NOT be included in the `"settings"` - needs to be separate.

hth, anyone else having the same problem

First you need to create template then create index. You can find the same from elasticsearch documentation.

Templates are only applied at index creation time. Changing a template will have no impact on existing indices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM