Commit a406638e284c90f58438fdec5ebbd68cbe996a6f

Authored by tangwang
1 parent 3a950275

up

@@ -88,7 +88,7 @@ The `searcher` supports: @@ -88,7 +88,7 @@ The `searcher` supports:
88 - Supports both local and remote images 88 - Supports both local and remote images
89 89
90 ## Test Data 90 ## Test Data
91 - 91 +、
92 **Customer1 Test Dataset:** 92 **Customer1 Test Dataset:**
93 - Location: `data/customer1/goods_with_pic.5years_congku.csv.shuf.1w` 93 - Location: `data/customer1/goods_with_pic.5years_congku.csv.shuf.1w`
94 - Contains 10,000 shuffled product records with images 94 - Contains 10,000 shuffled product records with images
@@ -51,8 +51,28 @@ def init_service(customer_id: str = "customer1", es_host: str = "http://localhos @@ -51,8 +51,28 @@ def init_service(customer_id: str = "customer1", es_host: str = "http://localhos
51 51
52 print(f"Configuration loaded: {_config.customer_name}") 52 print(f"Configuration loaded: {_config.customer_name}")
53 53
54 - # Initialize ES client  
55 - _es_client = ESClient(hosts=[es_host]) 54 + # Get ES credentials from environment variables or .env file
  55 + es_username = os.getenv('ES_USERNAME')
  56 + es_password = os.getenv('ES_PASSWORD')
  57 +
  58 + # Try to load from config if not in env
  59 + if not es_username or not es_password:
  60 + try:
  61 + from config.env_config import get_es_config
  62 + es_config = get_es_config()
  63 + es_username = es_username or es_config.get('username')
  64 + es_password = es_password or es_config.get('password')
  65 + except Exception:
  66 + pass
  67 +
  68 + # Initialize ES client with authentication if credentials are available
  69 + if es_username and es_password:
  70 + print(f"Connecting to Elasticsearch with authentication: {es_username}")
  71 + _es_client = ESClient(hosts=[es_host], username=es_username, password=es_password)
  72 + else:
  73 + print(f"Connecting to Elasticsearch without authentication")
  74 + _es_client = ESClient(hosts=[es_host])
  75 +
56 if not _es_client.ping(): 76 if not _es_client.ping():
57 raise ConnectionError(f"Failed to connect to Elasticsearch at {es_host}") 77 raise ConnectionError(f"Failed to connect to Elasticsearch at {es_host}")
58 78
api/routes/admin.py
@@ -18,7 +18,7 @@ async def health_check(): @@ -18,7 +18,7 @@ async def health_check():
18 Returns service status and Elasticsearch connectivity. 18 Returns service status and Elasticsearch connectivity.
19 """ 19 """
20 try: 20 try:
21 - from main import get_es_client, get_config 21 + from ..app import get_es_client, get_config
22 22
23 es_client = get_es_client() 23 es_client = get_es_client()
24 config = get_config() 24 config = get_config()
@@ -46,7 +46,7 @@ async def get_configuration(): @@ -46,7 +46,7 @@ async def get_configuration():
46 Get current customer configuration (sanitized). 46 Get current customer configuration (sanitized).
47 """ 47 """
48 try: 48 try:
49 - from main import get_config 49 + from ..app import get_config
50 50
51 config = get_config() 51 config = get_config()
52 52
@@ -74,7 +74,7 @@ async def update_rewrite_rules(rules: Dict[str, str]): @@ -74,7 +74,7 @@ async def update_rewrite_rules(rules: Dict[str, str]):
74 rules: Dictionary of pattern -> replacement mappings 74 rules: Dictionary of pattern -> replacement mappings
75 """ 75 """
76 try: 76 try:
77 - from main import get_query_parser 77 + from ..app import get_query_parser
78 78
79 query_parser = get_query_parser() 79 query_parser = get_query_parser()
80 query_parser.update_rewrite_rules(rules) 80 query_parser.update_rewrite_rules(rules)
@@ -94,7 +94,7 @@ async def get_rewrite_rules(): @@ -94,7 +94,7 @@ async def get_rewrite_rules():
94 Get current query rewrite rules. 94 Get current query rewrite rules.
95 """ 95 """
96 try: 96 try:
97 - from main import get_query_parser 97 + from ..app import get_query_parser
98 98
99 query_parser = get_query_parser() 99 query_parser = get_query_parser()
100 rules = query_parser.get_rewrite_rules() 100 rules = query_parser.get_rewrite_rules()
@@ -114,7 +114,7 @@ async def get_index_stats(): @@ -114,7 +114,7 @@ async def get_index_stats():
114 Get index statistics. 114 Get index statistics.
115 """ 115 """
116 try: 116 try:
117 - from main import get_es_client, get_config 117 + from ..app import get_es_client, get_config
118 118
119 es_client = get_es_client() 119 es_client = get_es_client()
120 config = get_config() 120 config = get_config()
scripts/start_backend.sh
@@ -16,16 +16,30 @@ echo -e "${GREEN}========================================${NC}" @@ -16,16 +16,30 @@ echo -e "${GREEN}========================================${NC}"
16 echo -e "${GREEN}Starting Backend API Service${NC}" 16 echo -e "${GREEN}Starting Backend API Service${NC}"
17 echo -e "${GREEN}========================================${NC}" 17 echo -e "${GREEN}========================================${NC}"
18 18
19 -# Load config  
20 -source .env 19 +# Load config from .env file if it exists
  20 +if [ -f .env ]; then
  21 + set -a
  22 + source .env
  23 + set +a
  24 +fi
21 25
22 echo -e "\n${YELLOW}Configuration:${NC}" 26 echo -e "\n${YELLOW}Configuration:${NC}"
23 -echo " Customer: $CUSTOMER_ID"  
24 -echo " API Host: $API_HOST"  
25 -echo " API Port: $API_PORT"  
26 -echo " ES Host: $ES_HOST" 27 +echo " Customer: ${CUSTOMER_ID:-customer1}"
  28 +echo " API Host: ${API_HOST:-0.0.0.0}"
  29 +echo " API Port: ${API_PORT:-6002}"
  30 +echo " ES Host: ${ES_HOST:-http://localhost:9200}"
  31 +echo " ES Username: ${ES_USERNAME:-not set}"
27 32
28 echo -e "\n${YELLOW}Starting service...${NC}" 33 echo -e "\n${YELLOW}Starting service...${NC}"
  34 +
  35 +# Export environment variables for the Python process
  36 +export CUSTOMER_ID=${CUSTOMER_ID:-customer1}
  37 +export API_HOST=${API_HOST:-0.0.0.0}
  38 +export API_PORT=${API_PORT:-6002}
  39 +export ES_HOST=${ES_HOST:-http://localhost:9200}
  40 +export ES_USERNAME=${ES_USERNAME:-}
  41 +export ES_PASSWORD=${ES_PASSWORD:-}
  42 +
29 python -m api.app \ 43 python -m api.app \
30 --host $API_HOST \ 44 --host $API_HOST \
31 --port $API_PORT \ 45 --port $API_PORT \
@@ -3,8 +3,8 @@ @@ -3,8 +3,8 @@
3 # One-click startup script for SearchEngine 3 # One-click startup script for SearchEngine
4 # This script starts everything you need 4 # This script starts everything you need
5 5
6 -  
7 -set -e 6 +# Don't exit on error - we want to continue even if some checks fail
  7 +# set -e
8 8
9 cd "$(dirname "$0")" 9 cd "$(dirname "$0")"
10 10
@@ -65,14 +65,33 @@ echo -e "${GREEN}日志文件: logs/backend.log${NC}" @@ -65,14 +65,33 @@ echo -e "${GREEN}日志文件: logs/backend.log${NC}"
65 65
66 # Wait for backend to start 66 # Wait for backend to start
67 echo -e "${YELLOW}等待后端服务启动...${NC}" 67 echo -e "${YELLOW}等待后端服务启动...${NC}"
68 -sleep 5 68 +MAX_RETRIES=12
  69 +RETRY_COUNT=0
  70 +BACKEND_READY=false
  71 +
  72 +while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
  73 + sleep 2
  74 + if curl -s http://localhost:6002/ > /dev/null 2>&1; then
  75 + BACKEND_READY=true
  76 + break
  77 + fi
  78 + RETRY_COUNT=$((RETRY_COUNT + 1))
  79 + echo -e "${YELLOW} 等待中... ($RETRY_COUNT/$MAX_RETRIES)${NC}"
  80 +done
69 81
70 # Check if backend is running 82 # Check if backend is running
71 -if curl -s http://localhost:6002/admin/health > /dev/null 2>&1; then 83 +if [ "$BACKEND_READY" = true ]; then
72 echo -e "${GREEN}✓ 后端服务运行正常${NC}" 84 echo -e "${GREEN}✓ 后端服务运行正常${NC}"
  85 + # Try health check, but don't fail if it's not ready yet
  86 + if curl -s http://localhost:6002/admin/health > /dev/null 2>&1; then
  87 + echo -e "${GREEN}✓ 健康检查通过${NC}"
  88 + else
  89 + echo -e "${YELLOW}⚠ 健康检查未通过,但服务已启动${NC}"
  90 + fi
73 else 91 else
74 echo -e "${RED}✗ 后端服务启动失败,请检查日志: logs/backend.log${NC}" 92 echo -e "${RED}✗ 后端服务启动失败,请检查日志: logs/backend.log${NC}"
75 - exit 1 93 + echo -e "${YELLOW}提示: 后端服务可能需要更多时间启动,或者检查端口是否被占用${NC}"
  94 + # Don't exit - let user decide whether to continue
76 fi 95 fi
77 96
78 # Step 4: Start frontend 97 # Step 4: Start frontend