【问题标题】:python elasticsearch client set mappings during create indexpython elasticsearch客户端在创建索引期间设置映射
【发布时间】:2020-01-29 23:05:47
【问题描述】:

我可以像这样设置在 curl 命令中创建的索引的映射:

{  
  "mappings":{  
    "logs_june":{  
      "_timestamp":{  
        "enabled":"true"
      },
      "properties":{  
        "logdate":{  
          "type":"date",
          "format":"dd/MM/yyy HH:mm:ss"
        }
      }
    }
  }
}

但是我需要在 python 中使用 elasticsearch 客户端创建该索引并设置映射.. 什么是方法?我在下面尝试了一些方法但不起作用:

self.elastic_con = Elasticsearch([host], verify_certs=True)
self.elastic_con.indices.create(index="accesslog", ignore=400)
params = "{\"mappings\":{\"logs_june\":{\"_timestamp\": {\"enabled\": \"true\"},\"properties\":{\"logdate\":{\"type\":\"date\",\"format\":\"dd/MM/yyy HH:mm:ss\"}}}}}"
self.elastic_con.indices.put_mapping(index="accesslog",body=params)

【问题讨论】:

    标签: python pyelasticsearch elasticsearch-py


    【解决方案1】:

    您可以像这样在create 调用中简单地添加映射:

    from elasticsearch import Elasticsearch
    
    self.elastic_con = Elasticsearch([host], verify_certs=True)
    mapping = '''
    {  
      "mappings":{  
        "logs_june":{  
          "_timestamp":{  
            "enabled":"true"
          },
          "properties":{  
            "logdate":{  
              "type":"date",
              "format":"dd/MM/yyy HH:mm:ss"
            }
          }
        }
      }
    }'''
    self.elastic_con.indices.create(index='test-index', ignore=400, body=mapping)
    

    【讨论】:

    • 如果我想在创建索引后更新映射怎么办?
    • Uascase 是这样的,我首先索引一些文档,索引后我需要在现有映射的映射中添加更多属性(字段)。我已经通过在 ES 中做“动态模板”来解决它。谢谢@Val。
    • @GauravKoradiya 请针对您的用例提出一个新问题
    • 如果您要更改现有索引上的映射,您肯定必须重新索引。而且看起来这种语法已经改变了。如果我想出新的方法,我会在这里发布。
    【解决方案2】:

    嗯,有更简单的方法可以使用通用的 python 语法来做到这一点:

    from elasticsearch import Elasticsearch
    # conntect es
    es = Elasticsearch([{'host': config.elastic_host, 'port': config.elastic_port}])
    # delete index if exists
    if es.indices.exists(config.elastic_urls_index):
        es.indices.delete(index=config.elastic_urls_index)
    # index settings
    settings = {
        "settings": {
            "number_of_shards": 1,
            "number_of_replicas": 0
        },
        "mappings": {
            "urls": {
                "properties": {
                    "url": {
                        "type": "string"
                    }
                }
            }
         }
    }
    # create index
    es.indices.create(index=config.elastic_urls_index, ignore=400, body=settings)
    

    【讨论】:

      【解决方案3】:

      Python API 客户端可能很难使用,它通常需要您将 JSON 规范文档的内部部分提供给关键字参数。

      对于put_mapping 方法,您必须为其提供document_type 参数并且只提供“映射”文档的inner 部分,而不是为其提供完整的“映射”JSON 文档,如下所示:

      self.client.indices.put_mapping(
          index="accesslog",
          doc_type="logs_june",
          body={
              "_timestamp": {  
                  "enabled":"true"
              },
              "properties": {  
                  "logdate": {  
                      "type":"date",
                      "format":"dd/MM/yyy HH:mm:ss"
                  }
              }
          }
      )
      

      【讨论】:

      • 感谢您提供 put_mapping() 而不是 create() 的示例!
      • @James Murty。谢谢你的例子。我能够运行上面的代码,并且在为字段指定日期类型时没有收到任何错误。但是,当我执行 GET my_index_name/_mapping/ 时,logdate 日期字段是 string 而不是 Kibana 上的 date
      • 请注意,doc_type 在 ElasticSearch 7 中已弃用,并将在版本 8 中删除。如果您对每种文档类型使用一个索引,则可以将其关闭。
      【解决方案4】:

      另一个如何通过创建索引提高字段限制的python客户端示例

      from elasticsearch import Elasticsearch
      
      es = Elasticsearch([{'host': config.elastic_host, 'port': 
      config.elastic_port}])
      settings = {
          'settings': {
              'index.mapping.total_fields.limit': 100000
          }
      }
      
      es.indices.create(index='myindex', body=settings)
      

      【讨论】:

        猜你喜欢
        • 2020-09-23
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-02
        相关资源
        最近更新 更多