Skip to content

Commit

Permalink
Merge #572
Browse files Browse the repository at this point in the history
572: Support experimental manager r=Ja7ad a=A7bari

# Pull Request

## Related issue
Fixes #571

## What does this PR do?
Introduce a new `ExperimentalFeatures` struct and methods to support the experimental features in the Meilisearch client. Users can enable or disable specific experimental features (vector store, logs route, and metrics). 

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: polyfloyd <[email protected]>
Co-authored-by: Javad <[email protected]>
Co-authored-by: Ahbari-M <[email protected]>
Co-authored-by: meili-bors[bot] <89034592+meili-bors[bot]@users.noreply.github.com>
Co-authored-by: Javad Rajabzadeh <[email protected]>
  • Loading branch information
4 people authored Sep 29, 2024
2 parents 2ee4206 + e98fd5e commit e1c62f5
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 e1c62f5

Please sign in to comment.