【问题标题】:elasticsearch node.js delete all docs of an indexelasticsearch node.js 删除索引的所有文档
【发布时间】:2020-07-17 14:31:24
【问题描述】:

我正在尝试从索引中删除所有文档,而不通过 node.js API 删除索引本身。

我正在尝试使用 deleteByQuery,但如何指定索引中的所有文档?

【问题讨论】:

    标签: node.js elasticsearch dml elasticsearch-query


    【解决方案1】:

    您可以简单地使用 REST API 从您的索引中删除所有文档,因为您不需要使用 node.js API,您可以直接点击下面的 API。 More info on delete all docs examples and different options

    从索引中删除所有文档

    POST <your-index-name>/_delete_by_query
    {
      "query": {
        "match_all": {} --> this matches all docs in index, hence deletes all of them.
      }
    }
    

    Curl 格式作为 POST 请求,因此很难与 rest-client 一起使用

    curl -X POST "localhost:9200/<your-index-name>/_delete_by_query" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "match_all": {}
      }
    }
    '
    

    【讨论】:

    • 很多谢谢...我已经看过这种语法,但我不确定是否理解它。我必须对您提到的网址执行 POST 请求?
    • 是的,您需要将 替换为您的索引名称,在此之前您需要传递您的 es-server IP 和端口
    • 我的快递服务器在 8080 端口。在客户端,我使用 axios 发出请求。我正在尝试使用 url 执行 POST:'127.0.0.1:8080//_delete_by_query { "query": { "match_all": {} } }'。但是浏览器说“DOMException:无法在'XMLHttpRequest'上执行'open':无效的URL”。
    • 8080 是您的应用程序端口,如果您没有更改其默认端口,您的 elasticsearch 必须在9200 端口上运行
    • 是的,抱歉 url: '127.0.0.1:9200//_delete_by_query{"query":{"match_all":{}}}',同样的错误
    【解决方案2】:

    感谢@opster.... 我一直在寻找纯 node.js 解决方案,终于找到了。 我想发布它,因为我从未在网上看到过这段代码 sn-p。

    _

    在使用 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();
    
        });
    

    【讨论】:

    • 太好了,终于你可以使用node.js 来解决它了 :),顺便说一句,你是否使用 REST 或 curl 或 using my postman 集合尝试过,如果你能做到,那就太好了也支持我的回答,您应该使用 node.js 将您的接受标记为它:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多