Tencent/WeKnora · error

failed to delete by query: %w

Error message

failed to delete by query: %w

What it means

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.

Source

Thrown at internal/application/repository/retriever/elasticsearch/v8/repository.go:235

}

// DeleteByChunkIDList removes documents from the index based on chunk IDs
// Returns an error if the delete operation fails
func (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunkIDList []string, dimension int, knowledgeType string) error {
	log := logger.GetLogger(ctx)
	if len(chunkIDList) == 0 {
		log.Warn("[Elasticsearch] Empty chunk ID list provided for deletion, skipping")
		return nil
	}

	log.Infof("[Elasticsearch] Deleting indices by chunk IDs, count: %d", len(chunkIDList))
	// Use DeleteByQuery to delete all documents matching the chunk IDs
	_, err := e.client.DeleteByQuery(e.index).Query(&types.Query{
		Terms: &types.TermsQuery{TermsQuery: map[string]types.TermsQueryField{e.idField("chunk_id"): chunkIDList}},
	}).Do(ctx)
	if err != nil {
		log.Errorf("[Elasticsearch] Failed to delete by chunk IDs: %v", err)
		return fmt.Errorf("failed to delete by query: %w", err)
	}

	log.Infof("[Elasticsearch] Successfully deleted documents by chunk IDs")
	return nil
}

// DeleteBySourceIDList removes documents from the index based on source IDs
// Returns an error if the delete operation fails
func (e *elasticsearchRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int, knowledgeType string) error {
	log := logger.GetLogger(ctx)
	if len(sourceIDList) == 0 {
		log.Warn("[Elasticsearch] Empty source ID list provided for deletion, skipping")
		return nil
	}

	log.Infof("[Elasticsearch] Deleting indices by source IDs, count: %d", len(sourceIDList))
	// Use DeleteByQuery to delete all documents matching the source IDs
	_, err := e.client.DeleteByQuery(e.index).Query(&types.Query{

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Log/unwrap the wrapped error to see the concrete ES failure (status code, shard failures).
  2. Verify the chunk_id field is indexed as keyword (or has .keyword subfield) in the index mapping.
  3. Guard against calling with an empty chunkIDList in the caller; skip the call or return early when the list is empty.
  4. Check cluster health and index blocks (disk watermarks, read_only settings) and lift them.
  5. Retry after transient network failures; delete-by-query is safe to re-run.

Example fix

// before
if len(chunkIDs) == 0 {
	// still calls ES, may produce odd query
}
_ = retriever.DeleteByChunkIDList(ctx, chunkIDs)
// after
if len(chunkIDs) == 0 {
	return nil
}
if err := retriever.DeleteByChunkIDList(ctx, chunkIDs); err != nil {
	return fmt.Errorf("delete chunks by id: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

if len(chunkIDList) == 0 { return nil } // avoid issuing an empty terms delete-by-query

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/0f9b599ae2abe375. Report an issue: GitHub.