Skip to content

Commit

Permalink
Merge pull request #290 from meilisearch/support-nested-field
Browse files Browse the repository at this point in the history
Ensure nested field support
  • Loading branch information
alallema authored Apr 28, 2022
2 parents 8445009 + f76c285 commit d9d08e0
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 2 deletions.
192 changes: 192 additions & 0 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,195 @@ 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",
"info": map[string]interface{}{
"comment": "An awesome book",
"reviewNb": float64(900),
},
},
},
NbHits: 1,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
args: args{
UID: "TestIndexBasicSearchOnNestedFieldsWithCustomClient",
client: customClient,
query: "An awesome",
request: SearchRequest{},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"id": float64(5), "title": "The Hobbit",
"info": map[string]interface{}{
"comment": "An awesome book",
"reviewNb": float64(900),
},
},
},
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",
"info": map[string]interface{}{
"comment": "A french book",
"reviewNb": float64(600),
},
},
map[string]interface{}{
"id": float64(3), "title": "Le Rouge et le Noir",
"info": map[string]interface{}{
"comment": "Another french book",
"reviewNb": float64(700),
},
},
},
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",
"info": map[string]interface{}{
"comment": "An awesome book",
"reviewNb": float64(900),
},
},
},
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",
"info": map[string]interface{}{
"comment": "An awesome book",
"reviewNb": float64(900),
},
},
},
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], got.Hits[len])
}
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,
})
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

0 comments on commit d9d08e0

Please sign in to comment.