refac
This commit is contained in:
1 parent
3127f1b462
commit
d74ee34d91
2 files changed
+34
-11
No files matched your search
@@ -24,7 +24,6 @@ from sqlalchemy import (
|
|||||||
String,
|
String,
|
||||||
Text,
|
Text,
|
||||||
UniqueConstraint,
|
UniqueConstraint,
|
||||||
cast,
|
|
||||||
delete,
|
delete,
|
||||||
func,
|
func,
|
||||||
or_,
|
or_,
|
||||||
@@ -361,21 +360,41 @@ class KnowledgeTable:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Apply filename / content search
|
# Apply filename / content search
|
||||||
|
# Use ->> (as_string) instead of CAST(-> AS TEXT) to avoid
|
||||||
|
# PostgreSQL "invalid memory alloc request size" on large
|
||||||
|
# extracted-content rows (#24670).
|
||||||
|
content_text = File.data['content'].as_string()
|
||||||
|
search_filter = None
|
||||||
if filter:
|
if filter:
|
||||||
q = filter.get('query')
|
q = filter.get('query')
|
||||||
if q:
|
if q:
|
||||||
stmt = stmt.filter(
|
search_filter = or_(
|
||||||
or_(
|
File.filename.ilike(f'%{q}%'),
|
||||||
File.filename.ilike(f'%{q}%'),
|
content_text.ilike(f'%{q}%'),
|
||||||
cast(File.data['content'], Text).ilike(f'%{q}%'),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
stmt = stmt.filter(search_filter)
|
||||||
|
|
||||||
# Order by file changes
|
# Order by file changes
|
||||||
stmt = stmt.order_by(File.updated_at.desc(), File.id.asc())
|
stmt = stmt.order_by(File.updated_at.desc(), File.id.asc())
|
||||||
|
|
||||||
# Count before pagination
|
# Lightweight count: avoid selecting File.data and ORDER BY
|
||||||
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
|
count_stmt = (
|
||||||
|
select(func.count(File.id))
|
||||||
|
.select_from(File)
|
||||||
|
.join(KnowledgeFile, File.id == KnowledgeFile.file_id)
|
||||||
|
.join(Knowledge, KnowledgeFile.knowledge_id == Knowledge.id)
|
||||||
|
)
|
||||||
|
count_stmt = AccessGrants.has_permission_filter(
|
||||||
|
db=db,
|
||||||
|
query=count_stmt,
|
||||||
|
DocumentModel=Knowledge,
|
||||||
|
filter=filter,
|
||||||
|
resource_type='knowledge',
|
||||||
|
permission='read',
|
||||||
|
)
|
||||||
|
if search_filter is not None:
|
||||||
|
count_stmt = count_stmt.filter(search_filter)
|
||||||
|
count_result = await db.execute(count_stmt)
|
||||||
total = count_result.scalar()
|
total = count_result.scalar()
|
||||||
|
|
||||||
if skip:
|
if skip:
|
||||||
@@ -527,10 +546,14 @@ class KnowledgeTable:
|
|||||||
if filter:
|
if filter:
|
||||||
query_key = filter.get('query')
|
query_key = filter.get('query')
|
||||||
if query_key:
|
if query_key:
|
||||||
|
# Use ->> (as_string) instead of CAST(-> AS TEXT) to
|
||||||
|
# avoid PostgreSQL memory allocation failures on large
|
||||||
|
# content (#24670).
|
||||||
|
content_text = File.data['content'].as_string()
|
||||||
stmt = stmt.filter(
|
stmt = stmt.filter(
|
||||||
or_(
|
or_(
|
||||||
File.filename.ilike(f'%{query_key}%'),
|
File.filename.ilike(f'%{query_key}%'),
|
||||||
cast(File.data['content'], Text).ilike(f'%{query_key}%'),
|
content_text.ilike(f'%{query_key}%'),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from open_webui.models.access_grants import AccessGrantModel, AccessGrants
|
|||||||
from open_webui.models.groups import Groups
|
from open_webui.models.groups import Groups
|
||||||
from open_webui.models.users import User, UserModel, UserResponse, Users
|
from open_webui.models.users import User, UserModel, UserResponse, Users
|
||||||
from pydantic import BaseModel, ConfigDict, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
from sqlalchemy import JSON, BigInteger, Boolean, Column, ForeignKey, Text, cast, delete, func, or_, select, update
|
from sqlalchemy import JSON, BigInteger, Boolean, Column, ForeignKey, Text, delete, func, or_, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
####################
|
####################
|
||||||
@@ -180,7 +180,7 @@ class NoteTable:
|
|||||||
or_(
|
or_(
|
||||||
func.replace(func.replace(Note.title, '-', ''), ' ', '').ilike(f'%{word}%'),
|
func.replace(func.replace(Note.title, '-', ''), ' ', '').ilike(f'%{word}%'),
|
||||||
func.replace(
|
func.replace(
|
||||||
func.replace(cast(Note.data['content']['md'], Text), '-', ''),
|
func.replace(Note.data['content']['md'].as_string(), '-', ''),
|
||||||
' ',
|
' ',
|
||||||
'',
|
'',
|
||||||
).ilike(f'%{word}%'),
|
).ilike(f'%{word}%'),
|
||||||
|
|||||||
Reference in new issue
Block a user