弹性搜索删除操作

时间:2014-03-12 08:46:12

标签: node.js elasticsearch elastic-beanstalk

对弹性搜索不熟悉,并努力从我的收藏中删除一个条目。 我需要一个类似于这个的查询 删除消息WHERE id ='1323'“和created_user =”user@gmail.com“。

以下是我的弹性搜索查询,当我执行此操作时,它只删除特定的id字段,而不是第二个参数created_user。请帮我解决这个问题。谢谢

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});
            }
});

////编辑 我尝试过deleteByQuery,以下是我的代码

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 个答案:

答案 0 :(得分:2)

您可以使用elasticsearch中的查询删除来删除与您的查询匹配的文档。请参阅

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');
    });

答案 1 :(得分:1)

删除API将完全按照您想要的方式执行,只需稍微进行一些。

首先要搜索要删除的文档,然后构建一个搜索查询,查找ID为“1323”且created_user为“user@gmail.com”的所有文档。从返回的文档中,您将能够检索您需要传递给删除API的文档ID。

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