我可以在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)
答案 0 :(得分:43)
您只需在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)
答案 1 :(得分:25)
嗯,使用常规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)
答案 2 :(得分:12)
Python API客户端可能很难处理,它通常要求您将JSON规范文档的内部部分提供给关键字参数。
对于put_mapping
方法,您必须为其提供document_type
参数,而不是为其提供完整的“映射”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"
}
}
}
)
答案 3 :(得分:0)
另一个如何通过create index
提高字段限制的python客户端示例from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': config.elastic_host, 'port': config.elastic_port}])
raiseFieldLimit = '''
{
"index.mapping.total_fields.limit": 2000
}'''
es.indices.create(index='myindex', body=raiseFieldLimit)