-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
53 lines (45 loc) · 1.74 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from fastapi import FastAPI
from pymongo import MongoClient
from bson import ObjectId
import requests
import utils.db as DB
app = FastAPI()
# MeiliSearch 配置
MEILISEARCH_URL = "http://127.0.0.1:7700"
MEILISEARCH_INDEX = "paper_id"
# MASTER_KEY = "your_master_key"
# 连接 MongoDB
client = MongoClient(DB.MONGO_URI)
db = client[DB.DATABASE_NAME]
collection = db[DB.COLLECTION_NAME]
def serialize_object_id(document):
""" 将 MongoDB 的 ObjectId 转换为字符串 """
if isinstance(document, dict):
for key, value in document.items():
if isinstance(value, ObjectId):
document[key] = str(value)
elif isinstance(value, dict):
serialize_object_id(value)
elif isinstance(value, list):
for i in range(len(value)):
if isinstance(value[i], ObjectId):
value[i] = str(value[i])
elif isinstance(value[i], dict):
serialize_object_id(value[i])
return document
@app.get("/import")
async def import_data_to_meilisearch():
# 从 MongoDB 获取数据
documents = list(collection.find({})) # 你可以根据需求过滤数据
documents = [serialize_object_id(doc) for doc in documents]
# 将数据发送到 MeiliSearch
# headers = {"Authorization": f"Bearer {MASTER_KEY}"}
url = f"{MEILISEARCH_URL}/indexes/{MEILISEARCH_INDEX}/documents"
response = requests.post(url, json=documents)
if response.status_code == 202:
return {"status": "success", "message": "Data is being indexed."}
else:
return {"status": "error", "message": response.text}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)