Python Elasticsearch客户端创建索引时设置映射。

48

我可以像这样在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)
4个回答

69

您可以在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)

3
如果我创建了索引后想要更新映射,该怎么办? - Gaurav Koradiya
Uascase是这样的,我首先对一些文档进行索引,然后在现有映射中添加更多属性(字段)。我通过在ES中使用“动态模板”来解决这个问题。感谢@Val。 - Gaurav Koradiya
@GauravKoradiya,请提出一个与您的用例相关的新问题。 - Val
如果您更改现有索引的映射,则几乎可以肯定需要重新索引。此外,看起来这个语法已经改变了。如果我找到了新的方法,我会在这里发布。 - szeitlin

35

嗯,使用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)

24

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

对于put_mapping方法,您必须提供document_type参数和"mappings"文档的内部部分,而不是提供完整的"mappings" 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"
            }
        }
    }
)

5
谢谢提供 put_mapping() 的示例,而不是 create()! - Ymartin
@James Murty。感谢您的示例。我已经成功运行了上面的代码,并且没有为字段指定日期类型出现任何错误。然而,当我执行GET my_index_name/_mapping/时,logdate日期字段在Kibana上是string类型,而不是date类型。 - sharp
1
请注意,doc_type在ElasticSearch 7中已被弃用,并将在版本8中删除。如果您每种文档类型使用一个索引,则可以省略它。 - Noumenon

0

另一个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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接