如果我的记录每年可以扩展到 6.58 亿条记录,我会尝试找出最适合 elasticsearch 的解决方案。实际上,现在我的所有记录都有一个索引,包含 2 个分片和 0 个副本。此外,我注意到使用 356k 记录和一个索引,排序和搜索的工作速度比具有 1000 条记录的 365 个索引更快。问题是,如果我要对搜索进行排序并删除超过一年的记录或索引,那么以弹性方式存储 6.58 亿条记录的数据的最佳和更快的方法是什么?
Elasticsearch 6.2 版本,javascript api。
const defaultPageSize = 10
const indexTemplateSettings = {
number_of_shards: 2,
number_of_replicas : 0,
max_result_window: 1000000000,
'index.routing.allocation.enable': 'all',
}
const createClient = () =>
new elasticsearch.Client({
host: `${config.elastic.host}:${config.elastic.port}`,
log: config.elastic.logLevel,
httpAuth: `${config.elastic.userName}:${config.elastic.password}`,
})
export const get = ({index, skip = 0, pageSize = defaultPageSize, search, sort = {by: 'timestamp', direction: 'desc'}}) => new Promise(async resolve => {
try {
logger.silly(`getting data from elastic: index: ${index}, skip: ${skip}, pageSize: ${pageSize}`)
let client = createClient()
const sortSettings = {
order: `${sort.direction.toLowerCase()}`,
missing: '_last',
unmapped_type: 'long',
}
const params = {
from: skip,
size: pageSize || undefined,
index: `${index.toLowerCase()}`,
filter_path: 'hits.hits._source, hits.total',
body: {
query: {'match_all': {}},
sort: {
[`${sort.by}.keyword`]: sortSettings,
[`${sort.by}.seconds`]: sortSettings,
},
},
}
if (search) {
params.body.query = {
query_string : {
query: `*${search}* OR *${search}`,
analyze_wildcard: true,
},
}
}
await client.search(params,
(e, {hits: {hits: data = [], total: totalCount} = {hits: [], total: 0}} = {}) => {
logger.silly(`elastic searching completed. Result: contains ${totalCount} items`)
resolve({items: data.map(t => t._source), totalCount})
})
} catch (e) {
logger.error(e)
}
})
export const push = (message, type) => new Promise(async resolve => {
try {
let client = createClient()
let oneYearAgoTime = new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().substring(0, 10)
let indexCreationTime = new Date('2016-04-27').toISOString().substring(0, 10)
await client.deleteByQuery({
index: type.toLowerCase(),
body: {
query: {
range: {
'_timestampIndex' : {
lte: oneYearAgoTime,
},
},
},
},
} , (error, response) => {
logger.silly('Deleted of data completed', response)
})
await client.index({
index: type.toLowerCase(),
type,
body: {
...message,
_timestampIndex: indexCreationTime,
},
},
(error, response) => {
logger.silly('Pushing of data completed', response)
resolve(response)
})
} catch (e) {
logger.error(e)
}
})