python elasticsearch client set mappings during cr

2019-02-01 06:09发布

问题:

I can set mappings of index being created in curl command like this:

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

But I need to create that index with elasticsearch client in python and set mappings.. what is the way ? I tried somethings below but not work:

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)

回答1:

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)


回答2:

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)


回答3:

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


回答4:

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)