Elastic search delete operation

2019-08-31 07:34发布

Am new to elastic search and struggling to delete an entry from my collection. I need a query similar to this one DELETE FROM message WHERE id='1323'" and created_user = "user@gmail.com".

Following are my elastic search query, when i execute this, its only deleting the particular id field, its not taking the second argument created_user. Please help me to solve this issue. Thanks

var created = "9ed8afe738aa63c28b66994cef1f83c6"
db.delete({
            index: 'outboxpro',
            type: 'message',
            id: req.body.post_id,
            created_user: created
        }, function (error, resp) {
            if (error) {
                return next(error);
            }
            var data = {};
            console.log('delete response',resp);
            if (resp.hits.successful < 1) {
            data = {status: false, message: 'NO POST FOUND TO DELETE', code: 400};
            res.send(data);
            } else {
                return next({status: true, message: 'POST DELETED', data: error, code: 500});
            }
});

//// EDIT I have tried deleteByQuery, following are my code

db.deleteByQuery({
    index: 'outboxpro',
    type: 'message',
    body:{
        "query": {
            "filtered": {
                "query": {
                    "match": {
                        "_id": {
                            "query": "Kal4AXi5R9G-IMx4GIKYMw"
                        }
                    }
                },
                "filter": {
                    "and": [
                        {
                            "term": {
                                "created_user": created 
                            }
                        }
                    ]
                }
            }
        }
    }
}, function (error, resp) {
    if (error) {
        return next(error);
    }
    console.log('post deleted');
});

2条回答
混吃等死
2楼-- · 2019-08-31 08:19

The delete API will do exactly what you want, just in a slightly round-about way.

What you'll have to do first is search for the documents you want to delete, so construct a search query that find all documents with the id of '1323' and a created_user of 'user@gmail.com'. From the returned documents you'll be able to retreive the document ID which you then need to pass through to the delete API.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html

查看更多
手持菜刀,她持情操
3楼-- · 2019-08-31 08:23

You can delete documents matching your query, using delete by query in elasticsearch.. Refer

http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/api-reference-1-0.html#api-deletebyquery-1-0

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

  db.deleteByQuery({
    index: 'outboxpro',
    type: 'message',
    body: {

        "query": {
            "filtered": {
                "query": {
                    "match": {
                        "_id": "Kal4AXi5R9G-IMx4GIKYMw"
                    }
                },
                "filter": {
                    "term": {
                        "created_user": "created"
                    }
                }
            }
        }
    },
    function (error, resp) {
        if (error) {
            return next(error);
        }
        console.log('post deleted');
    });
查看更多
登录 后发表回答