This is an automated email from the ASF dual-hosted git repository. kumfo pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/answer-plugins.git
commit dca141bf745d16ebc385ba58a92ffddb1ae7f9f0 Author: kumfo <[email protected]> AuthorDate: Sat Oct 11 18:17:14 2025 +0800 feat(algolia): refactor data handling to use reflection for record parsing --- search-algolia/algolia.go | 49 ++++++++++++++++++++++++++++++++++------------- search-algolia/sync.go | 11 +++-------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/search-algolia/algolia.go b/search-algolia/algolia.go index dd14afc..c985594 100644 --- a/search-algolia/algolia.go +++ b/search-algolia/algolia.go @@ -22,8 +22,8 @@ package algolia import ( "context" "embed" - "encoding/json" "github.com/segmentfault/pacman/log" + "reflect" "strconv" "strings" "sync" @@ -138,7 +138,7 @@ func (s *SearchAlgolia) SearchContents(ctx context.Context, cond *plugin.SearchB for _, hit := range qres.Hits { res = append(res, plugin.SearchResult{ ID: hit.ObjectID, - Type: "question", + Type: hit.AdditionalProperties["type"].(string), }) } total = int64(*qres.NbHits) @@ -214,7 +214,7 @@ func (s *SearchAlgolia) SearchQuestions(ctx context.Context, cond *plugin.Search for _, hit := range qres.Hits { res = append(res, plugin.SearchResult{ ID: hit.ObjectID, - Type: "question", + Type: hit.AdditionalProperties["type"].(string), }) } @@ -272,10 +272,13 @@ func (s *SearchAlgolia) SearchAnswers(ctx context.Context, cond *plugin.SearchBa ), ), ) + if err != nil { + return + } for _, hit := range qres.Hits { res = append(res, plugin.SearchResult{ ID: hit.ObjectID, - Type: "question", + Type: hit.AdditionalProperties["type"].(string), }) } total = int64(*qres.NbHits) @@ -284,15 +287,8 @@ func (s *SearchAlgolia) SearchAnswers(ctx context.Context, cond *plugin.SearchBa // UpdateContent updates the content to algolia server func (s *SearchAlgolia) UpdateContent(ctx context.Context, content *plugin.SearchContent) (err error) { - var data map[string]any - j, err := json.Marshal(content) - if err != nil { - return - } - - err = json.Unmarshal(j, &data) - - _, err = s.client.SaveObject(s.client.NewApiSaveObjectRequest(s.getIndexName(""), data)) + record := s.parseRecordData(content) + _, err = s.client.SaveObject(s.client.NewApiSaveObjectRequest(s.getIndexName(""), record)) if err != nil { return } @@ -334,3 +330,30 @@ func (s *SearchAlgolia) getIndexName(order string) string { } return idx } + +func (s *SearchAlgolia) parseRecordData(content *plugin.SearchContent) (contentMap map[string]any) { + // 使用反射将结构体转换为map + contentMap = make(map[string]any) + val := reflect.ValueOf(content).Elem() // 获取指针指向的实际结构体 + typ := val.Type() + + // each struct field + for i := 0; i < val.NumField(); i++ { + field := val.Field(i) + // fetch json tag name, if not exist use field name + fieldName := typ.Field(i).Name + jsonTag := typ.Field(i).Tag.Get("json") + if jsonTag != "" { + parts := strings.Split(jsonTag, ",") + if parts[0] != "-" && parts[0] != "" { + fieldName = parts[0] + } + } + + // only set the field that can be interfaced + if field.CanInterface() { + contentMap[fieldName] = field.Interface() + } + } + return +} diff --git a/search-algolia/sync.go b/search-algolia/sync.go index 6b4f841..050390a 100644 --- a/search-algolia/sync.go +++ b/search-algolia/sync.go @@ -21,7 +21,6 @@ package algolia import ( "context" - "encoding/json" "github.com/algolia/algoliasearch-client-go/v4/algolia/search" "github.com/apache/answer/plugin" "github.com/segmentfault/pacman/log" @@ -78,13 +77,9 @@ func (s *SearchAlgolia) batchUpdateContent(ctx context.Context, contents []*plug // Prepare your records as a slice of map[string]any var records []map[string]any - jsonRecords, err := json.Marshal(contents) - if err != nil { - return - } - err = json.Unmarshal(jsonRecords, &records) - if err != nil { - return + for _, content := range contents { + record := s.parseRecordData(content) + records = append(records, record) } // Batch insert with transformation (API v4)
