Troubleshooting
Troubleshooting Overview
Section titled “Troubleshooting Overview”This guide covers common issues, error messages, and solutions for the Elasticsearch integration system. Each section includes symptoms, causes, and step-by-step resolution procedures.
Common Issues
Section titled “Common Issues”1. Database Connection Issues
Section titled “1. Database Connection Issues”Symptom: “Database connection failed”
Section titled “Symptom: “Database connection failed””Error: Failed to connect to PostgreSQL databaseCauses
Section titled “Causes”- Database server is down
- Network connectivity issues
- Incorrect credentials
- Firewall blocking connection
- Database server overloaded
Solutions
Section titled “Solutions”Check Database Server Status:
# Check PostgreSQL servicesudo systemctl status postgresql
# Test connection manuallypsql -h 162.55.174.116 -U website -d categories_db
# Check network connectivityping 162.55.174.116telnet 162.55.174.116 5432Verify Credentials:
# Test with credentialspsql -h 162.55.174.116 -U website -d categories_db -c "SELECT 1;"Check Firewall:
# Check firewall statussudo ufw status
# Allow PostgreSQL port if neededsudo ufw allow from <your-ip> to any port 5432Database Server Overload:
-- Check active connectionsSELECT count(*) FROM pg_stat_activity;
-- Check database sizeSELECT pg_size_pretty(pg_database_size('categories_db'));
-- Check slow queriesSELECT query, calls, total_time, mean_timeFROM pg_stat_statementsORDER BY mean_time DESCLIMIT 10;2. Elasticsearch Connection Issues
Section titled “2. Elasticsearch Connection Issues”Symptom: “Elasticsearch connection failed”
Section titled “Symptom: “Elasticsearch connection failed””Error: Elasticsearch cURL error: Connection timeoutCauses
Section titled “Causes”- Elasticsearch server is down
- Network connectivity issues
- Firewall blocking connection
- Elasticsearch cluster issues
- Server overloaded
Solutions
Section titled “Solutions”Check Elasticsearch Status:
# Test basic connectivitycurl -X GET "http://91.99.113.45:9200/"
# Check cluster healthcurl -X GET "http://91.99.113.45:9200/_cluster/health?pretty"
# Check node statuscurl -X GET "http://91.99.113.45:9200/_nodes/stats?pretty"Network Diagnostics:
# Test network connectivityping 91.99.113.45telnet 91.99.113.45 9200
# Check DNS resolutionnslookup 91.99.113.45Elasticsearch Cluster Issues:
# Check cluster statecurl -X GET "http://91.99.113.45:9200/_cluster/state?pretty"
# Check index statuscurl -X GET "http://91.99.113.45:9200/_cat/indices?v"
# Check shard statuscurl -X GET "http://91.99.113.45:9200/_cat/shards?v"3. Sync Process Issues
Section titled “3. Sync Process Issues”Symptom: “Sync process not running”
Section titled “Symptom: “Sync process not running””Error: No sync state foundCauses
Section titled “Causes”- Cron job not configured
- Script permissions issues
- PHP execution errors
- Memory limits exceeded
- State file corruption
Solutions
Section titled “Solutions”Check Cron Configuration:
# Check cron servicesudo systemctl status cron
# List cron jobssudo crontab -l
# Check cron logssudo tail -f /var/log/syslog | grep CRONTest Script Execution:
# Test script manuallycd /var/www/html/wp-content/themes/products/scriptsphp elasticsearch-auto-sync.php
# Check script permissionsls -la elasticsearch-auto-sync.phpchmod +x elasticsearch-auto-sync.phpCheck PHP Errors:
# Check PHP error logtail -f /var/log/php8.1-fpm.log
# Test PHP configurationphp -m | grep -E "(pgsql|curl|json)"Memory Issues:
# Check memory usagefree -h
# Increase memory limitphp -d memory_limit=512M elasticsearch-auto-sync.php4. Search Functionality Issues
Section titled “4. Search Functionality Issues”Symptom: “Search returns no results”
Section titled “Symptom: “Search returns no results””Error: Search failed: No results foundCauses
Section titled “Causes”- Elasticsearch index empty
- Index mapping issues
- Search query problems
- Data synchronization issues
Solutions
Section titled “Solutions”Check Index Status:
# Check document countcurl -X GET "http://91.99.113.45:9200/products/_count"
# Check index mappingcurl -X GET "http://91.99.113.45:9200/products/_mapping?pretty"
# Test simple searchcurl -X GET "http://91.99.113.45:9200/products/_search?q=*&size=1"Verify Data Sync:
-- Check sync statusSELECT COUNT(*) as total, COUNT(CASE WHEN added_search = true THEN 1 END) as indexed, ROUND(COUNT(CASE WHEN added_search = true THEN 1 END)::numeric / COUNT(*) * 100, 1) as progress_pctFROM product WHERE title IS NOT NULL;Test Search Query:
# Test search via APIcurl -X POST "http://91.99.113.45:9200/products/_search" \ -H 'Content-Type: application/json' \ -d '{ "query": { "match_all": {} }, "size": 1 }'5. Performance Issues
Section titled “5. Performance Issues”Symptom: “Slow search response times”
Section titled “Symptom: “Slow search response times””Error: Search query took 5.2 secondsCauses
Section titled “Causes”- Large result sets
- Complex queries
- Insufficient resources
- Index optimization needed
- Network latency
Solutions
Section titled “Solutions”Optimize Queries:
// Use filters instead of queries when possible$query = [ 'bool' => [ 'filter' => [ ['term' => ['category' => '123']], ['range' => ['price' => ['gte' => 100, 'lte' => 500]]] ] ]];Check Resource Usage:
# Check CPU usagetop -p $(pgrep -f elasticsearch)
# Check memory usagefree -h
# Check disk I/Oiostat -x 1Optimize Index:
# Optimize indexcurl -X POST "http://91.99.113.45:9200/products/_optimize?max_num_segments=1"
# Check index statscurl -X GET "http://91.99.113.45:9200/products/_stats?pretty"Error Messages
Section titled “Error Messages”Database Errors
Section titled “Database Errors””PostgreSQL connection has already been closed”
Section titled “”PostgreSQL connection has already been closed””Error: PostgreSQL connection has already been closedSolution:
// Implement connection reconnectionprivate function ensureDatabaseConnection() { if ($this->connection === null) { $this->connection = $this->getDatabaseConnection(); }
// Test connection $result = pg_query($this->connection, "SELECT 1"); if (!$result) { $this->connection = $this->getDatabaseConnection(); }
return $this->connection;}“Database query failed”
Section titled ““Database query failed””Error: Database query failed: relation "product" does not existSolution:
-- Check if table exists\dt product
-- Create table if missingCREATE TABLE product ( id SERIAL PRIMARY KEY, title TEXT, brand VARCHAR(255), price NUMERIC(10, 2), added_search BOOLEAN DEFAULT FALSE);Elasticsearch Errors
Section titled “Elasticsearch Errors””Index not found”
Section titled “”Index not found””Error: Index [products] not foundSolution:
# Create indexcurl -X PUT "http://91.99.113.45:9200/products" \ -H 'Content-Type: application/json' \ -d '{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }'“Bulk indexing failed”
Section titled ““Bulk indexing failed””Error: Bulk indexing failed: Request entity too largeSolution:
// Reduce batch sizeprivate $batch_size = 100; // Instead of 500
// Or increase Elasticsearch limitscurl -X PUT "http://91.99.113.45:9200/products/_settings" \ -H 'Content-Type: application/json' \ -d '{ "index": { "max_result_window": 100000 } }'WordPress Errors
Section titled “WordPress Errors””Template not found”
Section titled “”Template not found””Error: Template not found: page-search.phpSolution:
# Check file existsls -la /var/www/html/wp-content/themes/products/page-search.php
# Check permissionschmod 644 /var/www/html/wp-content/themes/products/page-search.phpchown www-data:www-data /var/www/html/wp-content/themes/products/page-search.php“AJAX nonce verification failed”
Section titled ““AJAX nonce verification failed””Error: Invalid nonceSolution:
// Check nonce generationwp_create_nonce('elasticsearch_nonce');
// Verify nonce in AJAX handlercheck_ajax_referer('elasticsearch_nonce', 'nonce');Diagnostic Commands
Section titled “Diagnostic Commands”System Diagnostics
Section titled “System Diagnostics”Check System Resources
Section titled “Check System Resources”# Check memory usagefree -h
# Check disk spacedf -h
# Check CPU usagetop -p $(pgrep -f elasticsearch)
# Check network connectionsnetstat -tulpn | grep -E "(5432|9200)"Check Service Status
Section titled “Check Service Status”# Check all servicessudo systemctl status postgresqlsudo systemctl status elasticsearchsudo systemctl status apache2sudo systemctl status cronApplication Diagnostics
Section titled “Application Diagnostics”Check Sync Status
Section titled “Check Sync Status”# Check sync statecat /var/www/html/wp-content/themes/products/scripts/elasticsearch-sync-state.json
# Check recent logstail -100 /var/log/dealai/elasticsearch-sync.log
# Check error patternsgrep ERROR /var/log/dealai/elasticsearch-sync.log | tail -10Check Database Status
Section titled “Check Database Status”-- Check sync progressSELECT COUNT(*) as total, COUNT(CASE WHEN added_search = true THEN 1 END) as indexed, ROUND(COUNT(CASE WHEN added_search = true THEN 1 END)::numeric / COUNT(*) * 100, 1) as progress_pctFROM product WHERE title IS NOT NULL;
-- Check recent updatesSELECT COUNT(*) FROM product WHERE updated_at > NOW() - INTERVAL '1 hour';
-- Check for stuck productsSELECT COUNT(*) FROM product WHERE added_search = FALSE AND updated_at < NOW() - INTERVAL '24 hours';Check Elasticsearch Status
Section titled “Check Elasticsearch Status”# Check cluster healthcurl -X GET "http://91.99.113.45:9200/_cluster/health?pretty"
# Check index statscurl -X GET "http://91.99.113.45:9200/products/_stats?pretty"
# Check document countcurl -X GET "http://91.99.113.45:9200/products/_count"
# Check index sizecurl -X GET "http://91.99.113.45:9200/_cat/indices/products?v"Recovery Procedures
Section titled “Recovery Procedures”Complete System Reset
Section titled “Complete System Reset”Reset Sync Process
Section titled “Reset Sync Process”# Stop cron jobssudo crontab -r
# Reset state filerm /var/www/html/wp-content/themes/products/scripts/elasticsearch-sync-state.json
# Reset database flagspsql -h 162.55.174.116 -U website -d categories_db -c "UPDATE product SET added_search = FALSE;"
# Delete Elasticsearch indexcurl -X DELETE "http://91.99.113.45:9200/products"
# Restart servicessudo systemctl restart postgresqlsudo systemctl restart elasticsearch
# Recreate indexcurl -X PUT "http://91.99.113.45:9200/products" -H 'Content-Type: application/json' -d '{"settings":{"number_of_shards":3,"number_of_replicas":1}}'
# Restart cronsudo crontab -e# Add: */15 * * * * /usr/bin/php /var/www/html/wp-content/themes/products/scripts/elasticsearch-auto-sync.php >> /var/log/dealai/elasticsearch-sync.log 2>&1Partial Reset
Section titled “Partial Reset”# Reset only failed batchespsql -h 162.55.174.116 -U website -d categories_db -c "UPDATE product SET added_search = FALSEWHERE id IN ( SELECT id FROM product WHERE added_search = FALSE AND updated_at < NOW() - INTERVAL '1 hour' LIMIT 1000);"Data Recovery
Section titled “Data Recovery”Recover from Backup
Section titled “Recover from Backup”# Restore database from backuppsql -h 162.55.174.116 -U website -d categories_db < backup_20230915.sql
# Restore Elasticsearch indexcurl -X POST "http://91.99.113.45:9200/_reindex" \ -H 'Content-Type: application/json' \ -d '{ "source": { "index": "products_backup" }, "dest": { "index": "products" } }'Manual Data Repair
Section titled “Manual Data Repair”-- Fix orphaned recordsUPDATE product SET added_search = FALSE WHERE id NOT IN ( SELECT DISTINCT id FROM product WHERE added_search = TRUE);
-- Fix duplicate recordsDELETE FROM product WHERE id IN ( SELECT id FROM ( SELECT id, ROW_NUMBER() OVER (PARTITION BY external_id ORDER BY id) as rn FROM product ) t WHERE rn > 1);Prevention
Section titled “Prevention”Monitoring Setup
Section titled “Monitoring Setup”Set Up Alerts
Section titled “Set Up Alerts”# Create monitoring scriptcat > /usr/local/bin/elasticsearch-monitor.sh << 'EOF'#!/bin/bash
# Check Elasticsearch healthif ! curl -s http://91.99.113.45:9200/_cluster/health | grep -q '"status":"green"'; then echo "Elasticsearch health check failed" | mail -s "Elasticsearch Alert" [email protected]fi
# Check database connectivityif ! psql -h 162.55.174.116 -U website -d categories_db -c "SELECT 1" > /dev/null 2>&1; then echo "Database connectivity check failed" | mail -s "Database Alert" [email protected]fi
# Check sync progressif [ -f /var/www/html/wp-content/themes/products/scripts/elasticsearch-sync-state.json ]; then LAST_RUN=$(jq -r '.last_run' /var/www/html/wp-content/themes/products/scripts/elasticsearch-sync-state.json) LAST_RUN_TS=$(date -d "$LAST_RUN" +%s) CURRENT_TS=$(date +%s)
if [ $((CURRENT_TS - LAST_RUN_TS)) -gt 3600 ]; then echo "Sync process hasn't run in over 1 hour" | mail -s "Sync Alert" [email protected] fifiEOF
chmod +x /usr/local/bin/elasticsearch-monitor.sh
# Add to crontabecho "*/5 * * * * /usr/local/bin/elasticsearch-monitor.sh" | sudo crontab -Log Monitoring
Section titled “Log Monitoring”# Set up log monitoringtail -f /var/log/dealai/elasticsearch-sync.log | grep -E "(ERROR|WARNING)" | while read line; dodoneRegular Maintenance
Section titled “Regular Maintenance”Daily Maintenance
Section titled “Daily Maintenance”# Create daily maintenance scriptcat > /usr/local/bin/daily-maintenance.sh << 'EOF'#!/bin/bash
# Clean old logsfind /var/log/dealai -name "*.log" -mtime +7 -delete
# Optimize databasepsql -h 162.55.174.116 -U website -d categories_db -c "VACUUM ANALYZE product;"
# Check disk spaceif [ $(df /var/log | tail -1 | awk '{print $5}' | sed 's/%//') -gt 80 ]; then echo "Disk space warning: $(df /var/log | tail -1 | awk '{print $5}')" | mail -s "Disk Space Alert" [email protected]fiEOF
chmod +x /usr/local/bin/daily-maintenance.sh
# Add to crontabecho "0 2 * * * /usr/local/bin/daily-maintenance.sh" | sudo crontab -Next Steps
Section titled “Next Steps”- Performance Optimization - Performance tuning
- Monitoring & Management - Console and monitoring
- Deployment - Installation and setup
- Configuration - System configuration