Sync Products to Elasticsearch
Manually trigger a full synchronization of all products from the database to Elasticsearch. This endpoint re-indexes all non-deleted product variants.
Endpoint
Authentication
Required - Admin role only
Request Body
No request body required.
Response
Success (200 OK)
Error Responses
Elasticsearch Unavailable (503)
Unauthorized (401)
Forbidden (403)
Example Requests
cURL
curl -X POST "https://api.luxmart.site/v1/products/search/sync/" \
-H "Authorization: Token <your_admin_token>"
JavaScript (Fetch)
const syncElasticsearch = async (token) => {
const response = await fetch(
'https://api.luxmart.site/v1/products/search/sync/',
{
method: 'POST',
headers: {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json'
}
}
);
if (!response.ok) {
throw new Error(`Sync failed: ${response.statusText}`);
}
return await response.json();
};
// Usage
try {
const result = await syncElasticsearch('your_admin_token');
console.log(result.message);
} catch (error) {
console.error('Sync error:', error);
}
Axios
import axios from 'axios';
const syncElasticsearch = async (token) => {
const response = await axios.post(
'https://api.luxmart.site/v1/products/search/sync/',
{},
{
headers: {
'Authorization': `Token ${token}`
}
}
);
return response.data;
};
// Usage
try {
const result = await syncElasticsearch('your_admin_token');
console.log(result.message);
} catch (error) {
console.error('Sync error:', error.response?.data || error.message);
}
Go Example
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func syncElasticsearch(token string) error {
url := "https://api.luxmart.site/v1/products/search/sync/"
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Token "+token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("sync failed with status: %d", resp.StatusCode)
}
var result map[string]string
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return err
}
fmt.Println(result["message"])
return nil
}
How It Works
- Index Initialization: Creates the Elasticsearch index if it doesn't exist, or recreates it if it does
- Product Fetching: Retrieves all non-deleted product variants from PostgreSQL
- Batch Indexing: Indexes products in batches of 100 for optimal performance
- Background Processing: The sync runs asynchronously in a background goroutine, so the API responds immediately
Important Notes
When to Use
- Initial Setup: After deploying Elasticsearch for the first time
- Data Recovery: If the Elasticsearch index is corrupted or lost
- Bulk Updates: After bulk product imports or migrations
- Re-indexing: To refresh the search index with latest product data
Performance Considerations
- Time: Sync time depends on the number of products. For 10,000 products, expect 2-5 minutes
- Resource Usage: The sync is CPU and I/O intensive. Avoid running multiple syncs simultaneously
- Non-blocking: The API responds immediately while sync runs in the background
- Logs: Check backend logs for sync progress and completion
Multiple Syncs
- Safe: Running multiple syncs is safe but resource-intensive
- Overwrite: Each sync completely re-indexes all products from scratch
- Concurrent: Multiple concurrent syncs can cause resource contention
- Recommendation: Wait for one sync to complete before starting another
Auto-Indexing
Products are automatically indexed when: - A new product variant is created - A product variant is updated - A product is updated (all variants are re-indexed) - A product or variant is deleted (removed from index)
Manual sync is only needed for: - Initial setup - Recovery scenarios - Bulk data operations
Monitoring
Check backend logs for sync status:
Starting full product sync to Elasticsearch...
Found 1000 product variants to index
Indexed 100/1000 variants
Indexed 200/1000 variants
...
Product sync completed!
Related Endpoints
- Search Products - Search products using Elasticsearch
- Get All Products - Get products without search
- Filter Products - Advanced filtering without Elasticsearch