If the Elasticsearch security features are enabled, you must have the manage index privilege for the target data stream, index, or alias. [7.9] If the request targets an index or index alias, you can also update its mapping with the create , create_doc , index , or write index privilege.
No, if you want to use a single index, you would need to define a single mapping that combines the fields of each document type. A better way might be to define separate indices on the same cluster for each document type.
Using the Mapping APIThese can be accessed from your dashboard by choosing Stack Settings > Elasticsearch . The next step is to write a a curl -x get command to retrieve the mappings from Elasticsearch. You will be returned with a json output similar to the below screenshot.
Create an index patterneditOpen the main menu, then click to Stack Management > Index Patterns. Click Create index pattern.
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"
}
}
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With