我使用await
作为搜索的一部分,但作为其+30000项,我必须使用scroll
。
问题是搜索的初始部分在scroll
之前完成,因此await
火灾和功能继续进行。我该怎么做才能制止这个?
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)
}
答案 0 :(得分:0)
所以,我重写了它,所以这里是为了帮助其他想要它的人。
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')