我正在使用nodejs并在elasticsearch中查询。我想要符合条件的输出。但是,由于弹性搜索中的max_result_window设置在弹性搜索中默认为10,000,因此我无法输出超过10,000条记录。
在谷歌搜索如何获得超过10,000条记录,我找到了2种方法:
1.使用:
curl -XPUT "http://localhost:9200/my_index/_settings" -d '{ "index" : { "max_result_window" : 500000 } }'
我无法运行上述命令。有人可以说在nodejs中运行上面的方法。
答案 0 :(得分:0)
async function getResultsFromElastic() {
let responseAll = {};
responseAll["hits"] = {};
responseAll.hits.hits = [];
const responseQueue = [];
searchQuery = {
index: 'test',
type: 'records',
body: {
query: {
"match_all": {}
}
}
}
searchQuery.scroll='10s';
searchQuery.size=10000;
responseQueue.push(await esclient.search(searchQuery));
while (responseQueue.length) {
const response = responseQueue.shift();
responseAll.hits.hits = responseAll.hits.hits.concat(response.hits.hits);
if (response.hits.total == responseAll.hits.hits.length) {
break;
}
// get the next response if there are more to fetch
responseQueue.push(
await esclient.scroll({
scrollId: response._scroll_id,
scroll: '30s'
})
);
}
return responseAll;
}
async function getLocationsFromElastic(searchQuery) {
let responseAll = {};
responseAll["hits"] = {};
responseAll.hits.hits = [];
searchQuery.from = searchQuery.from || 0;
searchQuery.size = searchQuery.size || 10000; //If there are more than 10k results from Elastic, the elastic client throws an error.
while( true ) {
let response = await esclient.search(searchQuery);
responseAll.hits.hits = responseAll.hits.hits.concat(response.hits.hits);
if (response.hits.total == responseAll.hits.hits.length) {
break;
} else {
searchQuery.from += 10000;
}
}
return responseAll;
}