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 20acfa9b082f599b4774459d9f8009ea24c27a8c
Author: kumfo <[email protected]>
AuthorDate: Sat Oct 11 16:18:18 2025 +0800

    feat: update algolia-search plugin
---
 search-algolia/algolia.go    | 140 +++++++++++++++++++++++++++++--------------
 search-algolia/go.mod        |  12 ++--
 search-algolia/go.sum        |  27 ++++-----
 search-algolia/initsearch.go |  72 +++++++++++++---------
 search-algolia/sync.go       |  25 +++++++-
 5 files changed, 178 insertions(+), 98 deletions(-)

diff --git a/search-algolia/algolia.go b/search-algolia/algolia.go
index c6e3795..dd14afc 100644
--- a/search-algolia/algolia.go
+++ b/search-algolia/algolia.go
@@ -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",
                })
        }
 
-       total = int64(qres.NbHits)
+       total = int64(*qres.NbHits)
        return res, total, err
 }
 
@@ -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)
+
+       _, 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
diff --git a/search-algolia/go.mod b/search-algolia/go.mod
index b39b058..39e84bb 100644
--- a/search-algolia/go.mod
+++ b/search-algolia/go.mod
@@ -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
 )
 
@@ -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
diff --git a/search-algolia/go.sum b/search-algolia/go.sum
index 02cf57b..3f7af30 100644
--- a/search-algolia/go.sum
+++ b/search-algolia/go.sum
@@ -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=
@@ -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=
@@ -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=
@@ -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=
@@ -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=
diff --git a/search-algolia/initsearch.go b/search-algolia/initsearch.go
index 373268c..fea30d0 100644
--- a/search-algolia/initsearch.go
+++ b/search-algolia/initsearch.go
@@ -20,28 +20,28 @@
 package algolia
 
 import (
-       "github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
-       "github.com/algolia/algoliasearch-client-go/v3/algolia/search"
+       "encoding/json"
+       "github.com/algolia/algoliasearch-client-go/v4/algolia/search"
 )
 
 // initSettings update algolia search settings
 func (s *SearchAlgolia) initSettings() (err error) {
        var (
-               settings = search.Settings{}
+               settings = search.IndexSettings{}
        )
-       err = settings.UnmarshalJSON(AlgoliaSearchServerConfig)
+       err = json.Unmarshal([]byte(AlgoliaSearchServerConfig), &settings)
        if err != nil {
                return
        }
 
        // point virtual index to sort
-       settings.Replicas = opt.Replicas(
-               "virtual("+s.getIndexName(NewestIndex)+")",
-               "virtual("+s.getIndexName(ActiveIndex)+")",
-               "virtual("+s.getIndexName(ScoreIndex)+")",
-       )
+       settings.Replicas = []string{
+               "virtual(" + s.getIndexName(NewestIndex) + ")",
+               "virtual(" + s.getIndexName(ActiveIndex) + ")",
+               "virtual(" + s.getIndexName(ScoreIndex) + ")",
+       }
 
-       _, err = s.getIndex("").SetSettings(settings, 
opt.ForwardToReplicas(true))
+       _, err = 
s.client.SetSettings(s.client.NewApiSetSettingsRequest(s.getIndexName(""), 
&settings).WithForwardToReplicas(true))
        if err != nil {
                return
        }
@@ -51,33 +51,47 @@ func (s *SearchAlgolia) initSettings() (err error) {
 
 // initVirtualReplicaSetting init virtual index replica setting
 func (s *SearchAlgolia) initVirtualReplicaSetting() (err error) {
-
-       _, err = s.getIndex(NewestIndex).SetSettings(search.Settings{
-               CustomRanking: opt.CustomRanking(
-                       "desc(created)",
-                       "desc(content)",
-                       "desc(title)"),
-       })
+       _, err = s.client.SetSettings(
+               s.client.NewApiSetSettingsRequest(
+                       s.getIndexName(NewestIndex),
+                       search.NewEmptyIndexSettings().
+                               SetCustomRanking([]string{
+                                       "desc(created)",
+                                       "desc(content)",
+                                       "desc(title)",
+                               }),
+               ),
+       )
        if err != nil {
                return
        }
 
-       _, err = s.getIndex(ActiveIndex).SetSettings(search.Settings{
-               CustomRanking: opt.CustomRanking(
-                       "desc(active)",
-                       "desc(content)",
-                       "desc(title)"),
-       })
+       _, err = s.client.SetSettings(
+               s.client.NewApiSetSettingsRequest(
+                       s.getIndexName(ActiveIndex),
+                       search.NewEmptyIndexSettings().
+                               SetCustomRanking([]string{
+                                       "desc(active)",
+                                       "desc(content)",
+                                       "desc(title)",
+                               }),
+               ),
+       )
        if err != nil {
                return
        }
 
-       _, err = s.getIndex(ScoreIndex).SetSettings(search.Settings{
-               CustomRanking: opt.CustomRanking(
-                       "desc(score)",
-                       "desc(content)",
-                       "desc(title)"),
-       })
+       _, err = s.client.SetSettings(
+               s.client.NewApiSetSettingsRequest(
+                       s.getIndexName(ScoreIndex),
+                       search.NewEmptyIndexSettings().
+                               SetCustomRanking([]string{
+                                       "desc(score)",
+                                       "desc(content)",
+                                       "desc(title)",
+                               }),
+               ),
+       )
        if err != nil {
                return
        }
diff --git a/search-algolia/sync.go b/search-algolia/sync.go
index 04b4202..6b4f841 100644
--- a/search-algolia/sync.go
+++ b/search-algolia/sync.go
@@ -21,6 +21,8 @@ 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"
 )
@@ -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
+       }
+
+       // Batch insert with transformation (API v4)
+       response, err := s.client.SaveObjects(
+               s.getIndexName(""),
+               records,
+               search.WithWaitForTasks(true), // Optional: wait for indexing 
to complete
+       )
+       if err != nil {
+               return
+       }
+
+       // Use response as needed
+       _ = response
        return
 }

Reply via email to