159 lines
5.1 KiB
Python
159 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
初始化社区 ES 索引
|
|
运行: python init_community_es.py
|
|
"""
|
|
|
|
from elasticsearch import Elasticsearch
|
|
|
|
# ES 客户端
|
|
es_client = Elasticsearch(
|
|
hosts=["http://222.128.1.157:19200"],
|
|
request_timeout=30
|
|
)
|
|
|
|
# 索引配置
|
|
INDICES = {
|
|
'community_messages': {
|
|
'mappings': {
|
|
'properties': {
|
|
'id': {'type': 'keyword'},
|
|
'channel_id': {'type': 'keyword'},
|
|
'thread_id': {'type': 'keyword'},
|
|
'author_id': {'type': 'keyword'},
|
|
'author_name': {'type': 'text', 'fields': {'keyword': {'type': 'keyword'}}},
|
|
'author_avatar': {'type': 'keyword'},
|
|
'content': {'type': 'text', 'analyzer': 'ik_max_word'},
|
|
'type': {'type': 'keyword'},
|
|
'mentioned_users': {'type': 'keyword'},
|
|
'mentioned_stocks': {'type': 'keyword'},
|
|
'mentioned_everyone': {'type': 'boolean'},
|
|
'reply_to': {
|
|
'type': 'object',
|
|
'properties': {
|
|
'messageId': {'type': 'keyword'},
|
|
'authorId': {'type': 'keyword'},
|
|
'authorName': {'type': 'text'},
|
|
'contentPreview': {'type': 'text'}
|
|
}
|
|
},
|
|
'reactions': {'type': 'object', 'enabled': False},
|
|
'reaction_count': {'type': 'integer'},
|
|
'is_pinned': {'type': 'boolean'},
|
|
'is_edited': {'type': 'boolean'},
|
|
'is_deleted': {'type': 'boolean'},
|
|
'created_at': {'type': 'date'},
|
|
'edited_at': {'type': 'date'}
|
|
}
|
|
},
|
|
'settings': {
|
|
'number_of_shards': 1,
|
|
'number_of_replicas': 0
|
|
}
|
|
},
|
|
'community_forum_posts': {
|
|
'mappings': {
|
|
'properties': {
|
|
'id': {'type': 'keyword'},
|
|
'channel_id': {'type': 'keyword'},
|
|
'author_id': {'type': 'keyword'},
|
|
'author_name': {'type': 'text', 'fields': {'keyword': {'type': 'keyword'}}},
|
|
'author_avatar': {'type': 'keyword'},
|
|
'title': {'type': 'text', 'analyzer': 'ik_max_word'},
|
|
'content': {'type': 'text', 'analyzer': 'ik_max_word'},
|
|
'tags': {'type': 'keyword'},
|
|
'stock_symbols': {'type': 'keyword'},
|
|
'is_pinned': {'type': 'boolean'},
|
|
'is_locked': {'type': 'boolean'},
|
|
'is_deleted': {'type': 'boolean'},
|
|
'reply_count': {'type': 'integer'},
|
|
'view_count': {'type': 'integer'},
|
|
'like_count': {'type': 'integer'},
|
|
'last_reply_at': {'type': 'date'},
|
|
'last_reply_by': {'type': 'keyword'},
|
|
'created_at': {'type': 'date'},
|
|
'updated_at': {'type': 'date'}
|
|
}
|
|
},
|
|
'settings': {
|
|
'number_of_shards': 1,
|
|
'number_of_replicas': 0
|
|
}
|
|
},
|
|
'community_forum_replies': {
|
|
'mappings': {
|
|
'properties': {
|
|
'id': {'type': 'keyword'},
|
|
'post_id': {'type': 'keyword'},
|
|
'channel_id': {'type': 'keyword'},
|
|
'author_id': {'type': 'keyword'},
|
|
'author_name': {'type': 'text', 'fields': {'keyword': {'type': 'keyword'}}},
|
|
'author_avatar': {'type': 'keyword'},
|
|
'content': {'type': 'text', 'analyzer': 'ik_max_word'},
|
|
'reply_to': {'type': 'object', 'enabled': False},
|
|
'reactions': {'type': 'object', 'enabled': False},
|
|
'like_count': {'type': 'integer'},
|
|
'is_solution': {'type': 'boolean'},
|
|
'is_deleted': {'type': 'boolean'},
|
|
'created_at': {'type': 'date'},
|
|
'edited_at': {'type': 'date'}
|
|
}
|
|
},
|
|
'settings': {
|
|
'number_of_shards': 1,
|
|
'number_of_replicas': 0
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
def create_index(index_name, config):
|
|
"""创建索引"""
|
|
if es_client.indices.exists(index=index_name):
|
|
print(f"✓ 索引 {index_name} 已存在")
|
|
return True
|
|
|
|
try:
|
|
es_client.indices.create(index=index_name, body=config)
|
|
print(f"✓ 创建索引 {index_name} 成功")
|
|
return True
|
|
except Exception as e:
|
|
print(f"✗ 创建索引 {index_name} 失败: {e}")
|
|
return False
|
|
|
|
|
|
def check_es_connection():
|
|
"""检查 ES 连接"""
|
|
try:
|
|
info = es_client.info()
|
|
print(f"✓ ES 连接成功: {info['cluster_name']} (v{info['version']['number']})")
|
|
return True
|
|
except Exception as e:
|
|
print(f"✗ ES 连接失败: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
print("=" * 50)
|
|
print("初始化社区 ES 索引")
|
|
print("=" * 50)
|
|
|
|
# 检查连接
|
|
if not check_es_connection():
|
|
return
|
|
|
|
print()
|
|
|
|
# 创建索引
|
|
for index_name, config in INDICES.items():
|
|
create_index(index_name, config)
|
|
|
|
print()
|
|
print("=" * 50)
|
|
print("初始化完成")
|
|
print("=" * 50)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|