Copilot commented on code in PR #301:
URL: https://github.com/apache/answer-plugins/pull/301#discussion_r2422608110


##########
search-algolia/algolia.go:
##########
@@ -109,23 +112,36 @@ func (s *SearchAlgolia) SearchContents(ctx 
context.Context, cond *plugin.SearchB
 
        var (
                query = strings.TrimSpace(strings.Join(cond.Words, " "))
-               opts  = []interface{}{
-                       opt.AttributesToRetrieve("objectID", "type"),
-                       opt.Filters(filters),
-                       opt.Page(cond.Page - 1),
-                       opt.HitsPerPage(cond.PageSize),
-               }
-               qres search.QueryRes
-       )
 
-       qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
+               qres *search.SearchResponse
+       )
+       qres, err = s.client.SearchSingleIndex(
+               s.client.NewApiSearchSingleIndexRequest(
+                       s.getIndexName(string(cond.Order))).
+                       WithSearchParams(
+                               search.SearchParamsObjectAsSearchParams(
+                                       search.NewEmptySearchParamsObject().
+                                               SetQuery(query).
+                                               
SetAttributesToRetrieve([]string{"objectID", "type"}).
+                                               SetFilters(filters).
+                                               SetPage(int32(cond.Page - 1)).
+                                               
SetHitsPerPage(int32(cond.PageSize)),
+                               ),
+                       ),
+       )
+       if err != nil {
+               return
+       }
+       if qres == nil {
+               return
+       }
        for _, hit := range qres.Hits {
                res = append(res, plugin.SearchResult{
-                       ID:   hit["objectID"].(string),
-                       Type: hit["type"].(string),
+                       ID:   hit.ObjectID,
+                       Type: "question",

Review Comment:
   The type is being hardcoded to 'question' instead of using the actual type 
from the hit. This will cause incorrect type classification for non-question 
content like answers.



##########
search-algolia/algolia.go:
##########
@@ -172,24 +188,37 @@ func (s *SearchAlgolia) SearchQuestions(ctx 
context.Context, cond *plugin.Search
 
        var (
                query = strings.TrimSpace(strings.Join(cond.Words, " "))
-               opts  = []interface{}{
-                       opt.AttributesToRetrieve("objectID", "type"),
-                       opt.Filters(filters),
-                       opt.Page(cond.Page - 1),
-                       opt.HitsPerPage(cond.PageSize),
-               }
-               qres search.QueryRes
+               qres  *search.SearchResponse
        )
 
-       qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
+       qres, err = s.client.SearchSingleIndex(
+               s.client.NewApiSearchSingleIndexRequest(
+                       s.getIndexName(string(cond.Order))).
+                       WithSearchParams(
+                               search.SearchParamsObjectAsSearchParams(
+                                       search.NewEmptySearchParamsObject().
+                                               SetQuery(query).
+                                               
SetAttributesToRetrieve([]string{"objectID", "type"}).
+                                               SetFilters(filters).
+                                               SetPage(int32(cond.Page - 1)).
+                                               
SetHitsPerPage(int32(cond.PageSize)),
+                               ),
+                       ),
+       )
+       if err != nil {
+               return
+       }
+       if qres == nil {
+               return
+       }
        for _, hit := range qres.Hits {
                res = append(res, plugin.SearchResult{
-                       ID:   hit["objectID"].(string),
-                       Type: hit["type"].(string),
+                       ID:   hit.ObjectID,
+                       Type: "question",

Review Comment:
   The type is being hardcoded to 'question' instead of using the actual type 
from the hit. This will cause incorrect type classification for answer content.
   ```suggestion
                        Type: hit.Type,
   ```



##########
search-algolia/algolia.go:
##########
@@ -226,50 +255,69 @@ func (s *SearchAlgolia) SearchAnswers(ctx 
context.Context, cond *plugin.SearchBa
 
        var (
                query = strings.TrimSpace(strings.Join(cond.Words, " "))
-               opts  = []interface{}{
-                       opt.AttributesToRetrieve("objectID", "type"),
-                       opt.Filters(filters),
-                       opt.Page(cond.Page - 1),
-                       opt.HitsPerPage(cond.PageSize),
-               }
-               qres search.QueryRes
+               qres  *search.SearchResponse
        )
 
-       qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
+       qres, err = s.client.SearchSingleIndex(
+               s.client.NewApiSearchSingleIndexRequest(
+                       s.getIndexName(string(cond.Order))).
+                       WithSearchParams(
+                               search.SearchParamsObjectAsSearchParams(
+                                       search.NewEmptySearchParamsObject().
+                                               SetQuery(query).
+                                               
SetAttributesToRetrieve([]string{"objectID", "type"}).
+                                               SetFilters(filters).
+                                               SetPage(int32(cond.Page - 1)).
+                                               
SetHitsPerPage(int32(cond.PageSize)),
+                               ),
+                       ),
+       )
        for _, hit := range qres.Hits {
                res = append(res, plugin.SearchResult{
-                       ID:   hit["objectID"].(string),
-                       Type: hit["type"].(string),
+                       ID:   hit.ObjectID,
+                       Type: "question",
                })
        }
-       total = int64(qres.NbHits)
+       total = int64(*qres.NbHits)
        return res, total, err
 }
 
 // UpdateContent updates the content to algolia server
 func (s *SearchAlgolia) UpdateContent(ctx context.Context, content 
*plugin.SearchContent) (err error) {
-       _, err = s.getIndex("").SaveObject(content)
+       var data map[string]any
+       j, err := json.Marshal(content)
+       if err != nil {
+               return
+       }
+
+       err = json.Unmarshal(j, &data)

Review Comment:
   This marshal-unmarshal pattern is inefficient. Consider using a more direct 
type conversion to convert *plugin.SearchContent to map[string]any without the 
JSON roundtrip.



##########
search-algolia/algolia.go:
##########
@@ -226,50 +255,69 @@ func (s *SearchAlgolia) SearchAnswers(ctx 
context.Context, cond *plugin.SearchBa
 
        var (
                query = strings.TrimSpace(strings.Join(cond.Words, " "))
-               opts  = []interface{}{
-                       opt.AttributesToRetrieve("objectID", "type"),
-                       opt.Filters(filters),
-                       opt.Page(cond.Page - 1),
-                       opt.HitsPerPage(cond.PageSize),
-               }
-               qres search.QueryRes
+               qres  *search.SearchResponse
        )
 
-       qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
+       qres, err = s.client.SearchSingleIndex(
+               s.client.NewApiSearchSingleIndexRequest(
+                       s.getIndexName(string(cond.Order))).
+                       WithSearchParams(
+                               search.SearchParamsObjectAsSearchParams(
+                                       search.NewEmptySearchParamsObject().
+                                               SetQuery(query).
+                                               
SetAttributesToRetrieve([]string{"objectID", "type"}).
+                                               SetFilters(filters).
+                                               SetPage(int32(cond.Page - 1)).
+                                               
SetHitsPerPage(int32(cond.PageSize)),
+                               ),
+                       ),
+       )
        for _, hit := range qres.Hits {
                res = append(res, plugin.SearchResult{
-                       ID:   hit["objectID"].(string),
-                       Type: hit["type"].(string),
+                       ID:   hit.ObjectID,
+                       Type: "question",

Review Comment:
   The type is being hardcoded to 'question' in the SearchAnswers method, but 
this should reflect the actual content type from the search results.



##########
search-algolia/algolia.go:
##########
@@ -226,50 +255,69 @@ func (s *SearchAlgolia) SearchAnswers(ctx 
context.Context, cond *plugin.SearchBa
 
        var (
                query = strings.TrimSpace(strings.Join(cond.Words, " "))
-               opts  = []interface{}{
-                       opt.AttributesToRetrieve("objectID", "type"),
-                       opt.Filters(filters),
-                       opt.Page(cond.Page - 1),
-                       opt.HitsPerPage(cond.PageSize),
-               }
-               qres search.QueryRes
+               qres  *search.SearchResponse
        )
 
-       qres, err = s.getIndex(string(cond.Order)).Search(query, opts...)
+       qres, err = s.client.SearchSingleIndex(
+               s.client.NewApiSearchSingleIndexRequest(
+                       s.getIndexName(string(cond.Order))).
+                       WithSearchParams(
+                               search.SearchParamsObjectAsSearchParams(
+                                       search.NewEmptySearchParamsObject().
+                                               SetQuery(query).
+                                               
SetAttributesToRetrieve([]string{"objectID", "type"}).
+                                               SetFilters(filters).
+                                               SetPage(int32(cond.Page - 1)).
+                                               
SetHitsPerPage(int32(cond.PageSize)),
+                               ),
+                       ),
+       )

Review Comment:
   Missing error check after the SearchSingleIndex call. The code should verify 
that err is nil before proceeding to iterate over hits.
   ```suggestion
        )
        if err != nil {
                return nil, 0, err
        }
   ```



##########
search-algolia/sync.go:
##########
@@ -73,10 +75,29 @@ func (s *SearchAlgolia) sync() {
 }
 
 func (s *SearchAlgolia) batchUpdateContent(ctx context.Context, contents 
[]*plugin.SearchContent) (err error) {
-       res, err := s.getIndex("").SaveObjects(contents)
+
+       // 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 = res.Wait()
+       err = json.Unmarshal(jsonRecords, &records)
+       if err != nil {
+               return
+       }

Review Comment:
   This marshal-unmarshal pattern is inefficient. Consider using a more direct 
type conversion or reflection to convert []*plugin.SearchContent to 
[]map[string]any without the JSON roundtrip.
   ```suggestion
        for _, content := range contents {
                var m map[string]any
                b, err := json.Marshal(content)
                if err != nil {
                        return err
                }
                if err := json.Unmarshal(b, &m); err != nil {
                        return err
                }
                records = append(records, m)
        }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to