Improve search relevance and artist transliteration
This commit is contained in:
1 parent
db4548b115
commit
82f3eed9a0
10 files changed
+212
-12
No files matched your search
@@ -141,13 +141,15 @@ type MultiQuery struct {
|
||||
Q string `json:"q"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset,omitempty"`
|
||||
MatchingStrategy string `json:"matchingStrategy,omitempty"`
|
||||
AttributesToRetrieve []string `json:"attributesToRetrieve"`
|
||||
}
|
||||
|
||||
// MultiResult — ответ на один запрос: id найденных документов и оценка общего числа.
|
||||
type MultiResult struct {
|
||||
IDs []string
|
||||
Total int
|
||||
IDs []string
|
||||
Total int
|
||||
ExactIDs []string
|
||||
}
|
||||
|
||||
// MultiSearch выполняет несколько запросов за один вызов.
|
||||
@@ -155,7 +157,9 @@ func (m *Meili) MultiSearch(ctx context.Context, queries []MultiQuery) ([]MultiR
|
||||
var res struct {
|
||||
Results []struct {
|
||||
Hits []struct {
|
||||
ID string `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
NameTranslit any `json:"name_translit"`
|
||||
} `json:"hits"`
|
||||
EstimatedTotalHits int `json:"estimatedTotalHits"`
|
||||
} `json:"results"`
|
||||
@@ -167,8 +171,32 @@ func (m *Meili) MultiSearch(ctx context.Context, queries []MultiQuery) ([]MultiR
|
||||
for i, r := range res.Results {
|
||||
for _, h := range r.Hits {
|
||||
out[i].IDs = append(out[i].IDs, h.ID)
|
||||
if exactArtistName(queries[i].Q, h.Name, h.NameTranslit) {
|
||||
out[i].ExactIDs = append(out[i].ExactIDs, h.ID)
|
||||
}
|
||||
}
|
||||
out[i].Total = r.EstimatedTotalHits
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func exactArtistName(query, name string, translit any) bool {
|
||||
key := searchKey(query)
|
||||
if key == "" {
|
||||
return false
|
||||
}
|
||||
if searchKey(name) == key {
|
||||
return true
|
||||
}
|
||||
switch v := translit.(type) {
|
||||
case string: // indexes built before transliteration alternatives
|
||||
return searchKey(v) == key
|
||||
case []any:
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok && searchKey(s) == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user