最近,我想将旧索引数据滚动到新的基于月度的索引。存储的数据从2015/07开始至今。每个月几乎有30,000条记录。按照2.2 API中提供的滚动和批量方法,我完成以下代码。
file main.coffee
logger = require 'graceful-logger'
elasticsearch = require 'elasticsearch'
setMonthlyIndices = require './es-test-promise'
client = new elasticsearch.Client
host:
host: 'localhost'
port: 9200
protocol: 'http'
setMonthlyIndices client, 'es_test_messages', 'talk_messages_v2', 'messages', 2015, 6
file es-test-promise.coffee
logger = require 'graceful-logger'
elasticsearch = require 'elasticsearch'
config = require 'config'
setIndice = (client, prefix, index, type, year, month) ->
allDocs = []
count = 0
prevYear = year + ''
# with leading '0' for month less than 10
prevMonth = ("0" + month).slice(-2)
nextDate = new Date(year, month)
nextYear = nextDate.getFullYear().toString()
nextMonth = ("0" + (nextDate.getMonth()+1)).slice(-2)
minDate = "#{prevYear}-#{prevMonth}-01"
maxDate = "#{nextYear}-#{nextMonth}-01"
indice_name = "#{prefix}_#{prevYear}_#{prevMonth}"
q =
filtered:
filter:
range:
createdAt:
gte: minDate
lt: maxDate
format: "yyyy-MM-dd"
client.search
index: index
type: type
scroll: '1m'
body:
query: q
sort: ['_doc']
size: 1000
, callback = (err, response) ->
console.log "indice_name 1", indice_name
return logger.err err.stack if err
return unless response.hits?.total
allDocs = []
response.hits.hits.forEach (hit)->
action =
index:
_id: hit._id
allDocs.push(action)
allDocs.push(hit._source)
count = count + allDocs.length
client.bulk
index: indice_name
type: type
body: allDocs
, (err, resp) ->
console.log "indice_name 2", indice_name
return logger.err err.stack if err
if response.hits.total *2 != count
client.scroll
scrollId: response._scroll_id
scroll: '1m'
, callback
else
logger.info "Finish indicing #{indice_name}"
setMonthlyIndices = (client, prefix, index, type, year, month) ->
current = new Date()
currentYear = current.getFullYear()
currentMonth = current.getMonth() + 1
processYear = year or currentYear
processMonth = month or 0
processDate = new Date(processYear, processMonth)
currentDate = new Date(currentYear, currentMonth)
processDate = new Date(2015, 6)
currentDate = new Date(2015, 9)
while processDate <= currentDate
year = processDate.getFullYear()
month = processDate.getMonth() + 1
setIndice(client, prefix, index, type, year, month)
processDate.setMonth(processDate.getMonth() + 1)
module.exports = setMonthlyIndices
我想知道是否是由于打开了太多客户端请求,因为在文件 es-test-promise.coffee 中,所有这些搜索请求都是同时运行的。这只是一个猜测,然后我也尝试用 promise 来实现,以确保请求可以逐个执行。最后,我无法弄清楚并放弃。
你有什么建议,我认为这应该是源问题,但我不知道在哪里检查......
答案 0 :(得分:1)
只需将requestTimeout放入您的配置。
例如:
<button ng-click="myFunction()">Click</button>
$scope.myFunction = function() {
return DOM;
}
您可以将'{1}}替换为'ms'中所需的限制。
答案 1 :(得分:0)
在 Elasticsearch 7.x 中,默认超时时间为 1m(一分钟)here。
Elasticsearch 的官方 go 客户端 go-elasticsearch 有设置这个值的方法。
// WithTimeout - explicit operation timeout.
//
func (f Bulk) WithTimeout(v time.Duration) func(*BulkRequest) {
return func(r *BulkRequest) {
r.Timeout = v
}
}
esutil.BulkIndexerConfig {
// ...
Timeout: timeout,
}