16c42787
tangwang
feat: implement r...
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
echo " LOG_LEVEL: $LOG_LEVEL"
echo " TESTING_MODE: $TESTING_MODE"
# 检查Elasticsearch是否运行
echo -e "${BLUE}检查Elasticsearch状态...${NC}"
if curl -s "$ES_HOST/_cluster/health" > /dev/null; then
echo -e "${GREEN}✓ Elasticsearch正在运行${NC}"
else
echo -e "${YELLOW}⚠ Elasticsearch未运行,尝试启动...${NC}"
# 尝试启动Elasticsearch(如果安装了本地版本)
if command -v elasticsearch &> /dev/null; then
echo -e "${BLUE}启动本地Elasticsearch...${NC}"
elasticsearch -d -p "$TEST_LOG_DIR/es.pid"
sleep 10
# 再次检查
if curl -s "$ES_HOST/_cluster/health" > /dev/null; then
echo -e "${GREEN}✓ Elasticsearch启动成功${NC}"
else
echo -e "${RED}✗ Elasticsearch启动失败${NC}"
echo -e "${YELLOW}请手动启动Elasticsearch或配置远程ES地址${NC}"
exit 1
fi
else
echo -e "${RED}✗ 未找到本地Elasticsearch${NC}"
echo -e "${YELLOW}请启动Elasticsearch服务或修改ES_HOST配置${NC}"
exit 1
fi
fi
# 等待Elasticsearch就绪
echo -e "${BLUE}等待Elasticsearch就绪...${NC}"
for i in {1..30}; do
if curl -s "$ES_HOST/_cluster/health?wait_for_status=yellow&timeout=1s" | grep -q '"status":"green\|yellow"'; then
echo -e "${GREEN}✓ Elasticsearch已就绪${NC}"
break
fi
if [ $i -eq 30 ]; then
echo -e "${RED}✗ Elasticsearch就绪超时${NC}"
exit 1
fi
sleep 1
done
# 创建测试索引(如果需要)
echo -e "${BLUE}准备测试数据索引...${NC}"
curl -X PUT "$ES_HOST/test_products" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"ansj": {
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ansj"
},
"brand_name": {
"type": "text",
"analyzer": "ansj"
},
"tags": {
"type": "text",
"analyzer": "ansj"
},
"price": {
"type": "double"
},
"category_id": {
"type": "integer"
},
"spu_id": {
"type": "keyword"
},
"text_embedding": {
"type": "dense_vector",
"dims": 1024
}
}
}
}' > /dev/null 2>&1 || echo -e "${YELLOW}索引可能已存在${NC}"
# 插入测试数据
echo -e "${BLUE}插入测试数据...${NC}"
curl -X POST "$ES_HOST/test_products/_bulk" -H 'Content-Type: application/json' -d'
{"index": {"_id": "1"}}
{"name": "红色连衣裙", "brand_name": "测试品牌", "tags": ["红色", "连衣裙", "女装"], "price": 299.0, "category_id": 1, "spu_id": "dress_001"}
{"index": {"_id": "2"}}
{"name": "蓝色连衣裙", "brand_name": "测试品牌", "tags": ["蓝色", "连衣裙", "女装"], "price": 399.0, "category_id": 1, "spu_id": "dress_002"}
{"index": {"_id": "3"}}
{"name": "智能手机", "brand_name": "科技品牌", "tags": ["智能", "手机", "数码"], "price": 2999.0, "category_id": 2, "spu_id": "phone_001"}
{"index": {"_id": "4"}}
{"name": "笔记本电脑", "brand_name": "科技品牌", "tags": ["笔记本", "电脑", "办公"], "price": 5999.0, "category_id": 3, "spu_id": "laptop_001"}
' > /dev/null 2>&1 || echo -e "${YELLOW}测试数据可能已存在${NC}"
# 启动测试API服务
echo -e "${BLUE}启动测试API服务...${NC}"
cd "$PROJECT_ROOT"
# 使用后台模式启动API
python -m api.app \
--host $API_HOST \
--port $API_PORT \
|