Repository: algolia/algoliasearch-client-go
Pull request #676 (2021-07-21 12:26:04)

feat(reranking extensions): Implement Reranking extensions settings
| Q                 | A
| ----------------- | ----------
| Bug fix?          | no
| New feature?      | yes
| BC breaks?        | no    
| Need Doc update   | yes

## Describe your change

Add reranking extension structure to the index settings


Patch:
diff --git a/algolia/internal/gen/templates/settings.go.tmpl b/algolia/internal/gen/templates/settings.go.tmpl
index fc2691ae..08637884 100644
--- a/algolia/internal/gen/templates/settings.go.tmpl
+++ b/algolia/internal/gen/templates/settings.go.tmpl
@@ -17,6 +17,7 @@ type Settings struct {
     {{ title $setting.Name }} *opt.{{ title $setting.Name }}Option `json:"{{ $setting.Name }},omitempty"`
     {{- end }}
     RenderingContent *RenderingContent `json:"renderingContent,omitempty"`
+    Extensions *Extensions `json:"extensions,omitempty"`
     CustomSettings map[string]interface{} `json:"-"`
 }
 
diff --git a/algolia/search/extensions.go b/algolia/search/extensions.go
new file mode 100644
index 00000000..c3149545
--- /dev/null
+++ b/algolia/search/extensions.go
@@ -0,0 +1,19 @@
+package search
+
+// Index extensions container
+type Extensions struct {
+	// Reranking extension configuration
+	Reranking *Reranking `json:"reranking,omitempty"`
+}
+
+// Reranking extension settings
+type Reranking struct {
+	// Whether the extension is enabled
+	Enabled bool `json:"enabled,omitempty"`
+
+	// Amount of hits to rerank
+	MaxNbHits int `json:"maxNbHits,omitempty"`
+
+	// URL to target
+	Endpoint string `json:"endpoint,omitempty"`
+}
diff --git a/algolia/search/settings.go b/algolia/search/settings.go
index 1eb673e7..88594506 100644
--- a/algolia/search/settings.go
+++ b/algolia/search/settings.go
@@ -73,7 +73,8 @@ type Settings struct {
 	Advanced                                *opt.AdvancedOption                                `json:"advanced,omitempty"`
 	AttributeCriteriaComputedByMinProximity *opt.AttributeCriteriaComputedByMinProximityOption `json:"attributeCriteriaComputedByMinProximity,omitempty"`
 	UserData                                *opt.UserDataOption                                `json:"userData,omitempty"`
-	RenderingContent                        *RenderingContent                                  `json:"renderingContent"`
+	RenderingContent                        *RenderingContent                                  `json:"renderingContent,omitempty"`
+	Extensions                              *Extensions                                        `json:"extensions,omitempty"`
 	CustomSettings                          map[string]interface{}                             `json:"-"`
 }
 
@@ -513,7 +514,7 @@ func (s Settings) String() string {
 	settingsStr += fmt.Sprintf("\tAttributeCriteriaComputedByMinProximity: %v,\n", stringifyReturnValues(s.AttributeCriteriaComputedByMinProximity.Get()))
 	settingsStr += fmt.Sprintf("\tUserData: %v,\n", stringifyReturnValues(s.UserData.Get()))
 
-	settingsStr += fmt.Sprintf("\tRenderingContent: %+v\n", *s.RenderingContent)
+	settingsStr += fmt.Sprintf("\tRenderingContent: %+v\n", s.RenderingContent)
 
 	settingsStr += "\tCustomSettings{\n"
 	for k, v := range s.CustomSettings {


Test patch:
diff --git a/algolia/search/extensions_test.go b/algolia/search/extensions_test.go
new file mode 100644
index 00000000..2cde17a9
--- /dev/null
+++ b/algolia/search/extensions_test.go
@@ -0,0 +1,28 @@
+package search
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestUnmarshalRerankingExtension(t *testing.T) {
+
+	payload := `{
+		"reranking": {
+			"enabled": true,
+			"maxNbHits": 100,
+			"endpoint": "example.org/rerankingextension"
+		}
+	}`
+
+	var e Extensions
+	err := json.Unmarshal([]byte(payload), &e)
+	require.NoError(t, err)
+	require.NotNil(t, e.Reranking)
+	require.Equal(t, e.Reranking.Enabled, true)
+	require.Equal(t, e.Reranking.Endpoint, "example.org/rerankingextension")
+	require.Equal(t, e.Reranking.MaxNbHits, 100)
+
+}