本文介绍了用Elasticsearch搜索/滚动异步等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用await
作为搜索的一部分,但是作为其+30000项,我必须使用scroll
.
I'm using await
as part of my search but as its +30000 items I have to use scroll
.
问题在于,在scroll
之前,搜索的初始部分已经完成,因此await
被触发并继续执行功能.我应该怎么做才能阻止这种情况?
The issue is that the initial part of the search is complete before the scroll
is so the await
fires and function carries on. what should I be doing to stop this?
var allTitles = [];
try {
await client.search({
index: 'myindex',
scroll: '30s',
source: ['title'],
q: 'title:test'
}, function getMoreUntilDone(error, response) {
response.hits.hits.forEach(function (hit) {
allTitles.push(hit._source.title);
});
if (response.hits.total > allTitles.length) {
client.scroll({
scrollId: response._scroll_id,
scroll: '30s'
}, getMoreUntilDone);
} else {
console.log('every "test" title', allTitles);
}
});
} catch (err) {
console.log(err)
}
推荐答案
因此,我已经对其进行了重新编写,以便在这里帮助任何需要它的人.
SO, I've re-written it so here it is to help anyone else who wants it.
var stuff = []
const q = {params}
const searchstuff = (q) => {
return new Promise((resolve, reject) => {
const get = x => {
stuff = stuff.concat(x.hits.hits)
if (x.hits.total > stuff.length) {
this.client.scroll({ scrollId: x._scrollId, scroll: '10s'}).then(get)
} else {
resolve(stuff)
}
}
this.client.search(q).then(get).catch(reject)
})
}
const search = await searchstuff(q)
if (search) console.log('Searched')
这篇关于用Elasticsearch搜索/滚动异步等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!