Skip to content

Commit dca141b

Browse files
committed
feat(algolia): refactor data handling to use reflection for record parsing
1 parent 20acfa9 commit dca141b

2 files changed

Lines changed: 39 additions & 21 deletions

File tree

search-algolia/algolia.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ package algolia
2222
import (
2323
"context"
2424
"embed"
25-
"encoding/json"
2625
"github.com/segmentfault/pacman/log"
26+
"reflect"
2727
"strconv"
2828
"strings"
2929
"sync"
@@ -138,7 +138,7 @@ func (s *SearchAlgolia) SearchContents(ctx context.Context, cond *plugin.SearchB
138138
for _, hit := range qres.Hits {
139139
res = append(res, plugin.SearchResult{
140140
ID: hit.ObjectID,
141-
Type: "question",
141+
Type: hit.AdditionalProperties["type"].(string),
142142
})
143143
}
144144
total = int64(*qres.NbHits)
@@ -214,7 +214,7 @@ func (s *SearchAlgolia) SearchQuestions(ctx context.Context, cond *plugin.Search
214214
for _, hit := range qres.Hits {
215215
res = append(res, plugin.SearchResult{
216216
ID: hit.ObjectID,
217-
Type: "question",
217+
Type: hit.AdditionalProperties["type"].(string),
218218
})
219219
}
220220

@@ -272,10 +272,13 @@ func (s *SearchAlgolia) SearchAnswers(ctx context.Context, cond *plugin.SearchBa
272272
),
273273
),
274274
)
275+
if err != nil {
276+
return
277+
}
275278
for _, hit := range qres.Hits {
276279
res = append(res, plugin.SearchResult{
277280
ID: hit.ObjectID,
278-
Type: "question",
281+
Type: hit.AdditionalProperties["type"].(string),
279282
})
280283
}
281284
total = int64(*qres.NbHits)
@@ -284,15 +287,8 @@ func (s *SearchAlgolia) SearchAnswers(ctx context.Context, cond *plugin.SearchBa
284287

285288
// UpdateContent updates the content to algolia server
286289
func (s *SearchAlgolia) UpdateContent(ctx context.Context, content *plugin.SearchContent) (err error) {
287-
var data map[string]any
288-
j, err := json.Marshal(content)
289-
if err != nil {
290-
return
291-
}
292-
293-
err = json.Unmarshal(j, &data)
294-
295-
_, err = s.client.SaveObject(s.client.NewApiSaveObjectRequest(s.getIndexName(""), data))
290+
record := s.parseRecordData(content)
291+
_, err = s.client.SaveObject(s.client.NewApiSaveObjectRequest(s.getIndexName(""), record))
296292
if err != nil {
297293
return
298294
}
@@ -334,3 +330,30 @@ func (s *SearchAlgolia) getIndexName(order string) string {
334330
}
335331
return idx
336332
}
333+
334+
func (s *SearchAlgolia) parseRecordData(content *plugin.SearchContent) (contentMap map[string]any) {
335+
// 使用反射将结构体转换为map
336+
contentMap = make(map[string]any)
337+
val := reflect.ValueOf(content).Elem() // 获取指针指向的实际结构体
338+
typ := val.Type()
339+
340+
// each struct field
341+
for i := 0; i < val.NumField(); i++ {
342+
field := val.Field(i)
343+
// fetch json tag name, if not exist use field name
344+
fieldName := typ.Field(i).Name
345+
jsonTag := typ.Field(i).Tag.Get("json")
346+
if jsonTag != "" {
347+
parts := strings.Split(jsonTag, ",")
348+
if parts[0] != "-" && parts[0] != "" {
349+
fieldName = parts[0]
350+
}
351+
}
352+
353+
// only set the field that can be interfaced
354+
if field.CanInterface() {
355+
contentMap[fieldName] = field.Interface()
356+
}
357+
}
358+
return
359+
}

search-algolia/sync.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package algolia
2121

2222
import (
2323
"context"
24-
"encoding/json"
2524
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
2625
"github.com/apache/answer/plugin"
2726
"github.com/segmentfault/pacman/log"
@@ -78,13 +77,9 @@ func (s *SearchAlgolia) batchUpdateContent(ctx context.Context, contents []*plug
7877

7978
// Prepare your records as a slice of map[string]any
8079
var records []map[string]any
81-
jsonRecords, err := json.Marshal(contents)
82-
if err != nil {
83-
return
84-
}
85-
err = json.Unmarshal(jsonRecords, &records)
86-
if err != nil {
87-
return
80+
for _, content := range contents {
81+
record := s.parseRecordData(content)
82+
records = append(records, record)
8883
}
8984

9085
// Batch insert with transformation (API v4)

0 commit comments

Comments
 (0)