fix meilisearch
This commit is contained in:
@@ -3,7 +3,6 @@ package meiliService
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -31,81 +30,145 @@ func New() *MeiliService {
|
||||
}
|
||||
}
|
||||
|
||||
func getIndexName(id_lang uint) string {
|
||||
return fmt.Sprintf("shop_%d_lang_%d", constdata.SHOP_ID, id_lang)
|
||||
}
|
||||
|
||||
// ==================================== FOR TESTING ONLY ====================================
|
||||
func (s *MeiliService) CreateIndex(id_lang uint) error {
|
||||
indexName := "meili_products_shop" + strconv.FormatInt(constdata.SHOP_ID, 10) + "_lang" + strconv.FormatInt(int64(id_lang), 10)
|
||||
indexName := getIndexName(id_lang)
|
||||
|
||||
products, err := s.productDescriptionRepo.GetMeiliProducts(id_lang)
|
||||
for i := 0; i < len(products); i++ {
|
||||
products[i].Description = cleanHTML(products[i].Description)
|
||||
products[i].DescriptionShort = cleanHTML(products[i].DescriptionShort)
|
||||
products[i].Usage = cleanHTML(products[i].Usage)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get products: %w", err)
|
||||
}
|
||||
|
||||
primaryKey := "ProductID"
|
||||
if len(products) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Process products: prepare for indexing
|
||||
for i := range products {
|
||||
// Convert JSON fields to searchable strings
|
||||
if len(products[i].Features) > 0 {
|
||||
products[i].FeaturesStr = string(products[i].Features)
|
||||
}
|
||||
if len(products[i].Attributes) > 0 {
|
||||
products[i].AttributesStr = string(products[i].Attributes)
|
||||
}
|
||||
if len(products[i].AttributeFilters) > 0 {
|
||||
products[i].AttributeFiltersStr = string(products[i].AttributeFilters)
|
||||
}
|
||||
|
||||
// Build cover image URL from image ID
|
||||
if products[i].IDImage > 0 {
|
||||
products[i].CoverImage = config.Get().Image.ImagePrefix + fmt.Sprintf("/%d", products[i].IDImage)
|
||||
}
|
||||
}
|
||||
|
||||
// Add documents to index
|
||||
primaryKey := "product_id"
|
||||
docOptions := &meilisearch.DocumentOptions{
|
||||
PrimaryKey: &primaryKey,
|
||||
SkipCreation: false,
|
||||
}
|
||||
|
||||
task, err := s.meiliClient.Index(indexName).AddDocuments(products, docOptions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("meili AddDocuments error: %w", err)
|
||||
return fmt.Errorf("failed to add documents: %w", err)
|
||||
}
|
||||
finishedTask, err := s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
fmt.Printf("Task status: %s\n", finishedTask.Status)
|
||||
fmt.Printf("Task error: %s\n", finishedTask.Error)
|
||||
|
||||
finishedTask, err := s.meiliClient.WaitForTask(task.TaskUID, 1000*time.Millisecond)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for task: %w", err)
|
||||
}
|
||||
if finishedTask.Status == "failed" {
|
||||
return fmt.Errorf("task failed: %v", finishedTask.Error)
|
||||
}
|
||||
|
||||
// Configure filterable attributes
|
||||
filterableAttributes := []interface{}{
|
||||
"CategoryID",
|
||||
"CategoryIDs",
|
||||
"product_id",
|
||||
"category_id",
|
||||
"category_ids",
|
||||
"active",
|
||||
"attribute_filters",
|
||||
"features",
|
||||
}
|
||||
task, err = s.meiliClient.Index(indexName).UpdateFilterableAttributes(&filterableAttributes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("meili AddDocuments error: %w", err)
|
||||
return fmt.Errorf("failed to update filterable attributes: %w", err)
|
||||
}
|
||||
_, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for filterable task: %w", err)
|
||||
}
|
||||
finishedTask, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
fmt.Printf("Task status: %s\n", finishedTask.Status)
|
||||
fmt.Printf("Task error: %s\n", finishedTask.Error)
|
||||
|
||||
// Configure sortable attributes
|
||||
sortableAttributes := []string{
|
||||
"price",
|
||||
"name",
|
||||
"product_id",
|
||||
"category_ids",
|
||||
}
|
||||
task, err = s.meiliClient.Index(indexName).UpdateSortableAttributes(&sortableAttributes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update sortable attributes: %w", err)
|
||||
}
|
||||
_, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for sortable task: %w", err)
|
||||
}
|
||||
|
||||
// Configure displayed attributes
|
||||
displayedAttributes := []string{
|
||||
"ProductID",
|
||||
"Name",
|
||||
"EAN13",
|
||||
"Reference",
|
||||
"Variations",
|
||||
"CoverImage",
|
||||
"product_id",
|
||||
"name",
|
||||
"ean13",
|
||||
"reference",
|
||||
"variations",
|
||||
"id_image",
|
||||
"price",
|
||||
"category_name",
|
||||
"category_ids",
|
||||
"attribute_filters",
|
||||
}
|
||||
task, err = s.meiliClient.Index(indexName).UpdateDisplayedAttributes(&displayedAttributes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("meili AddDocuments error: %w", err)
|
||||
return fmt.Errorf("failed to update displayed attributes: %w", err)
|
||||
}
|
||||
_, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for displayed task: %w", err)
|
||||
}
|
||||
finishedTask, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
fmt.Printf("Task status: %s\n", finishedTask.Status)
|
||||
fmt.Printf("Task error: %s\n", finishedTask.Error)
|
||||
|
||||
// Configure searchable attributes
|
||||
searchableAttributes := []string{
|
||||
"Name",
|
||||
"DescriptionShort",
|
||||
"Reference",
|
||||
"EAN13",
|
||||
"CategoryName",
|
||||
"Description",
|
||||
"Usage",
|
||||
"name",
|
||||
"description",
|
||||
"description_short",
|
||||
"usage",
|
||||
"features_str",
|
||||
"attributes_str",
|
||||
"reference",
|
||||
"ean13",
|
||||
"category_name",
|
||||
}
|
||||
task, err = s.meiliClient.Index(indexName).UpdateSearchableAttributes(&searchableAttributes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("meili AddDocuments error: %w", err)
|
||||
return fmt.Errorf("failed to update searchable attributes: %w", err)
|
||||
}
|
||||
_, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for searchable task: %w", err)
|
||||
}
|
||||
finishedTask, err = s.meiliClient.WaitForTask(task.TaskUID, 500*time.Millisecond)
|
||||
fmt.Printf("Task status: %s\n", finishedTask.Status)
|
||||
fmt.Printf("Task error: %s\n", finishedTask.Error)
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// ==================================== FOR TESTING ONLY ====================================
|
||||
func (s *MeiliService) Test(id_lang uint) (meilisearch.SearchResponse, error) {
|
||||
indexName := "meili_products_shop" + strconv.FormatInt(constdata.SHOP_ID, 10) + "_lang" + strconv.FormatInt(int64(id_lang), 10)
|
||||
indexName := getIndexName(id_lang)
|
||||
|
||||
searchReq := &meilisearch.SearchRequest{
|
||||
Limit: 4,
|
||||
@@ -128,7 +191,7 @@ func (s *MeiliService) Test(id_lang uint) (meilisearch.SearchResponse, error) {
|
||||
|
||||
// Search performs a full-text search on the specified index
|
||||
func (s *MeiliService) Search(id_lang uint, query string, id_category uint) (meilisearch.SearchResponse, error) {
|
||||
indexName := "meili_products_shop" + strconv.FormatInt(constdata.SHOP_ID, 10) + "_lang" + strconv.FormatInt(int64(id_lang), 10)
|
||||
indexName := getIndexName(id_lang)
|
||||
|
||||
filter_query := "Active = 1"
|
||||
if id_category != 0 {
|
||||
@@ -165,7 +228,7 @@ func (s *MeiliService) HealthCheck() (*meilisearch.Health, error) {
|
||||
|
||||
// GetIndexSettings retrieves the current settings for a specific index
|
||||
func (s *MeiliService) GetIndexSettings(id_lang uint) (map[string]interface{}, error) {
|
||||
indexName := "meili_products_shop" + strconv.FormatInt(constdata.SHOP_ID, 10) + "_lang" + strconv.FormatInt(int64(id_lang), 10)
|
||||
indexName := getIndexName(id_lang)
|
||||
|
||||
index := s.meiliClient.Index(indexName)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user