【问题标题】:Elasticsearch python create alias from jsonElasticsearch python从json创建别名
【发布时间】:2020-07-04 02:40:14
【问题描述】:

对弹性搜索非常陌生

尝试使用 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 文件。当我从这个文件中删除别名时,我收到另一个关于不支持的映射参数的错误,所以我不确定问题出在哪里,但想先解决别名问题

【问题讨论】:

    标签: python json elasticsearch


    【解决方案1】:

    正如接受的答案所说,我没有阅读文档。发布最终对我有用的东西以记录下来

    config.json 文件看起来像

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

    还有蟒蛇

    _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)
    

    【讨论】:

      【解决方案2】:

      正如elasticsearch-pysource code 所说,indices.createbody 参数不能包含任何别名:

      body:索引的配置(settingsmappings

      话虽如此,该库中有 other methods 可让您创建别名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-10
        • 2018-04-30
        • 1970-01-01
        • 2014-01-21
        • 2014-09-24
        • 2022-01-21
        相关资源
        最近更新 更多