OpenSearch: delete_by_metadata_field never deletes anything, so edited or deleted annotations stay in the annotation index

Open
#47,014 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Aadharbindal is already working on this.

Since Nov 24, 2026.

  • #47016 by @Aadharbindal — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
search

Research direction

Start in api/providers/vdb/vdb-opensearch/src/dify_vdb_opensearch/opensearch_vector.py, where delete_by_metadata_field, get_ids_by_metadata_field and delete_by_ids are defined, and check create_collection's field mapping for app_id and annotation_id. Trace the callers in update_annotation_to_index_task, delete_annotation_index_task and enable_annotation_reply_task. Done means deleting by annotation_id or app_id removes the matching documents, and unit tests cover it. A linked pull request already exists, so coordinate with it before starting.

Written by the indexing model from the issue text.

Description

🐞 bug
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 (8985a46)

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

With VECTOR_STORE=opensearch:

  1. Enable annotation reply on a chat app and add an annotation.
  2. Edit the annotation's question, or delete the annotation.
  3. Ask the old question again.

The annotation tasks remove the old vector with vector.delete_by_metadata_field("annotation_id", annotation_id) (update_annotation_to_index_task, delete_annotation_index_task) and vector.delete_by_metadata_field("app_id", app_id) (enable_annotation_reply_task).

✔️ Expected Behavior

The old annotation vector is removed, so the edited or deleted annotation no longer answers the old question.

❌ Actual Behavior

Nothing is deleted. The old vector stays in the OpenSearch index, so annotation reply keeps answering with the old or deleted annotation, and re-enabling annotation reply adds duplicates. For annotation_id and app_id this happens silently; for keyword fields the only trace is a Document with metadata doc_id ... not found for deletion warning per hit.

In api/providers/vdb/vdb-opensearch/src/dify_vdb_opensearch/opensearch_vector.py there are three problems:

  1. delete_by_metadata_field passes the result of get_ids_by_metadata_field to delete_by_ids. Those are OpenSearch _ids (uuid4().hex, set in add_texts), but delete_by_ids treats its argument as metadata doc_ids and looks each one up again with a metadata.doc_id term query. No document has an OpenSearch _id as its doc_id, so nothing is found and nothing is deleted. This affects every key, including document_id.
  2. get_ids_by_metadata_field uses {"term": {"metadata.<key>": value}}. create_collection only maps doc_id and document_id as keyword. app_id and annotation_id get the dynamic mapping (analyzed text plus a .keyword subfield), and a term query for a whole UUID never matches the analyzed tokens of a text field.
  3. The search has no size, so it returns at most 10 hits.

The Elasticsearch backend has a similar problem with a different cause (match instead of term), reported in #46995. This one is separate: it is the OpenSearch backend, and the _id/doc_id mix-up in (1) is specific to it.

I have a fix with unit tests 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.