{"record":{"id":"0f9b599ae2abe375","repo":"Tencent/WeKnora","slug":"failed-to-delete-by-query-w","errorCode":null,"errorMessage":"failed to delete by query: %w","messagePattern":"failed to delete by query: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/repository/retriever/elasticsearch/v8/repository.go","lineNumber":235,"sourceCode":"}\n\n// DeleteByChunkIDList removes documents from the index based on chunk IDs\n// Returns an error if the delete operation fails\nfunc (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunkIDList []string, dimension int, knowledgeType string) error {\n\tlog := logger.GetLogger(ctx)\n\tif len(chunkIDList) == 0 {\n\t\tlog.Warn(\"[Elasticsearch] Empty chunk ID list provided for deletion, skipping\")\n\t\treturn nil\n\t}\n\n\tlog.Infof(\"[Elasticsearch] Deleting indices by chunk IDs, count: %d\", len(chunkIDList))\n\t// Use DeleteByQuery to delete all documents matching the chunk IDs\n\t_, err := e.client.DeleteByQuery(e.index).Query(&types.Query{\n\t\tTerms: &types.TermsQuery{TermsQuery: map[string]types.TermsQueryField{e.idField(\"chunk_id\"): chunkIDList}},\n\t}).Do(ctx)\n\tif err != nil {\n\t\tlog.Errorf(\"[Elasticsearch] Failed to delete by chunk IDs: %v\", err)\n\t\treturn fmt.Errorf(\"failed to delete by query: %w\", err)\n\t}\n\n\tlog.Infof(\"[Elasticsearch] Successfully deleted documents by chunk IDs\")\n\treturn nil\n}\n\n// DeleteBySourceIDList removes documents from the index based on source IDs\n// Returns an error if the delete operation fails\nfunc (e *elasticsearchRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int, knowledgeType string) error {\n\tlog := logger.GetLogger(ctx)\n\tif len(sourceIDList) == 0 {\n\t\tlog.Warn(\"[Elasticsearch] Empty source ID list provided for deletion, skipping\")\n\t\treturn nil\n\t}\n\n\tlog.Infof(\"[Elasticsearch] Deleting indices by source IDs, count: %d\", len(sourceIDList))\n\t// Use DeleteByQuery to delete all documents matching the source IDs\n\t_, err := e.client.DeleteByQuery(e.index).Query(&types.Query{","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/repository/retriever/elasticsearch/v8/repository.go#L217-L253","documentation":"DeleteByChunkIDList issues an ES DeleteByQuery with a terms query on the chunk_id field and wraps any error from .Do(ctx) as 'failed to delete by query: %w'. This means the delete-by-query request itself failed (transport error, query parsing problem, index read-only, etc.), not that zero documents matched. The underlying ES error is preserved via %w for inspection.","triggerScenarios":"Calling DeleteByChunkIDList with an unreachable/misbehaving cluster, an empty or oversized chunkIDList producing an invalid terms query, a mapping issue where chunk_id is not indexed, or the index being write/read-blocked.","commonSituations":"Cluster restart or DNS failure during cleanup; chunk_id mapped as text without a keyword subfield so terms matching fails at query parse time; index under a read-only block due to disk watermarks; passing an empty list yielding a query the server rejects.","solutions":["Log/unwrap the wrapped error to see the concrete ES failure (status code, shard failures).","Verify the chunk_id field is indexed as keyword (or has .keyword subfield) in the index mapping.","Guard against calling with an empty chunkIDList in the caller; skip the call or return early when the list is empty.","Check cluster health and index blocks (disk watermarks, read_only settings) and lift them.","Retry after transient network failures; delete-by-query is safe to re-run."],"exampleFix":"// before\nif len(chunkIDs) == 0 {\n\t// still calls ES, may produce odd query\n}\n_ = retriever.DeleteByChunkIDList(ctx, chunkIDs)\n// after\nif len(chunkIDs) == 0 {\n\treturn nil\n}\nif err := retriever.DeleteByChunkIDList(ctx, chunkIDs); err != nil {\n\treturn fmt.Errorf(\"delete chunks by id: %w\", err)\n}","handlingStrategy":"validation","validationCode":"if len(chunkIDList) == 0 { return nil } // avoid issuing an empty terms delete-by-query","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call delete-by-query with an empty ID list; short-circuit in the caller.","Ensure ID fields (chunk_id) are mapped as keyword in the index template.","Chunk large ID lists (e.g. 512 per request) to keep queries well-formed.","Monitor cluster health and index read-only blocks in operations dashboards."],"tags":["elasticsearch","go","delete-by-query","network"],"backgroundTag":"elasticsearch-bulk-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}