-
Notifications
You must be signed in to change notification settings - Fork 63
feat: update algolia-search plugin #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,11 +22,13 @@ package algolia | |||||||||||
| import ( | ||||||||||||
| "context" | ||||||||||||
| "embed" | ||||||||||||
| "encoding/json" | ||||||||||||
| "github.com/segmentfault/pacman/log" | ||||||||||||
| "strconv" | ||||||||||||
| "strings" | ||||||||||||
| "sync" | ||||||||||||
|
|
||||||||||||
| "github.com/algolia/algoliasearch-client-go/v3/algolia/opt" | ||||||||||||
| "github.com/algolia/algoliasearch-client-go/v3/algolia/search" | ||||||||||||
| "github.com/algolia/algoliasearch-client-go/v4/algolia/search" | ||||||||||||
| "github.com/apache/answer-plugins/search-algolia/i18n" | ||||||||||||
| "github.com/apache/answer-plugins/util" | ||||||||||||
| "github.com/apache/answer/plugin" | ||||||||||||
|
|
@@ -37,8 +39,9 @@ var Info embed.FS | |||||||||||
|
|
||||||||||||
| type SearchAlgolia struct { | ||||||||||||
| Config *AlgoliaSearchConfig | ||||||||||||
| client *search.Client | ||||||||||||
| client *search.APIClient | ||||||||||||
| syncer plugin.SearchSyncer | ||||||||||||
| once sync.Once | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| func init() { | ||||||||||||
|
|
@@ -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", | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| total = int64(qres.NbHits) | ||||||||||||
| total = int64(*qres.NbHits) | ||||||||||||
| return res, total, err | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -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", | ||||||||||||
|
||||||||||||
| Type: "question", | |
| Type: hit.Type, |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error check after the SearchSingleIndex call. The code should verify that err is nil before proceeding to iterate over hits.
| ) | |
| ) | |
| if err != nil { | |
| return nil, 0, err | |
| } |
Outdated
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is being hardcoded to 'question' in the SearchAnswers method, but this should reflect the actual content type from the search results.
Outdated
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.