python elasticsearch client set mappings during create index

随声附和 提交于 2019-11-29 22:56:39
Val

You can simply add the mapping in the create call like this:

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)

Well, there is easier way to do this with general python syntax:

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)

The Python API client can be a tricky to work with and it often requires you to provide the inner portions of JSON spec documentations to keyword arguments.

For the put_mapping method, instead of providing it the full "mappings" JSON document you must give it the document_type parameter and only the inner portion of the "mappings" document like so:

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

Another python client sample of how to raise field limit through create index

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!