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)
This commit is contained in:
qinling0210
2026-02-03 17:28:10 +08:00
committed by GitHub
parent ff7afcbe5f
commit 205ae769bb

View File

@ -22,6 +22,7 @@ This is the SOLE source of truth for document metadata - MySQL meta_fields colum
import json import json
import logging import logging
import re
from copy import deepcopy from copy import deepcopy
from typing import Dict, List, Optional from typing import Dict, List, Optional
@ -158,15 +159,24 @@ class DocMetadataService:
limit: Max results to return limit: Max results to return
Returns: 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) kb = Knowledgebase.get_by_id(kb_id)
if not kb: if not kb:
return None return []
tenant_id = kb.tenant_id tenant_id = kb.tenant_id
index_name = cls._get_doc_meta_index_name(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: if condition is None:
condition = {"kb_id": kb_id} condition = {"kb_id": kb_id}
@ -199,8 +209,6 @@ class DocMetadataService:
Returns: Returns:
Processed metadata with split values Processed metadata with split values
""" """
import re
if not meta_fields or not isinstance(meta_fields, dict): if not meta_fields or not isinstance(meta_fields, dict):
return meta_fields return meta_fields
@ -288,6 +296,9 @@ class DocMetadataService:
# Both ES and Infinity now use per-tenant metadata tables # Both ES and Infinity now use per-tenant metadata tables
result = settings.docStoreConn.create_doc_meta_idx(index_name) result = settings.docStoreConn.create_doc_meta_idx(index_name)
logging.debug(f"Table creation result: {result}") logging.debug(f"Table creation result: {result}")
if result is False:
logging.error(f"Failed to create metadata table {index_name}")
return False
else: else:
logging.debug(f"Metadata table already exists: {index_name}") logging.debug(f"Metadata table already exists: {index_name}")
@ -800,11 +811,17 @@ class DocMetadataService:
Dictionary with metadata field statistics in format: Dictionary with metadata field statistics in format:
{ {
"field_name": { "field_name": {
"type": "string" | "number" | "list", "type": "string" | "number" | "list" | "time",
"values": [("value1", count1), ("value2", count2), ...] # sorted by count desc "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): def _meta_value_type(value):
"""Determine the type of a metadata value.""" """Determine the type of a metadata value."""
if value is None: if value is None:
@ -815,6 +832,8 @@ class DocMetadataService:
return "string" return "string"
if isinstance(value, (int, float)): if isinstance(value, (int, float)):
return "number" return "number"
if isinstance(value, str) and _is_time_string(value):
return "time"
return "string" return "string"
try: try:
@ -850,7 +869,7 @@ class DocMetadataService:
# Aggregate value counts # Aggregate value counts
values = v if isinstance(v, list) else [v] values = v if isinstance(v, list) else [v]
for vv in values: for vv in values:
if not vv: if vv is None:
continue continue
sv = str(vv) sv = str(vv)
if k not in summary: if k not in summary: