From 205ae769bb48c4f2fa4685f87c0fb42822a81ba7 Mon Sep 17 00:00:00 2001 From: qinling0210 <88864212+qinling0210@users.noreply.github.com> Date: Tue, 3 Feb 2026 17:28:10 +0800 Subject: [PATCH] Fix "metadata table not exists" (#12949) ### What problem does this PR solve? Fix "metadata table not exists" when updating a meta data. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- api/db/services/doc_metadata_service.py | 31 ++++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/api/db/services/doc_metadata_service.py b/api/db/services/doc_metadata_service.py index bca9ef1e0..339d51c30 100644 --- a/api/db/services/doc_metadata_service.py +++ b/api/db/services/doc_metadata_service.py @@ -22,6 +22,7 @@ This is the SOLE source of truth for document metadata - MySQL meta_fields colum import json import logging +import re from copy import deepcopy from typing import Dict, List, Optional @@ -158,15 +159,24 @@ class DocMetadataService: limit: Max results to return Returns: - Search results from ES/Infinity + Search results from ES/Infinity, or empty list if index doesn't exist """ kb = Knowledgebase.get_by_id(kb_id) if not kb: - return None + return [] tenant_id = kb.tenant_id index_name = cls._get_doc_meta_index_name(tenant_id) + # Check if metadata index exists, create if it doesn't + if not settings.docStoreConn.index_exist(index_name, ""): + logging.debug(f"Metadata index {index_name} does not exist, creating it") + result = settings.docStoreConn.create_doc_meta_idx(index_name) + if result is False: + logging.error(f"Failed to create metadata index {index_name}") + return [] + logging.debug(f"Successfully created metadata index {index_name}") + if condition is None: condition = {"kb_id": kb_id} @@ -199,8 +209,6 @@ class DocMetadataService: Returns: Processed metadata with split values """ - import re - if not meta_fields or not isinstance(meta_fields, dict): return meta_fields @@ -288,6 +296,9 @@ class DocMetadataService: # Both ES and Infinity now use per-tenant metadata tables result = settings.docStoreConn.create_doc_meta_idx(index_name) logging.debug(f"Table creation result: {result}") + if result is False: + logging.error(f"Failed to create metadata table {index_name}") + return False else: logging.debug(f"Metadata table already exists: {index_name}") @@ -800,11 +811,17 @@ class DocMetadataService: Dictionary with metadata field statistics in format: { "field_name": { - "type": "string" | "number" | "list", + "type": "string" | "number" | "list" | "time", "values": [("value1", count1), ("value2", count2), ...] # sorted by count desc } } """ + def _is_time_string(value: str) -> bool: + """Check if a string value is an ISO 8601 datetime (e.g., '2026-02-03T00:00:00').""" + if not isinstance(value, str): + return False + return bool(re.match(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$', value)) + def _meta_value_type(value): """Determine the type of a metadata value.""" if value is None: @@ -815,6 +832,8 @@ class DocMetadataService: return "string" if isinstance(value, (int, float)): return "number" + if isinstance(value, str) and _is_time_string(value): + return "time" return "string" try: @@ -850,7 +869,7 @@ class DocMetadataService: # Aggregate value counts values = v if isinstance(v, list) else [v] for vv in values: - if not vv: + if vv is None: continue sv = str(vv) if k not in summary: