Repository: algolia/algoliasearch-client-go
Pull request #668 (2021-06-28 21:57:45)

feat(facet ordering): Dynamic facet ordering support
| Q                 | A
| ----------------- | ----------
| Bug fix?          | no
| New feature?      | yes
| BC breaks?        | no     
| Need Doc update   | yes


## Describe your change

This PR Introduces models for dynamic facet ordering support


Patch:
diff --git a/algolia/search/rendering_content.go b/algolia/search/rendering_content.go
new file mode 100644
index 00000000..ad6572b8
--- /dev/null
+++ b/algolia/search/rendering_content.go
@@ -0,0 +1,45 @@
+package search
+
+// Content defining how the search interface should be rendered.
+// This is set via the settings for a default value and can be overridden via rules
+type RenderingContent struct {
+	FacetOrdering *FacetOrdering `json:"facetOrdering"`
+}
+
+// Facets and facets values ordering rules container
+type FacetOrdering struct {
+	// The ordering of facets.
+	Facets *FacetsOrder `json:"facets"`
+
+	// The ordering of facet values, within an individual list.
+	Values map[string]FacetValuesOrder `json:"values"`
+}
+
+// Facets ordering rule container
+type FacetsOrder struct {
+	// Pinned order of facet lists.
+	Order []string `json:"order"`
+}
+
+// Facet values ordering rule container
+type FacetValuesOrder struct {
+	// Pinned order of facet values.
+	Order []string `json:"order"`
+
+	// How to display the remaining items.
+	SortRemainingBy *SortRule `json:"sortRemainingBy"`
+}
+
+// Rule defining the sort order of facet values.
+type SortRule string
+
+const (
+	// alphabetical (ascending)
+	Alpha SortRule = "alpha"
+
+	// facet count (descending)
+	Count SortRule = "count"
+
+	// hidden (show only pinned values)
+	Hidden SortRule = "hidden"
+)
diff --git a/algolia/search/responses_search.go b/algolia/search/responses_search.go
index 400e0b51..325824a9 100644
--- a/algolia/search/responses_search.go
+++ b/algolia/search/responses_search.go
@@ -39,6 +39,7 @@ type QueryRes struct {
 	TimeoutHits                bool                              `json:"timeoutHits"`
 	UserData                   []interface{}                     `json:"userData"`
 	ABTestVariantID            int                               `json:"abTestVariantID"`
+	RenderingContent           *RenderingContent                 `json:"renderingContent"`
 }
 
 type AppliedRule struct {
diff --git a/algolia/search/rule_consequence_params.go b/algolia/search/rule_consequence_params.go
index 770bc1a3..b1e147ea 100644
--- a/algolia/search/rule_consequence_params.go
+++ b/algolia/search/rule_consequence_params.go
@@ -9,6 +9,7 @@ type RuleParams struct {
 	Query                         *RuleQuery             `json:"query,omitempty"`
 	AutomaticFacetFilters         []AutomaticFacetFilter `json:"automaticFacetFilters,omitempty"`
 	AutomaticOptionalFacetFilters []AutomaticFacetFilter `json:"automaticOptionalFacetFilters,omitempty"`
+	RenderingContent              *RenderingContent      `json:"renderingContent,omitempty"`
 	QueryParams
 }
 


Test patch:
diff --git a/algolia/search/rendering_content_test.go b/algolia/search/rendering_content_test.go
new file mode 100644
index 00000000..290aeab9
--- /dev/null
+++ b/algolia/search/rendering_content_test.go
@@ -0,0 +1,36 @@
+package search
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestUnmarshalRenderingContent(t *testing.T) {
+
+	payload := `{
+		"facetOrdering": {
+			"facets": {
+				"order": ["brand", "size", "color"]
+			},
+			"values": {
+				"brand": {
+					"order": ["Apple", "Sony", "Samsung"],
+					"sortRemainingBy": "alpha"
+				},
+				"color": {
+					"order": ["red", "green"]
+				}
+			}
+		}
+	}`
+	var r RenderingContent
+	err := json.Unmarshal([]byte(payload), &r)
+	require.NoError(t, err)
+	require.Equal(t, r.FacetOrdering.Facets.Order, []string{"brand", "size", "color"})
+	require.Equal(t, r.FacetOrdering.Values["brand"].Order, []string{"Apple", "Sony", "Samsung"})
+	require.Equal(t, *r.FacetOrdering.Values["brand"].SortRemainingBy, Alpha)
+	require.Equal(t, r.FacetOrdering.Values["color"].Order, []string{"red", "green"})
+	require.Nil(t, r.FacetOrdering.Values["color"].SortRemainingBy)
+}