Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure nested field support #290

Merged
merged 4 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,172 @@ func TestIndex_SearchWithSort(t *testing.T) {
})
}
}

func TestIndex_SearchOnNestedFileds(t *testing.T) {
type args struct {
UID string
PrimaryKey string
client *Client
query string
request SearchRequest
searchableAttribute []string
sortableAttribute []string
}
tests := []struct {
name string
args args
want *SearchResponse
}{
{
name: "TestIndexBasicSearchOnNestedFields",
args: args{
UID: "TestIndexBasicSearchOnNestedFields",
client: defaultClient,
query: "An awesome",
request: SearchRequest{},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(5), "title": "The Hobbit",
},
},
NbHits: 1,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is a custom client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently use the fasthttp library to create an http client but in go you have the native net/http library to create a client. We offer the possibility for the user to generate his own client with net/http or another library.

args: args{
UID: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
client: customClient,
query: "An awesome",
request: SearchRequest{},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(5), "title": "The Hobbit",
},
},
NbHits: 1,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexSearchOnMultipleNestedFields",
args: args{
UID: "TestIndexSearchOnMultipleNestedFields",
client: defaultClient,
query: "french",
request: SearchRequest{},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(2), "title": "Le Petit Prince",
},
map[string]interface{}{
"id": float64(3), "title": "Le Rouge et le Noir",
},
},
NbHits: 2,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexSearchOnNestedFieldsWithSearchableAttribute",
args: args{
UID: "TestIndexSearchOnNestedFieldsWithSearchableAttribute",
client: defaultClient,
query: "An awesome",
request: SearchRequest{},
searchableAttribute: []string{
"title", "info.comment",
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(5), "title": "The Hobbit",
},
},
alallema marked this conversation as resolved.
Show resolved Hide resolved
NbHits: 1,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexSearchOnNestedFieldsWithSortableAttribute",
args: args{
UID: "TestIndexSearchOnNestedFieldsWithSortableAttribute",
client: defaultClient,
query: "An awesome",
request: SearchRequest{
Sort: []string{
"info.reviewNb:desc",
},
},
searchableAttribute: []string{
"title", "info.comment",
},
sortableAttribute: []string{
"info.reviewNb",
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(5), "title": "The Hobbit",
},
},
NbHits: 1,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpIndexWithNestedFields(tt.args.UID)
c := tt.args.client
i := c.Index(tt.args.UID)
t.Cleanup(cleanup(c))

if tt.args.searchableAttribute != nil {
gotTask, err := i.UpdateSearchableAttributes(&tt.args.searchableAttribute)
require.NoError(t, err)
testWaitForTask(t, i, gotTask)
}

if tt.args.sortableAttribute != nil {
gotTask, err := i.UpdateSortableAttributes(&tt.args.sortableAttribute)
require.NoError(t, err)
testWaitForTask(t, i, gotTask)
}

got, err := i.Search(tt.args.query, &tt.args.request)

require.NoError(t, err)
require.Equal(t, len(tt.want.Hits), len(got.Hits))
for len := range got.Hits {
require.Equal(t, tt.want.Hits[len].(map[string]interface{})["title"], got.Hits[len].(map[string]interface{})["title"])
require.Equal(t, tt.want.Hits[len].(map[string]interface{})["id"], got.Hits[len].(map[string]interface{})["id"])
}
require.Equal(t, tt.want.NbHits, got.NbHits)
require.Equal(t, tt.want.Offset, got.Offset)
require.Equal(t, tt.want.Limit, got.Limit)
require.Equal(t, tt.want.ExhaustiveNbHits, got.ExhaustiveNbHits)
require.Equal(t, tt.want.FacetsDistribution, got.FacetsDistribution)
require.Equal(t, tt.want.ExhaustiveFacetsCount, got.ExhaustiveFacetsCount)
})
}
}
27 changes: 27 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,33 @@ func SetUpBasicIndex(indexUID string) {
}
}

func SetUpIndexWithNestedFields(indexUID string) {
client := NewClient(ClientConfig{
Host: "http://localhost:7700",
APIKey: masterKey,
alallema marked this conversation as resolved.
Show resolved Hide resolved
})
index := client.Index(indexUID)

documents := []map[string]interface{}{
{"id": 1, "title": "Pride and Prejudice", "info": map[string]interface{}{"comment": "A great book", "reviewNb": 50}},
{"id": 2, "title": "Le Petit Prince", "info": map[string]interface{}{"comment": "A french book", "reviewNb": 600}},
{"id": 3, "title": "Le Rouge et le Noir", "info": map[string]interface{}{"comment": "Another french book", "reviewNb": 700}},
{"id": 4, "title": "Alice In Wonderland", "comment": "A weird book", "info": map[string]interface{}{"comment": "A weird book", "reviewNb": 800}},
{"id": 5, "title": "The Hobbit", "info": map[string]interface{}{"comment": "An awesome book", "reviewNb": 900}},
{"id": 6, "title": "Harry Potter and the Half-Blood Prince", "info": map[string]interface{}{"comment": "The best book", "reviewNb": 1000}},
{"id": 7, "title": "The Hitchhiker's Guide to the Galaxy"},
}
task, err := index.AddDocuments(documents)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
finalTask, _ := index.WaitForTask(task)
if finalTask.Status != "succeeded" {
os.Exit(1)
}
}

func SetUpIndexForFaceting() {
client := NewClient(ClientConfig{
Host: "http://localhost:7700",
Expand Down
4 changes: 2 additions & 2 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package meilisearch

import (
"testing"
"fmt"
"regexp"
"testing"

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

func TestVersion_GetQualifiedVersion(t *testing.T) {
Expand Down