python elasticsearch client set mappings during create index
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)
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"
}
}
}
)
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)