0

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');
});
Dibish
  • 9,133
  • 22
  • 64
  • 106
  • Could you give your mapping and also the answer that you got from elastic search ? –  Mar 12 '14 at 13:35

2 Answers2

2

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');
    });
BlackPOP
  • 5,657
  • 2
  • 33
  • 49
  • Thanks for your comment. I tried db.deleteByQuery, i didn't get any error by running the query. But data not deleted – Dibish Mar 13 '14 at 09:03
  • which version of ES you are using?? – BlackPOP Mar 13 '14 at 10:00
  • It should be deleted.i don know which step you did wrong..pls post the query u used..! – BlackPOP Mar 13 '14 at 10:08
  • i have edited my question to include deleteByQuery code, can u please check it – Dibish Mar 13 '14 at 12:51
  • I have tested the query only in sense, i got error "SearchPhaseExecutionException", i don't know is it about query part. I have tested simple match query with deleteByQuery, but its also not working for me. – Dibish Mar 13 '14 at 13:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49661/discussion-between-sidharthan-and-dibish) – BlackPOP Mar 13 '14 at 13:33
  • i don know why your mentioning _id field in query [_id field in unique in ES].. if you mention then only one doc that matches _id ly deleted.. Remove the _id field and try again.. – BlackPOP Mar 13 '14 at 13:38
  • 2
    deleteByQuery was removed in more recent versions of API (testing on 2.0) – Voy Nov 25 '15 at 11:28
1

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

Garry Welding
  • 3,599
  • 1
  • 29
  • 46