fix top menu embeding struct

This commit is contained in:
2026-03-26 18:39:13 +01:00
parent 396e25b750
commit 9336cdfa28
9 changed files with 203 additions and 171 deletions

View File

@@ -1,14 +1,13 @@
package meiliService
import (
"encoding/xml"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"time"
"git.ma-al.com/goc_daniel/b2b/app/config"
"git.ma-al.com/goc_daniel/b2b/app/repos/productDescriptionRepo"
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
"github.com/meilisearch/meilisearch-go"
@@ -20,12 +19,10 @@ type MeiliService struct {
}
func New() *MeiliService {
meiliURL := os.Getenv("MEILISEARCH_URL")
meiliAPIKey := os.Getenv("MEILISEARCH_API_KEY")
client := meilisearch.New(
meiliURL,
meilisearch.WithAPIKey(meiliAPIKey),
config.Get().MailiSearch.ServerURL,
meilisearch.WithAPIKey(config.Get().MailiSearch.ApiKey),
)
return &MeiliService{
@@ -40,32 +37,9 @@ func (s *MeiliService) CreateIndex(id_lang uint) error {
products, err := s.productDescriptionRepo.GetMeiliProducts(id_lang)
for i := 0; i < len(products); i++ {
products[i].Description, err = cleanHTML(products[i].Description)
if err != nil {
fmt.Printf("products[i].Description: %v\n", products[i].Description)
fmt.Printf("products[i].ProductID: %v\n", products[i].ProductID)
fmt.Println("failed at description")
fmt.Printf("err: %v\n", err)
return err
}
products[i].DescriptionShort, err = cleanHTML(products[i].DescriptionShort)
if err != nil {
fmt.Printf("products[i].DescriptionShort: %v\n", products[i].DescriptionShort)
fmt.Printf("products[i].ProductID: %v\n", products[i].ProductID)
fmt.Println("failed at description short")
fmt.Printf("err: %v\n", err)
return err
}
products[i].Usage, err = cleanHTML(products[i].Usage)
if err != nil {
fmt.Printf("products[i].Usage: %v\n", products[i].Usage)
fmt.Printf("products[i].ProductID: %v\n", products[i].ProductID)
fmt.Println("failed at usage")
fmt.Printf("err: %v\n", err)
return err
}
products[i].Description = cleanHTML(products[i].Description)
products[i].DescriptionShort = cleanHTML(products[i].DescriptionShort)
products[i].Usage = cleanHTML(products[i].Usage)
}
primaryKey := "ProductID"
@@ -83,7 +57,7 @@ func (s *MeiliService) CreateIndex(id_lang uint) error {
filterableAttributes := []interface{}{
"CategoryID",
"Price",
"CategoryIDs",
}
task, err = s.meiliClient.Index(indexName).UpdateFilterableAttributes(&filterableAttributes)
if err != nil {
@@ -96,11 +70,10 @@ func (s *MeiliService) CreateIndex(id_lang uint) error {
displayedAttributes := []string{
"ProductID",
"Name",
"Active",
"Price",
"EAN13",
"Reference",
"Variations",
"CoverImage",
}
task, err = s.meiliClient.Index(indexName).UpdateDisplayedAttributes(&displayedAttributes)
if err != nil {
@@ -154,28 +127,17 @@ 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, limit uint, id_category uint, price_lower_bound float64, price_upper_bound float64) (meilisearch.SearchResponse, error) {
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)
filter_query := ""
filter_query := "Active = 1"
if id_category != 0 {
filter_query = "CategoryID = " + strconv.FormatUint(uint64(id_category), 10)
}
if price_lower_bound > 0.0 {
if filter_query != "" {
filter_query += " AND "
}
filter_query += "Price >= " + strconv.FormatFloat(price_lower_bound, 'f', -1, 64)
}
if price_upper_bound > 0.0 {
if filter_query != "" {
filter_query += " AND "
}
filter_query += "Price <= " + strconv.FormatFloat(price_upper_bound, 'f', -1, 64)
// Use CategoryIDs to include products from child categories
filter_query += fmt.Sprintf(" AND CategoryIDs = %d", id_category)
}
searchReq := &meilisearch.SearchRequest{
Limit: int64(limit),
Limit: 30,
Facets: []string{
"CategoryID",
},
@@ -201,40 +163,59 @@ func (s *MeiliService) HealthCheck() (*meilisearch.Health, error) {
return health, nil
}
// remove all tags from HTML text
func cleanHTML(s string) (string, error) {
r := strings.NewReader(s)
d := xml.NewDecoder(r)
// 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)
text := ""
index := s.meiliClient.Index(indexName)
// Configure the decoder for HTML; leave off strict and autoclose for XHTML
d.Strict = true
d.AutoClose = xml.HTMLAutoClose
d.Entity = xml.HTMLEntity
for {
token, err := d.Token()
if err == io.EOF {
break
} else if err != nil {
return text, err
}
result := make(map[string]interface{})
switch v := token.(type) {
case xml.StartElement:
if len(text) > 0 && text[len(text)-1] != '\n' {
text += " \n "
}
case xml.EndElement:
case xml.CharData:
if strings.TrimSpace(string(v)) != "" {
text += string(v)
}
case xml.Comment:
case xml.ProcInst:
case xml.Directive:
}
// Get searchable attributes
searchable, err := index.GetSearchableAttributes()
if err == nil {
result["searchableAttributes"] = searchable
}
return text, nil
// Get filterable attributes
filterable, err := index.GetFilterableAttributes()
if err == nil {
result["filterableAttributes"] = filterable
}
// Get displayed attributes
displayed, err := index.GetDisplayedAttributes()
if err == nil {
result["displayedAttributes"] = displayed
}
// Get ranking rules
ranking, err := index.GetRankingRules()
if err == nil {
result["rankingRules"] = ranking
}
// Get distinct attribute
distinct, err := index.GetDistinctAttribute()
if err == nil && distinct != nil {
result["distinctAttribute"] = *distinct
}
// Get typo tolerance
typo, err := index.GetTypoTolerance()
if err == nil {
result["typoTolerance"] = typo
}
return result, nil
}
// remove all tags from HTML text
func cleanHTML(s string) string {
// Simple regex to remove all HTML tags
re := regexp.MustCompile(`<[^>]*>`)
result := re.ReplaceAllString(s, "")
// Replace multiple spaces with single space
result = regexp.MustCompile(`\s+`).ReplaceAllString(result, " ")
return strings.TrimSpace(result)
}