我想设置一个单个 json配置文件,可以通过python api到create indices加载和使用它,如下所示:
import elasticsearch
es = elasticsearch.Elasticsearch(hosts=[{'host': host, 'port': port}])
es.indices.create(index="my_special_index", body=es_config)
索引必须是每月time based rollover index。
这是用于创建静态索引:
es_config = {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"analysis": {
"analyzer": {
"default": {
"tokenizer": "standard",
"filter": ["lowercase", "stop", "asciifolding"]
}
}
}
},
"mappings": {
"alert": {
"properties": {
"id": {"type": "long"},
"title": {"type": "text"},
"user": {
"type": "nested",
"properties": {
"id": {"type": "long"},
"name": {"type": "text"}
}
}
}
}
}
}
我还没有弄清楚如何设置翻转群集,但是是否有可能在单个config +调用中完成所有操作?
答案 0 :(得分:0)
让我感到困惑的是,我认为过渡是一个被动的过程。但是,在调用elasticsearch.indices.rollover()
时,它将对别名/活动索引进行活动检查,以查看是否满足过渡条件;如果是,则它会增加活动索引。进行主动过渡看起来像这样:
给出问题的索引配置为index_config
es = elasticsearch.Elasticsearch(hosts=[{'host': host, 'port': port}])
index_name = "test-index-00001" # initial index name ends with number
alias_name = "test-index" # we will refer to the alias name from now on
doctype = "test"
# first create index and alias
es.indices.create(index=index_name, body=index_config)
# create alias
es.indices.put_alias(index=index_name, name=alias_name)
for i in range(5):
import datetime
doc_body = {"id": i, "title": str(datetime.datetime.now())}
es.index(index=alias_name, doc_type=doctype, body=doc_body)
time.sleep(0.1)
time.sleep(1) # es needs about a second
rollover_config = {"conditions": {"max_age": "10d", "max_docs": 3}}
rollover_ret = es.indices.rollover(alias=alias_name, body=rollover_config)
print(rollover_ret)
for i in range(5):
import datetime
doc_body = {"id": i, "title": str(datetime.datetime.now())}
es.index(index=alias_name, doc_type=doctype, body=doc_body)
time.sleep(0.1)
检查索引时,您会发现test-index-00001
包含五个,而不包含3个项目(如rollover_config声明的内容),因为仅在对这5个索引进行索引之后,才会进行滚动。 / p>
为了获得被动过渡,我们可以结合使用ElasticSearch的自动索引创建和模板系统。假设我们要为每个日历月创建一个新索引(对于复制粘贴测试,则创建第二个索引):
首先,使用别名和模板字段创建template config:
{
"aliases": {"alerts-index": {}},
"template": "alerts-index-*",
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"alert": {
"properties": {
"id": {"type": "long"},
"title": {"type": "text"}
}
}
}
}
然后使用后缀YYYYMM的初始索引名称创建模板:
es = elasticsearch.Elasticsearch(hosts=[{'host': host, 'port': port}])
alias_name = "test-index"
doctype = "test"
initial_index_name = "{}-{}".format(alias_name, datetime.datetime.now().strftime("%Y%m%S"))
es.indices.create(index=initial_index_name, body=index_config)
time.sleep(1)
for i in range(5):
doc_body = {"id": i, "title": str(datetime.datetime.now())}
indexname = "{}-{}".format(aliasname, datetime.datetime.now().strftime("%Y%m%S"))
rets = es.index(index=alias_name, doc_type=doctype, body=doc_body)
time.sleep(0.3)
在这里您应该看到您的文档基于时间以多个索引结尾。