@@ -22,8 +22,8 @@ package algolia
2222import (
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
286289func (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+ }
0 commit comments