Skip to content

Commit

Permalink
chore: add request and response content encoding options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Aug 24, 2024
1 parent 7cb77b2 commit 9c2fc3d
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 43 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/meilisearch/meilisearch-go
go 1.16

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/andybalholm/brotli v1.1.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.9
github.com/mailru/easyjson v0.7.7
github.com/stretchr/testify v1.8.2
)
6 changes: 6 additions & 0 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ func (i *index) FetchInfoWithContext(ctx context.Context) (*IndexResult, error)
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "FetchInfo",
}
Expand Down Expand Up @@ -648,6 +649,8 @@ func (i *index) UpdateIndexWithContext(ctx context.Context, primaryKey string) (
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
reqEncoding: true,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "UpdateIndex",
}
Expand All @@ -669,6 +672,7 @@ func (i *index) DeleteWithContext(ctx context.Context, uid string) (bool, error)
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "Delete",
}
Expand All @@ -691,6 +695,7 @@ func (i *index) GetStatsWithContext(ctx context.Context) (*StatsIndex, error) {
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetStats",
}
Expand Down Expand Up @@ -719,6 +724,7 @@ func (i *index) GetTasksWithContext(ctx context.Context, param *TasksQuery) (*Ta
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
respEncoding: true,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTasks",
Expand Down
5 changes: 5 additions & 0 deletions index_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (i *index) DeleteDocumentWithContext(ctx context.Context, identifier string
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteDocument",
}
Expand All @@ -319,6 +320,7 @@ func (i *index) DeleteDocumentsWithContext(ctx context.Context, identifiers []st
contentType: contentTypeJSON,
withRequest: identifiers,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteDocuments",
}
Expand Down Expand Up @@ -362,6 +364,7 @@ func (i *index) DeleteAllDocumentsWithContext(ctx context.Context) (*TaskInfo, e
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteAllDocuments",
}
Expand Down Expand Up @@ -391,6 +394,7 @@ func (i *index) addDocuments(ctx context.Context, documentsPtr interface{}, cont
reqEncoding: true,
withRequest: documentsPtr,
withResponse: resp,
respEncoding: true,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "AddDocuments",
}
Expand Down Expand Up @@ -514,6 +518,7 @@ func (i *index) updateDocuments(ctx context.Context, documentsPtr interface{}, c
method: http.MethodPut,
contentType: contentType,
reqEncoding: true,
respEncoding: true,
withRequest: documentsPtr,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
Expand Down
24 changes: 0 additions & 24 deletions index_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@ func Test_AddOrUpdateDocumentsWithContentEncoding(t *testing.T) {
},
},
},
{
Name: "TestIndexBasicAddDocumentsWithDeflate",
ContentEncoding: DeflateEncoding,
Request: []map[string]interface{}{
{"ID": "123", "Name": "Pride and Prejudice"},
},
Response: struct {
WantResp *TaskInfo
DocResp DocumentsResult
}{WantResp: &TaskInfo{
TaskUID: 0,
Status: "enqueued",
Type: TaskTypeDocumentAdditionOrUpdate,
},
DocResp: DocumentsResult{
Results: []map[string]interface{}{
{"ID": "123", "Name": "Pride and Prejudice"},
},
Limit: 3,
Offset: 0,
Total: 1,
},
},
},
{
Name: "TestIndexBasicAddDocumentsWithBrotli",
ContentEncoding: BrotliEncoding,
Expand Down
4 changes: 4 additions & 0 deletions index_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (i *index) SearchWithContext(ctx context.Context, query string, request *Se
method: http.MethodPost,
contentType: contentTypeJSON,
respEncoding: true,
reqEncoding: true,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
Expand Down Expand Up @@ -71,6 +72,7 @@ func (i *index) SearchRawWithContext(ctx context.Context, query string, request
method: http.MethodPost,
contentType: contentTypeJSON,
respEncoding: true,
reqEncoding: true,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
Expand Down Expand Up @@ -100,6 +102,7 @@ func (i *index) FacetSearchWithContext(ctx context.Context, request *FacetSearch
method: http.MethodPost,
contentType: contentTypeJSON,
respEncoding: true,
reqEncoding: true,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
Expand All @@ -122,6 +125,7 @@ func (i *index) SearchSimilarDocumentsWithContext(ctx context.Context, param *Si
endpoint: "/indexes/" + i.uid + "/similar",
method: http.MethodPost,
respEncoding: true,
reqEncoding: true,
withRequest: param,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
Expand Down
165 changes: 150 additions & 15 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,118 @@ import (
"testing"
)

func TestIndex_SearchWithContentEncoding(t *testing.T) {
tests := []struct {
Name string
ContentEncoding ContentEncoding
Query string
Request *SearchRequest
FacetRequest *FacetSearchRequest
Response *SearchResponse
FacetResponse *FacetSearchResponse
}{
{
Name: "SearchResultWithGzipEncoding",
ContentEncoding: GzipEncoding,
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
},
FacetRequest: &FacetSearchRequest{
FacetName: "tag",
FacetQuery: "Novel",
},
FacetResponse: &FacetSearchResponse{
FacetHits: []interface{}{
map[string]interface{}{
"value": "Novel", "count": float64(5),
},
},
FacetQuery: "Novel",
},
Response: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(456), "title": "Le Petit Prince",
},
map[string]interface{}{
"Tag": "Epic fantasy", "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
EstimatedTotalHits: 2,
Offset: 0,
Limit: 20,
},
},
{
Name: "SearchResultWithBrotliEncoding",
ContentEncoding: BrotliEncoding,
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
},
Response: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(456), "title": "Le Petit Prince",
},
map[string]interface{}{
"Tag": "Epic fantasy", "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
EstimatedTotalHits: 2,
Offset: 0,
Limit: 20,
},
FacetRequest: &FacetSearchRequest{
FacetName: "tag",
FacetQuery: "Novel",
},
FacetResponse: &FacetSearchResponse{
FacetHits: []interface{}{
map[string]interface{}{
"value": "Novel", "count": float64(5),
},
},
FacetQuery: "Novel",
},
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
sv := setup(t, "", WithContentEncoding(tt.ContentEncoding, DefaultCompression))
setUpIndexForFaceting(sv)
i := sv.Index(tt.Request.IndexUID)
t.Cleanup(cleanup(sv))

got, err := i.Search(tt.Query, tt.Request)
require.NoError(t, err)
require.Equal(t, len(tt.Response.Hits), len(got.Hits))

gotJson, err := i.SearchRaw(tt.Query, tt.Request)
require.NoError(t, err)

var resp SearchResponse
err = json.Unmarshal(*gotJson, &resp)
require.NoError(t, err, "error unmarshalling raw got SearchResponse")
require.Equal(t, len(tt.Response.Hits), len(resp.Hits))

task, err := i.UpdateFilterableAttributes(&[]string{"tag"})
require.NoError(t, err)
testWaitForTask(t, i, task)

gotJson, err = i.FacetSearch(tt.FacetRequest)
require.NoError(t, err)
var gotFacet FacetSearchResponse
err = json.Unmarshal(*gotJson, &gotFacet)
require.NoError(t, err, "error unmarshalling raw got SearchResponse")
require.NoError(t, err)
require.Equal(t, len(gotFacet.FacetHits), len(tt.FacetResponse.FacetHits))
})
}
}

func TestIndex_SearchRaw(t *testing.T) {
sv := setup(t, "")

Expand Down Expand Up @@ -1847,40 +1959,62 @@ func TestIndex_SearchWithDistinct(t *testing.T) {
}

func TestIndex_SearchSimilarDocuments(t *testing.T) {
sv := setup(t, "")

tests := []struct {
UID string
PrimaryKey string
client ServiceManager
request *SimilarDocumentQuery
resp *SimilarDocumentResult
wantErr bool
Name string
UID string
PrimaryKey string
request *SimilarDocumentQuery
resp *SimilarDocumentResult
contentEncoding ContentEncoding
wantErr bool
}{
{
UID: "indexUID",
client: sv,
Name: "TestGetSimilarBasic",
UID: "indexUID",
request: &SimilarDocumentQuery{
Id: "123",
},
resp: new(SimilarDocumentResult),
wantErr: false,
},
{
Name: "TestGetSimilarWithError",
UID: "indexUID",
client: sv,
request: &SimilarDocumentQuery{},
resp: new(SimilarDocumentResult),
wantErr: true,
},
{
Name: "TestGetSimilarBasicWithContentEncodingGzip",
UID: "indexUID",
request: &SimilarDocumentQuery{
Id: "123",
},
resp: new(SimilarDocumentResult),
contentEncoding: GzipEncoding,
wantErr: false,
},
{
Name: "TestGetSimilarBasicWithContentEncodingBrotli",
UID: "indexUID",
request: &SimilarDocumentQuery{
Id: "123",
},
resp: new(SimilarDocumentResult),
contentEncoding: BrotliEncoding,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.UID, func(t *testing.T) {
i, err := setUpIndexWithVector(tt.client.(*meilisearch), tt.UID)
t.Run(tt.Name, func(t *testing.T) {
sv := setup(t, "")
if !tt.contentEncoding.IsZero() {
sv = setup(t, "", WithContentEncoding(tt.contentEncoding, DefaultCompression))
}
i, err := setUpIndexWithVector(sv.(*meilisearch), tt.UID)
require.NoError(t, err)
c := tt.client
t.Cleanup(cleanup(c))
t.Cleanup(cleanup(sv))

err = i.SearchSimilarDocuments(tt.request, tt.resp)
if tt.wantErr {
Expand All @@ -1890,6 +2024,7 @@ func TestIndex_SearchSimilarDocuments(t *testing.T) {

require.NoError(t, err)
require.NotNil(t, tt.resp)
require.Equal(t, len(tt.resp.Hits), 1)
})
}
}
Expand Down
Loading

0 comments on commit 9c2fc3d

Please sign in to comment.