0

Very new to elasticsearch

Trying to initialize an index from json using the elasticsearch-py api; using this simple json file to just understand how it's working

{
  "index": "index_name",
  "body": {
    "aliases": {
      "actions": [
        { "add": { "index": "index_name",  "alias": "alias1" } }
      ]
    },
    "mappings": {
      "properties": {
        "age":    { "type": "integer" },
        "email":  { "type": "keyword"  },
        "name":   { "type": "text"  }
      }
    },
    "settings": {
      "number_of_shards":   2,
      "number_of_replicas": 1,
      "index.refresh_interval": "120s"
    }
  }
}

(read a json file, parse out index and body) then the important python part es.indices.create(index=index, body=body)

But I'm getting an error:

elasticsearch.exceptions.TransportError: TransportError(500, 'null_pointer_exception', 'fieldName cannot be null')

I pulled the mapping and alias example from the ES docs. The python works with a different json file that doesn't have aliases in it. When I remove the aliases from this file I get another error about unsupported mapping parameters so I'm not sure where the problem is, but want to solve the alias issue first

2 Answers 2

1

As the accepted answer says, I failed to read the docs. Posting what ended up working for me for a record

a config.json file would look like

"index": "index_1",
"aliases": ["alias1", "alias2"],
"body": {
  "mappings": {...},
  "settings": {...}
}

And the python

_json = json.load(file)
index = _json['index']
aliases = _json['aliases']
body = _json['body']

es.indices.create(index=index, body=body)
if len(aliases) > 0:
       for alias in aliases:
           es.indices.put_alias(index=index, name=alias)
Sign up to request clarification or add additional context in comments.

Comments

0

As the source code of elasticsearch-py says, the body argument of indices.create cannot include any aliases:

body: The configuration for the index (settings and mappings)

With that being said, there are other methods in that library that enable you to create aliases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.