Skip to content

Commit

Permalink
Merge branch 'main' into meili-bot/bump-version
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema authored Jun 5, 2023
2 parents bae947e + 9c789f0 commit c812338
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 26 deletions.
19 changes: 15 additions & 4 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@ get_one_document_1: |-
Fields: []string{"id", "title", "poster", "release_date"},
}, &a)
get_documents_1: |-
var a []interface{}
client.Index("movies").GetDocuments(nil, &meilisearch.DocumentsQuery{
var result meilisearch.DocumentsResult
client.Index("movies").GetDocuments(&meilisearch.DocumentsQuery{
Limit: 2,
}, &a)
Filter: "genres = action",
}, &result)
get_documents_post_1: |-
var result meilisearch.DocumentsResult
client.Index("books").GetDocuments(&meilisearch.DocumentsQuery{
Fields: []string{"title", "genres", "rating", "language"},
Filter: "(rating > 3 AND (genres = Adventure OR genres = Fiction)) AND language = English",
}, &result)
add_or_replace_documents_1: |-
documents := []map[string]interface{}{
{
Expand All @@ -59,13 +68,15 @@ delete_all_documents_1: |-
client.Index("movies").DeleteAllDocuments()
delete_one_document_1: |-
client.Index("movies").DeleteDocument("25684")
delete_documents_1: |-
delete_documents_by_batch_1: |-
client.Index("movies").DeleteDocuments([]string{
"23488",
"153738",
"437035",
"363869",
})
delete_documents_by_filter_1: |-
client.Index("movies").DeleteDocumentsByFilter("genres=action OR genres=adventure")
search_post_1: |-
client.Index("movies").Search("american ninja", &meilisearch.SearchRequest{})
multi_search_1: |-
Expand Down
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (e *Error) ErrorBody(body []byte) {
}
}

// Added a hint to the error message if it may come from a version incompatibility with Meilisearch
func VersionErrorHintMessage(err error, req *internalRequest) error {
return fmt.Errorf("%w. Hint: It might not be working because you're not up to date with the Meilisearch version that %s call requires.", err, req.functionName)
}

func namedSprintf(format string, params map[string]interface{}) string {
for key, val := range params {
format = strings.ReplaceAll(format, "${"+key+"}", fmt.Sprintf("%v", val))
Expand Down
49 changes: 49 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package meilisearch

import (
"fmt"
"net/http"
"testing"

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

func TestError_VersionErrorHintMessage(t *testing.T) {
type args struct {
request *internalRequest
mockedError error
}
tests := []struct {
name string
args args
}{
{
name: "VersionErrorHintMessageGetDocument",
args: args{
request: &internalRequest{
functionName: "GetDocuments",
},
mockedError: &Error{
Endpoint: "endpointForGetDocuments",
Method: http.MethodPost,
Function: "GetDocuments",
RequestToString: "empty request",
ResponseToString: "empty response",
MeilisearchApiError: meilisearchApiError{
Message: "empty Meilisearch message",
},
StatusCode: 1,
rawMessage: "empty message",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := VersionErrorHintMessage(tt.args.mockedError, tt.args.request)
require.Error(t, err)
fmt.Println(err)
require.Equal(t, tt.args.mockedError.Error()+". Hint: It might not be working because you're not up to date with the Meilisearch version that "+tt.args.request.functionName+" call requires.", err.Error())
})
}
}
31 changes: 28 additions & 3 deletions index_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ func (i Index) GetDocuments(request *DocumentsQuery, resp *DocumentsResult) erro
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/documents",
method: http.MethodGet,
contentType: contentTypeJSON,
withRequest: nil,
withResponse: resp,
withQueryParams: map[string]string{},
withQueryParams: nil,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetDocuments",
}
if request != nil {
if request != nil && request.Filter == nil {
req.withQueryParams = map[string]string{}
if request.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(request.Limit, 10)
}
Expand All @@ -193,9 +195,13 @@ func (i Index) GetDocuments(request *DocumentsQuery, resp *DocumentsResult) erro
if len(request.Fields) != 0 {
req.withQueryParams["fields"] = strings.Join(request.Fields, ",")
}
} else if request != nil && request.Filter != nil {
req.withRequest = request
req.method = http.MethodPost
req.endpoint = req.endpoint + "/fetch"
}
if err := i.client.executeRequest(req); err != nil {
return err
return VersionErrorHintMessage(err, &req)
}
return nil
}
Expand Down Expand Up @@ -526,6 +532,25 @@ func (i Index) DeleteDocuments(identifier []string) (resp *TaskInfo, err error)
return resp, nil
}

func (i Index) DeleteDocumentsByFilter(filter interface{}) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/documents/delete",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: map[string]interface{}{
"filter": filter,
},
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteDocumentsByFilter",
}
if err := i.client.executeRequest(req); err != nil {
return nil, VersionErrorHintMessage(err, &req)
}
return resp, nil
}

func (i Index) DeleteAllDocuments() (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
Expand Down
Loading

0 comments on commit c812338

Please sign in to comment.