Fix linting

This commit is contained in:
yangdx 2025-09-20 15:30:27 +08:00
parent 8e2a1fa59e
commit 5da1df3b19

View file

@ -72,7 +72,7 @@ class Neo4JStorage(BaseGraphStorage):
def _is_chinese_text(self, text: str) -> bool: def _is_chinese_text(self, text: str) -> bool:
"""Check if text contains Chinese characters.""" """Check if text contains Chinese characters."""
chinese_pattern = re.compile(r'[\u4e00-\u9fff]+') chinese_pattern = re.compile(r"[\u4e00-\u9fff]+")
return bool(chinese_pattern.search(text)) return bool(chinese_pattern.search(text))
async def initialize(self): async def initialize(self):
@ -251,8 +251,10 @@ class Neo4JStorage(BaseGraphStorage):
if existing_index: if existing_index:
# Check if the existing index has CJK analyzer # Check if the existing index has CJK analyzer
index_config = existing_index.get("options", {}) index_config = existing_index.get("options", {})
current_analyzer = index_config.get("indexConfig", {}).get("fulltext.analyzer", "standard") current_analyzer = index_config.get("indexConfig", {}).get(
"fulltext.analyzer", "standard"
)
if current_analyzer != "cjk": if current_analyzer != "cjk":
logger.info( logger.info(
f"[{self.workspace}] Existing index '{index_name}' uses '{current_analyzer}' analyzer. " f"[{self.workspace}] Existing index '{index_name}' uses '{current_analyzer}' analyzer. "
@ -272,18 +274,22 @@ class Neo4JStorage(BaseGraphStorage):
drop_query = f"DROP INDEX {index_name}" drop_query = f"DROP INDEX {index_name}"
result = await session.run(drop_query) result = await session.run(drop_query)
await result.consume() await result.consume()
logger.info(f"[{self.workspace}] Dropped existing index '{index_name}'") logger.info(
f"[{self.workspace}] Dropped existing index '{index_name}'"
)
except Exception as drop_error: except Exception as drop_error:
logger.warning(f"[{self.workspace}] Failed to drop existing index: {str(drop_error)}") logger.warning(
f"[{self.workspace}] Failed to drop existing index: {str(drop_error)}"
)
# Create new index with CJK analyzer # Create new index with CJK analyzer
logger.info( logger.info(
f"[{self.workspace}] Creating full-text index '{index_name}' with Chinese tokenizer support." f"[{self.workspace}] Creating full-text index '{index_name}' with Chinese tokenizer support."
) )
try: try:
create_index_query = f""" create_index_query = f"""
CREATE FULLTEXT INDEX {index_name} CREATE FULLTEXT INDEX {index_name}
FOR (n:`{workspace_label}`) ON EACH [n.entity_id] FOR (n:`{workspace_label}`) ON EACH [n.entity_id]
OPTIONS {{ OPTIONS {{
indexConfig: {{ indexConfig: {{
@ -304,7 +310,7 @@ class Neo4JStorage(BaseGraphStorage):
"Falling back to standard analyzer." "Falling back to standard analyzer."
) )
create_index_query = f""" create_index_query = f"""
CREATE FULLTEXT INDEX {index_name} CREATE FULLTEXT INDEX {index_name}
FOR (n:`{workspace_label}`) ON EACH [n.entity_id] FOR (n:`{workspace_label}`) ON EACH [n.entity_id]
""" """
result = await session.run(create_index_query) result = await session.run(create_index_query)
@ -1708,7 +1714,7 @@ class Neo4JStorage(BaseGraphStorage):
LIMIT $limit LIMIT $limit
""" """
search_query = f"{query_strip}*" search_query = f"{query_strip}*"
result = await session.run( result = await session.run(
cypher_query, cypher_query,
index_name=index_name, index_name=index_name,
@ -1724,14 +1730,14 @@ class Neo4JStorage(BaseGraphStorage):
f"[{self.workspace}] Full-text search ({'Chinese' if is_chinese else 'Latin'}) for '{query}' returned {len(labels)} results (limit: {limit})" f"[{self.workspace}] Full-text search ({'Chinese' if is_chinese else 'Latin'}) for '{query}' returned {len(labels)} results (limit: {limit})"
) )
return labels return labels
except Exception as e: except Exception as e:
# If the full-text search fails, fall back to CONTAINS search # If the full-text search fails, fall back to CONTAINS search
logger.warning( logger.warning(
f"[{self.workspace}] Full-text search failed with error: {str(e)}. " f"[{self.workspace}] Full-text search failed with error: {str(e)}. "
"Falling back to slower, non-indexed search." "Falling back to slower, non-indexed search."
) )
# Enhanced fallback implementation # Enhanced fallback implementation
async with self._driver.session( async with self._driver.session(
database=self._DATABASE, default_access_mode="READ" database=self._DATABASE, default_access_mode="READ"
@ -1776,7 +1782,7 @@ class Neo4JStorage(BaseGraphStorage):
result = await session.run( result = await session.run(
cypher_query, query_lower=query_lower, limit=limit cypher_query, query_lower=query_lower, limit=limit
) )
labels = [record["label"] async for record in result] labels = [record["label"] async for record in result]
await result.consume() await result.consume()
logger.debug( logger.debug(