Skip to content

Commit

Permalink
Support experimental manager (#572)
Browse files Browse the repository at this point in the history
* Add Embedder.URL

* feat: add gzip and deflate encoding

* feat: client support content encoding for body and response

* feat: add option for enable content encoding

* feat: add content encoding and compress level type

* feat: add content encoding on constructor

* fix: header set for user agent

* feat: add brotli encoding

* fix: support request and response encoding same type encoding opt

* fix: add unit test for options

* feat: Add experimental features support

The code changes introduce a new `ExperimentalFeatures` struct and methods to support experimental features

* generating type marshalers and unmarshalers

* add: zero allocation copy for writer

* add: unit test for zero allocation copy

* feat: transcode get and update curl to golang example on code-samples file

* fix: remove encoding option on internal request object

* chore: add unit test for content encoding

* fix: used built-in flate package

* fix: used built-in gzip package

* fix: data race on test zero alloc

* fix: test case issue

* fix: add some test stage for improve coverage

* fix: remove brotli test stage

* fix: add encoder for decoing encoded internal error

* fix: currntly support delfate compression by using zlib

* fix: add writer nil error on test for prevent panic

* Add multi-search federation (fixes #573)

* feat: Add experimental features support

The code changes introduce a new `ExperimentalFeatures` struct and methods to support experimental features

* feat: transcode get and update curl to golang example on code-samples file

* feat: Add support for EditDocumentsByFunction and ContainsFilter in ExperimentalFeatures

---------

Co-authored-by: polyfloyd <floyd@polyfloyd.net>
Co-authored-by: Javad <ja7ad@live.com>
Co-authored-by: meili-bors[bot] <89034592+meili-bors[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 1, 2024
1 parent 2ee4206 commit 1cd56a5
Show file tree
Hide file tree
Showing 6 changed files with 500 additions and 56 deletions.
6 changes: 5 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ multi_search_1: |-
},
},
})
get_experimental_features_1: |-
client.ExperimentalFeatures().Get()
update_experimental_features_1: |-
client.ExperimentalFeatures().SetMetrics(true).Update()
facet_search_1: |-
client.Index("books").FacetSearch(&meilisearch.FacetSearchRequest{
FacetQuery: "fiction",
Expand Down Expand Up @@ -919,7 +923,7 @@ update_separator_tokens_1: |-
"&hellip;",
})
reset_separator_tokens_1: |-
client.Index("articles").ResetSeparatorTokens()
client.Index("articles").ResetSeparatorTokens()
get_non_separator_tokens_1: |-
client.Index("articles").GetNonSeparatorTokens()
update_non_separator_tokens_1: |-
Expand Down
93 changes: 93 additions & 0 deletions features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package meilisearch

import (
"context"
"net/http"
)

// Type for experimental features with additional client field
type ExperimentalFeatures struct {
client *client
ExperimentalFeaturesBase
}

func (m *meilisearch) ExperimentalFeatures() *ExperimentalFeatures {
return &ExperimentalFeatures{client: m.client}
}

func (ef *ExperimentalFeatures) SetVectorStore(vectorStore bool) *ExperimentalFeatures {
ef.VectorStore = &vectorStore
return ef
}

func (ef *ExperimentalFeatures) SetLogsRoute(logsRoute bool) *ExperimentalFeatures {
ef.LogsRoute = &logsRoute
return ef
}

func (ef *ExperimentalFeatures) SetMetrics(metrics bool) *ExperimentalFeatures {
ef.Metrics = &metrics
return ef
}

func (ef *ExperimentalFeatures) SetEditDocumentsByFunction(editDocumentsByFunction bool) *ExperimentalFeatures {
ef.EditDocumentsByFunction = &editDocumentsByFunction
return ef
}

func (ef *ExperimentalFeatures) SetContainsFilter(containsFilter bool) *ExperimentalFeatures {
ef.ContainsFilter = &containsFilter
return ef
}

func (ef *ExperimentalFeatures) Get() (*ExperimentalFeaturesResult, error) {
return ef.GetWithContext(context.Background())
}

func (ef *ExperimentalFeatures) GetWithContext(ctx context.Context) (*ExperimentalFeaturesResult, error) {
resp := new(ExperimentalFeaturesResult)
req := &internalRequest{
endpoint: "/experimental-features",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetExperimentalFeatures",
}

if err := ef.client.executeRequest(ctx, req); err != nil {
return nil, err
}

return resp, nil
}

func (ef *ExperimentalFeatures) Update() (*ExperimentalFeaturesResult, error) {
return ef.UpdateWithContext(context.Background())
}

func (ef *ExperimentalFeatures) UpdateWithContext(ctx context.Context) (*ExperimentalFeaturesResult, error) {
request := ExperimentalFeaturesBase{
VectorStore: ef.VectorStore,
LogsRoute: ef.LogsRoute,
Metrics: ef.Metrics,
EditDocumentsByFunction: ef.EditDocumentsByFunction,
ContainsFilter: ef.ContainsFilter,
}
resp := new(ExperimentalFeaturesResult)
req := &internalRequest{
endpoint: "/experimental-features",
method: http.MethodPatch,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
withQueryParams: nil,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "UpdateExperimentalFeatures",
}
if err := ef.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
77 changes: 77 additions & 0 deletions features_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package meilisearch

import (
"crypto/tls"
"testing"

"github.com/stretchr/testify/require"
)

func TestGet_ExperimentalFeatures(t *testing.T) {
sv := setup(t, "")
customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{
InsecureSkipVerify: true,
}))

tests := []struct {
name string
client ServiceManager
}{
{
name: "TestGetStats",
client: sv,
},
{
name: "TestGetStatsWithCustomClient",
client: customSv,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ef := tt.client.ExperimentalFeatures()
gotResp, err := ef.Get()
require.NoError(t, err)
require.NotNil(t, gotResp, "ExperimentalFeatures.Get() should not return nil value")
})
}
}

func TestUpdate_ExperimentalFeatures(t *testing.T) {
sv := setup(t, "")
customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{
InsecureSkipVerify: true,
}))

tests := []struct {
name string
client ServiceManager
}{
{
name: "TestUpdateStats",
client: sv,
},
{
name: "TestUpdateStatsWithCustomClient",
client: customSv,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ef := tt.client.ExperimentalFeatures()
ef.SetVectorStore(true)
ef.SetLogsRoute(true)
ef.SetMetrics(true)
ef.SetEditDocumentsByFunction(true)
ef.SetContainsFilter(true)
gotResp, err := ef.Update()
require.NoError(t, err)
require.Equal(t, true, gotResp.VectorStore, "ExperimentalFeatures.Update() should return vectorStore as true")
require.Equal(t, true, gotResp.LogsRoute, "ExperimentalFeatures.Update() should return logsRoute as true")
require.Equal(t, true, gotResp.Metrics, "ExperimentalFeatures.Update() should return metrics as true")
require.Equal(t, true, gotResp.EditDocumentsByFunction, "ExperimentalFeatures.Update() should return editDocumentsByFunction as true")
require.Equal(t, true, gotResp.ContainsFilter, "ExperimentalFeatures.Update() should return containsFilter as true")
})
}
}
6 changes: 5 additions & 1 deletion meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package meilisearch
import (
"context"
"fmt"
"github.com/golang-jwt/jwt/v4"
"net/http"
"strconv"
"time"

"github.com/golang-jwt/jwt/v4"
)

type meilisearch struct {
Expand Down Expand Up @@ -161,6 +162,9 @@ type ServiceManager interface {
// CreateSnapshotWithContext create database snapshot from meilisearch and support parent context
CreateSnapshotWithContext(ctx context.Context) (*TaskInfo, error)

// ExperimentalFeatures returns the experimental features manager.
ExperimentalFeatures() *ExperimentalFeatures

// Close closes the connection to the Meilisearch server.
Close()
}
Expand Down
17 changes: 17 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,23 @@ type DocumentsResult struct {
Total int64 `json:"total"`
}

// ExperimentalFeaturesResult represents the experimental features result from the API.
type ExperimentalFeaturesBase struct {
VectorStore *bool `json:"vectorStore,omitempty"`
LogsRoute *bool `json:"logsRoute,omitempty"`
Metrics *bool `json:"metrics,omitempty"`
EditDocumentsByFunction *bool `json:"editDocumentsByFunction,omitempty"`
ContainsFilter *bool `json:"containsFilter,omitempty"`
}

type ExperimentalFeaturesResult struct {
VectorStore bool `json:"vectorStore"`
LogsRoute bool `json:"logsRoute"`
Metrics bool `json:"metrics"`
EditDocumentsByFunction bool `json:"editDocumentsByFunction"`
ContainsFilter bool `json:"containsFilter"`
}

type SwapIndexesParams struct {
Indexes []string `json:"indexes"`
}
Expand Down
Loading

0 comments on commit 1cd56a5

Please sign in to comment.