Node.js 弹性搜索批量索引超时错误!错误:30000ms后请求超时
最近,我想在旧的指数数据中滚动到新的月度指数。存储的数据从2015/07年开始至今。每个月几乎有30000条记录。按照中提供的滚动和批量方法完成代码,如下所示 文件main.coffee 文件es-test-promise.coffeeNode.js 弹性搜索批量索引超时错误!错误:30000ms后请求超时,node.js,elasticsearch,Node.js,elasticsearch,最近,我想在旧的指数数据中滚动到新的月度指数。存储的数据从2015/07年开始至今。每个月几乎有30000条记录。按照中提供的滚动和批量方法完成代码,如下所示 文件main.coffee 文件es-test-promise.coffee logger=需要“优雅的记录器” elasticsearch=需要“elasticsearch” config=require'config' setIndice=(客户端、前缀、索引、类型、年、月)-> allDocs=[] 计数=0 上一年=年+“” #前
logger=需要“优雅的记录器”
elasticsearch=需要“elasticsearch”
config=require'config'
setIndice=(客户端、前缀、索引、类型、年、月)->
allDocs=[]
计数=0
上一年=年+“”
#前导“0”的月份少于10个
prevMonth=(“0”+月).slice(-2)
下一个日期=新日期(年、月)
nextYear=nextDate.getFullYear().toString()
nextMonth=(“0”+(nextDate.getMonth()+1)).slice(-2)
minDate=“#{prevYear}-#{prevMonth}-01”
maxDate=“#{nextYear}-#{nextMonth}-01”
标记名称=“#{prefix}#{prevYear}#{prevMonth}”
q=
过滤:
过滤器:
范围:
创建数据:
gte:minDate
lt:maxDate
格式:“yyyy-MM-dd”
client.search
索引:索引
类型:类型
滚动:“1m”
正文:
查询:q
排序:[“文件”]
尺寸:1000
,回调=(错误,响应)->
console.log“标识名称1”,标识名称
如果出现错误,则返回logger.err err.stack
返回,除非响应。命中?总数
allDocs=[]
response.hits.hits.forEach(点击)->
行动=
索引:
_id:hit.\U id
allDocs.push(操作)
allDocs.push(命中源)
count=count+allDocs.length
客户机批量
索引:标识名称
类型:类型
正文:allDocs
,(分别为错误)->
console.log“标识名称2”,标识名称
如果出现错误,则返回logger.err err.stack
如果response.hits.total*2!=计数
client.scroll
scrollId:响应。\u scroll\u id
滚动:“1m”
,回调
其他的
logger.info“完成标记#{indice_name}”
setMonthlyIndices=(客户端、前缀、索引、类型、年份、月份)->
当前=新日期()
currentYear=current.getFullYear()
currentMonth=current.getMonth()+1
processYear=年或当前年
processMonth=month或0
processDate=新日期(processYear,processMonth)
currentDate=新日期(当前年、当前月)
processDate=新日期(2015年6月)
当前日期=新日期(2015年9月)
而processDate只需将requestTimeout放入您的配置中
e、 g:
您可以将无限
替换为所需的“ms”限制。在Elasticsearch 7.x中,默认超时为1m(一分钟)
Elasticsearch的官方go客户端可以设置此值
- 批量请求:
//WithTimeout-显式操作超时。
//
func(f Bulk)with timeout(v time.Duration)func(*BulkRequest){
返回函数(r*BulkRequest){
r、 超时=v
}
}
- 批量索引器:
esutil.BulkIndexerConfig{
// ...
超时:超时,
}
我们可以覆盖特定请求的请求超时。例如,client.bulk({index:index_name,requestTimeout:'50s',…})
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
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
new elasticsearch.Client({host:"localhost", requestTimeout : Infinity});