我正在尝试从索引中删除所有文档,而不通过node.js API删除索引本身。
我正在尝试使用deleteByQuery,但如何指定索引中的所有文档?
感谢@opster。。。。我一直在寻找一个纯node.js的解决方案,终于找到了。我想发布它,因为我从未在网上看到过这个代码片段。
_
在使用axios的客户端上:
axios({
method: 'post',
url: '/empty_index',
}).then();
_
在服务器上我的快递路线
app.post( '/empty_index', function( req, res, next ) {
const client = new Client({
node: 'http://localhost:'+process.env.ELASTICSEARCH_PORT
});
client.deleteByQuery({
index: <your-index-name>,
body: {
query: {
match_all: {}
}
}
}, function (error, response) {
console.log(response);
});
return res.status( 200 ).send();
});
您只需使用REST API从索引中删除所有文档,因此您不需要使用node.js API,您可以直接点击API下方。更多关于删除所有文档示例和不同选项的信息
从索引中删除所有文档
POST <your-index-name>/_delete_by_query
{
"query": {
"match_all": {} --> this matches all docs in index, hence deletes all of them.
}
}
Curl格式作为POST请求,因此很难将其与rest客户端一起使用
curl -X POST "localhost:9200/<your-index-name>/_delete_by_query" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'