user1283776
user1283776

Reputation: 21764

Delete all documents with a certain property value?

I'm trying to delete all documents where a certain property has a certain value. The code below is my best attempt, but the ES API returns a parse error:

const userProperty = "couchDbOrigin";

client.deleteByQuery({
    index: "_all",
    body: { query: { bool: { must: [{ terms: { [userProperty]: user } }] } } }
});

What is wrong with this code?

Upvotes: 0

Views: 36

Answers (1)

Pierre Mallet
Pierre Mallet

Reputation: 7221

terms query expect criteria as an array :

so you should use :

client.deleteByQuery({
    index: "_all",
    body: { query: { bool: { must: [{ terms: { [userProperty]: [user] } }] } } }
});

But if you delete document for one user at a time, you should use a term query that expects a single value and can perform better

client.deleteByQuery({
        index: "_all",
        body: { query: { bool: { must: [{ term: { [userProperty]: user } }] } } }
    });

Upvotes: 1

Related Questions