Подтвердить что ты не робот

Python elasticsearch отображает набор клиентов во время создания индекса

Я могу установить отображения индекса, создаваемого в команде curl следующим образом:

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

Но мне нужно создать этот индекс с клиентом elasticsearch в python и установить сопоставления.. каким образом? Я пробовал что-то внизу, но не работал:

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)
4b9b3361

Ответ 1

Вы можете просто добавить отображение в вызов 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)

Ответ 2

Ну, есть более простой способ сделать это с помощью общего синтаксиса 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)

Ответ 3

Клиент API Python может быть сложным для работы, и часто требуется, чтобы вы предоставили внутренние части документации JSON для аргументов ключевого слова.

Для метода put_mapping вместо того, чтобы предоставить ему полный документ JSON "сопоставления", вы должны указать ему параметр document_type и только внутреннюю часть документа "сопоставления"

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

Еще один пример клиента 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)