面试题答案
一键面试主要步骤
- 确定业务需求:明确索引用途、数据量、读写模式等,以此确定合适的分片数、副本数及映射结构。
- 创建索引:使用Elasticsearch的
PUT
API创建索引,并在请求体中设置分片数、副本数。例如:
PUT /my_index
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
- 定义映射(mapping):可以在创建索引时同时定义映射,也可以后续通过
PUT mapping
API添加。例如:
PUT /my_index/_mapping
{
"properties" : {
"field1" : { "type" : "text" },
"field2" : { "type" : "keyword" },
"field3" : { "type" : "date" }
}
}
关键API
- 创建索引:
PUT /{index}
,通过请求体设置settings
参数确定分片数和副本数。 - 设置映射:
PUT /{index}/_mapping
,用于定义索引字段的数据类型等映射信息。
定期检查和更新索引设置
- 使用脚本:可以使用Elasticsearch提供的
_cluster/state
API获取当前索引设置,与期望设置对比。例如使用Python和elasticsearch
库:
from elasticsearch import Elasticsearch
es = Elasticsearch()
index_name ='my_index'
expected_settings = {
"number_of_shards": 3,
"number_of_replicas": 2
}
current_state = es.cluster.state(metric="indices", index=index_name)
current_settings = current_state['indices'][index_name]['settings']['index']
if (int(current_settings['number_of_shards']) != expected_settings["number_of_shards"] or
int(current_settings['number_of_replicas']) != expected_settings["number_of_replicas"]):
# 更新设置
es.indices.put_settings(
index=index_name,
body={
"number_of_shards": expected_settings["number_of_shards"],
"number_of_replicas": expected_settings["number_of_replicas"]
}
)
- 使用工具:可以利用
curator
工具,它可以根据配置文件定期检查和更新索引设置。例如配置文件中定义检查和更新操作:
actions:
1:
action: update_settings
description: "Update index settings"
options:
wait_for_completion: true
filters:
- filtertype: pattern
kind: prefix
value: my_index
settings:
index:
number_of_replicas: 2
number_of_shards: 3
然后通过命令行执行curator --config curator.yml action_file.yml
定期执行检查和更新。