1
votes

I wrote the following code to set a template for new indexes:

from elasticsearch import Elasticsearch

doc = {
   "template": "te*",
  "settings": {
     "number_of_shards": 1
  },
  "mappings": {
     "type1": {
      "_source": {
         "enabled": "false"
       },
      "properties": {
        "host_name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
 }

 es = Elasticsearch([{'host': "127.0.0.1", "port": 9200}])
 es.put_template("f_1", body=doc)

The following error looks like some syntax error i can't solve:

launch(file, globals, locals) # execute the script
File "/Users/D/workspace/es_index_template/create_schema.py", line 31, in es.put_template("freshmilk_1", body=doc) File "/Users/D/.virtualenvs/es_index_template/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped return func(*args, params=params, **kwargs) File "/Users/D/.virtualenvs/es_index_template/lib/python2.7/site-packages/elasticsearch/client/init.py", line 1152, in put_template 'template', id), params=params, body=body) File "/Users/D/.virtualenvs/es_index_template/lib/python2.7/site-packages/elasticsearch/transport.py", line 329, in perform_request status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout) File "/Users/D/.virtualenvs/es_index_template/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 109, in perform_request self._raise_error(response.status, raw_data) File "/Users/D/.virtualenvs/es_index_template/lib/python2.7/site-packages/elasticsearch/connection/base.py",

line 108, in _raise_error raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) elasticsearch.exceptions.TransportError: TransportError(500, u'script_parse_exception', u'unexpected field [mappings]')

Has someone an idea why he fails with the mappings?

1
What version of ES are you using?Loïc
@Loïc: "2.1.1" Thanks!Jurudocs

1 Answers

1
votes

I took the put_template method as a query template but its meant to be a index template: Library usage should look like this:

es = Elasticsearch([{'host': "127.0.0.1", "port": 9200}])
IndicesClient(es).put_template(name="f_1", body=request_body)