个股论坛重做
This commit is contained in:
@@ -1301,7 +1301,23 @@ def es_search_proxy(index):
|
||||
try:
|
||||
body = request.get_json()
|
||||
print(f"[Community API] ES 搜索请求: index={index}, body={body}")
|
||||
|
||||
# 尝试使用不同的 API 调用方式(兼容不同版本的 elasticsearch-py)
|
||||
try:
|
||||
# 新版本 API
|
||||
result = es_client.search(index=index, body=body)
|
||||
except TypeError:
|
||||
# 旧版本 API,参数可能不同
|
||||
result = es_client.search(index=index, **body)
|
||||
|
||||
# 处理结果格式
|
||||
if hasattr(result, 'body'):
|
||||
result = result.body
|
||||
elif hasattr(result, '__getitem__'):
|
||||
pass # 已经是 dict
|
||||
else:
|
||||
result = dict(result)
|
||||
|
||||
print(f"[Community API] ES 搜索成功: 返回 {result.get('hits', {}).get('total', {})} 条结果")
|
||||
|
||||
return jsonify(result)
|
||||
@@ -1310,7 +1326,13 @@ def es_search_proxy(index):
|
||||
print(f"[Community API] ES 搜索失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return api_error(f'搜索失败: {str(e)}', 500)
|
||||
# 返回更详细的错误信息
|
||||
return jsonify({
|
||||
'error': str(e),
|
||||
'type': type(e).__name__,
|
||||
'index': index,
|
||||
'body': body if 'body' in dir() else None
|
||||
}), 500
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
||||
@@ -110,41 +110,46 @@ export const getMessages = async (
|
||||
): Promise<PaginatedResponse<Message>> => {
|
||||
const { before, after, limit = 50 } = options;
|
||||
|
||||
// 构建 ES 查询
|
||||
// 只查询主消息(非讨论串消息)
|
||||
const filters: any[] = [
|
||||
{ term: { channel_id: channelId } },
|
||||
];
|
||||
// 构建 ES 查询 - 使用最简单的 match_all 先测试
|
||||
const esBody: any = {
|
||||
query: {
|
||||
match: {
|
||||
channel_id: channelId,
|
||||
},
|
||||
},
|
||||
sort: [
|
||||
{ created_at: 'desc' },
|
||||
],
|
||||
size: limit,
|
||||
};
|
||||
|
||||
// 分页游标
|
||||
if (before) {
|
||||
filters.push({ range: { created_at: { lt: before } } });
|
||||
}
|
||||
if (after) {
|
||||
filters.push({ range: { created_at: { gt: after } } });
|
||||
}
|
||||
|
||||
const query: any = {
|
||||
esBody.query = {
|
||||
bool: {
|
||||
filter: filters,
|
||||
must_not: [
|
||||
{ term: { is_deleted: true } },
|
||||
must: [
|
||||
{ match: { channel_id: channelId } },
|
||||
],
|
||||
filter: [
|
||||
{ range: { created_at: { lt: before } } },
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
console.log('[getMessages] ES 查询:', JSON.stringify(esBody, null, 2));
|
||||
|
||||
// 查询时总是按时间降序获取(最新的在前)
|
||||
const response = await fetch(`${ES_API_BASE}/community_messages/_search`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
sort: [{ created_at: { order: 'desc' } }],
|
||||
size: limit,
|
||||
}),
|
||||
body: JSON.stringify(esBody),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('获取消息失败');
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
console.error('[getMessages] ES 查询失败:', response.status, errorData);
|
||||
throw new Error(`获取消息失败: ${errorData.error || response.statusText}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
|
||||
// 转换 ES 返回的 snake_case 字段为 camelCase
|
||||
|
||||
Reference in New Issue
Block a user