Black
Black

Reputation: 4644

Delete documents by prefix from elastic search

I have JSON documents with the below format and I would like to delete documents that have any id starting with users_.

{
"id": "users_1",
"name": "Adam"
}

One approach is to get all documents using wildcard then delete their IDs but this would require two requests. Is there a way to delete using a wildcard on a single field?

The solution I've tried is

requests.post(
    'https://<some-project>.us-central1.gcp.cloud.es.io/api/as/v1/engines/<project-name>/_delete_by_query?conflicts=proceed&pretty',
    headers=header, 
    data=json.dumps({
        "query": {
            "match_all": {"ids": "users_*"}
        }
    })
)

Upvotes: 0

Views: 1901

Answers (2)

apt-get_install_skill
apt-get_install_skill

Reputation: 2908

In general you should use the delete by query API (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html) which lets you (you guessed it!) delete documents that match a query rather than having to know their document id's in advance.

I'd recommend using the prefix-query (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html) for your particular use case. This however requires your id-field to be mapped as text in order to perform fulltext searches.

I hope I could help you.

Upvotes: 0

Joe - Check out my books
Joe - Check out my books

Reputation: 16905

Depending on your id mapping, the prefix query should work too:

.../_delete_by_query
{
  "query": {
    "prefix": {
      "id": {
        "value": "users_"
      }
    }
  }
}

Upvotes: 3

Related Questions