Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 94 additions & 46 deletions search-algolia/algolia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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",
Copy link

Copilot AI Oct 11, 2025

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.

Copilot uses AI. Check for mistakes.
})
}
total = int64(qres.NbHits)
total = int64(*qres.NbHits)
return res, total, err
}

Expand Down Expand Up @@ -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",
Copy link

Copilot AI Oct 11, 2025

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 answer content.

Suggested change
Type: "question",
Type: hit.Type,

Copilot uses AI. Check for mistakes.
})
}

total = int64(qres.NbHits)
total = int64(*qres.NbHits)
return res, total, err
}

Expand Down Expand Up @@ -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)),
),
),
)
Copy link

Copilot AI Oct 11, 2025

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.

Suggested change
)
)
if err != nil {
return nil, 0, err
}

Copilot uses AI. Check for mistakes.
for _, hit := range qres.Hits {
res = append(res, plugin.SearchResult{
ID: hit["objectID"].(string),
Type: hit["type"].(string),
ID: hit.ObjectID,
Type: "question",
Copy link

Copilot AI Oct 11, 2025

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.

Copilot uses AI. Check for mistakes.
})
}
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)
Copy link

Copilot AI Oct 11, 2025

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.

Copilot uses AI. Check for mistakes.

_, err = s.client.SaveObject(s.client.NewApiSaveObjectRequest(s.getIndexName(""), data))
if err != nil {
return
}

return
}

// DeleteContent deletes the content
func (s *SearchAlgolia) DeleteContent(ctx context.Context, contentID string) (err error) {
_, err = s.getIndex("").DeleteObject(contentID)
_, err = s.client.DeleteObject(s.client.NewApiDeleteObjectRequest(s.getIndexName(""), contentID))
return
}

// connect connect to algolia server
func (s *SearchAlgolia) connect() (err error) {
s.client = search.NewClient(s.Config.APPID, s.Config.APIKey)
s.once.Do(func() {
s.client, err = search.NewClient(s.Config.APPID, s.Config.APIKey)
if err != nil {
log.Error("algolia: connect error", err)
}
log.Info("algolia: connected")
})
return
}

// init or create index
func (s *SearchAlgolia) getIndex(order string) (index *search.Index) {
idx := s.getIndexName(order)
return s.client.InitIndex(idx)
}

func (s *SearchAlgolia) getIndexName(order string) string {
// main index
var idx = s.Config.Index
Expand Down
12 changes: 6 additions & 6 deletions search-algolia/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/apache/answer-plugins/search-algolia
go 1.22.0

require (
github.com/algolia/algoliasearch-client-go/v3 v3.29.2
github.com/algolia/algoliasearch-client-go/v4 v4.28.1
github.com/apache/answer v1.4.2-RC1.0.20250107023923-061894735091
github.com/apache/answer-plugins/util v1.0.3-0.20250107030257-cf94ebc70954
github.com/apache/answer-plugins/util v1.0.3
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f
)

Expand Down Expand Up @@ -38,10 +38,10 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.10.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
27 changes: 12 additions & 15 deletions search-algolia/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/LinkinStars/go-i18n/v2 v2.2.2 h1:ZfjpzbW13dv6btv3RALKZkpN9A+7K1JA//2QcNeWaxU=
github.com/LinkinStars/go-i18n/v2 v2.2.2/go.mod h1:hLglSJ4/3M0Y7ZVcoEJI+OwqkglHCA32DdjuJJR2LbM=
github.com/algolia/algoliasearch-client-go/v3 v3.29.2 h1:lmAD/gNui2TLoT8az22ECr056EjoBR0RJQssNezyniQ=
github.com/algolia/algoliasearch-client-go/v3 v3.29.2/go.mod h1:i7tLoP7TYDmHX3Q7vkIOL4syVse/k5VJ+k0i8WqFiJk=
github.com/algolia/algoliasearch-client-go/v4 v4.28.1 h1:axkIsuZJHMQQU3FmNn0NJdu0T/01Y2GyLukPzp2zi2A=
github.com/algolia/algoliasearch-client-go/v4 v4.28.1/go.mod h1:2bHeze2/5+jvT8IYVq8j2NDLr/4R6erGxgud7ESuXww=
github.com/apache/answer v1.4.2-RC1.0.20250107023923-061894735091 h1:TmUPU0tX3VzbUO7rCBW3hJDUGO/WOI343zcbymIWQSM=
github.com/apache/answer v1.4.2-RC1.0.20250107023923-061894735091/go.mod h1:ehAJmrP4X9kBJKlYzTtRrwRbKeRqjnHxyYlhf11yzfw=
github.com/apache/answer-plugins/util v1.0.3-0.20250107030257-cf94ebc70954 h1:jVqxzyeHvYxAT30vGfHXtZLNoi9qAWnvTtuMSj6pFys=
github.com/apache/answer-plugins/util v1.0.3-0.20250107030257-cf94ebc70954/go.mod h1:wQEKNXVa/BKKq5yro9qo5bFiO3/SW1noORabxEndk3o=
github.com/apache/answer-plugins/util v1.0.3 h1:RrQ1wnNeOr1HVvqDWqWS22ZtWx9oB9c/y/lZpII0tXM=
github.com/apache/answer-plugins/util v1.0.3/go.mod h1:wQEKNXVa/BKKq5yro9qo5bFiO3/SW1noORabxEndk3o=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/bytedance/sonic v1.12.2 h1:oaMFuRTpMHYLpCntGca65YWt5ny+wAceDERTkT2L9lg=
Expand Down Expand Up @@ -85,7 +85,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand All @@ -101,15 +100,15 @@ golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8=
golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -119,16 +118,16 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand All @@ -139,8 +138,6 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
Loading