Elasticsearch: delete_by_metadata_field deletes other annotations whose id shares a token, and at most 10 documents

Open
#46,995 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@aniketatgithub is already working on this.

Since Nov 25, 2026.

  • #47191 by @aniketatgithub — open
  • #47201 by @Aseel012 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
elasticsearch, python
Domain
backend, databases

Research direction

Start in api/core/rag/datasource/vdb/elasticsearch/elasticsearch_vector.py at ElasticSearchVector.delete_by_metadata_field, which currently issues a match query and deletes the returned hits without a size; the Huawei Cloud backend has the same method. Read the annotation index mapping to see which metadata.* fields are declared keyword, then compare with the flow through update_annotation_to_index_task and delete_annotation_index_task. Done means the delete uses a term query against the field and its keyword subfield via delete_by_query, covered by a unit test plus the failing Elasticsearch e2e test. Note the reporter states a fix and PR are already in preparation.

Written by the indexing model from the issue text.

Description

Self Checks
  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.
Dify version

main (580416d, api 1.20.0)

Cloud or Self Hosted

Self Hosted (Source)### Steps to reproduce

Against docker.elastic.co/elasticsearch/elasticsearch:8.14.3 (the image in docker-compose), with the annotation documents the annotation tasks index (metadata with doc_id, annotation_id, app_id):

a = "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
b = "7c0e2f3a-1111-4b2d-8f00-0123456789ab"   # shares only the group "4b2d" with a
vector.create(texts=[annotation(a, app_id), annotation(b, app_id)] + [annotation(uuid4(), app_id) for _ in range(13)], embeddings=...)
vector.delete_by_metadata_field("annotation_id", a)   # update/delete of one annotation
vector.delete_by_metadata_field("app_id", app_id)     # enable_annotation_reply_task
✔️ Expected Behavior

Deleting by annotation_id removes only that annotation (14 left), and deleting by app_id removes all 15.

❌ Actual Behavior

Deleting annotation a also removes annotation b (13 left), and deleting by app_id removes only 10, leaving 3.

ElasticSearchVector.delete_by_metadata_field searches with {"match": {"metadata.<key>": value}} and deletes the hits. The index mapping only declares metadata.doc_id and metadata.document_id as keyword, so annotation_id and app_id get the dynamic mapping, an analyzed text field. A match query splits the UUID into tokens and matches any document that shares one; UUID4s share their third group (4 hex digits after the version digit, 4096 values) often, so in an app with a few hundred annotations, updating or deleting one annotation (update_annotation_to_index_task, delete_annotation_index_task) can silently remove others from the annotation reply index. The search also has no size, so it returns and deletes at most 10 hits.

The Huawei Cloud backend has the same delete_by_metadata_field.

I have a fix with tests (term queries on the field and its keyword subfield, through delete_by_query; unit test and an Elasticsearch e2e test that fails on main) and will open a PR.

Dominant language
Python
Stars
162k
Forks
25.6k
Avg merge
15h 9m
Merged PRs (30d)
606

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/dify

All issues in langgenius/dify

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.