Elasticsearch Python从JSON创建别名

时间:2020-03-23 15:45:35

标签: python json elasticsearch

elasticsearch的新手

尝试使用elasticsearch-py api从json初始化索引;使用这个简单的json文件只是了解其工作原理

{
  "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"
    }
  }
}

(读取一个json文件,解析出indexbody),然后是重要的python部分 es.indices.create(index=index, body=body)

但是我遇到一个错误:

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

我从ES文档中提取了映射和别名示例。 python与另一个没有别名的json文件一起使用。当我从该文件中删除别名时,出现另一个错误,提示不支持的映射参数,因此我不确定问题出在哪里,但想先解决别名问题

2 个答案:

答案 0 :(得分:1)

正如公认的答案所说,我未能阅读文档。发布最终对我有用的记录

config.json文件看起来像

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

还有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)

答案 1 :(得分:0)

正如elasticsearch-py的{​​{3}}所说,body的{​​{1}}参数不能包含任何别名:

body:索引(indices.createsettings)的配置

话虽如此,该库中有source code,使您可以创建别名。