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

Changes related to the next MeiliSearch release (v0.22.0) #192

Merged
merged 10 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 57 additions & 6 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ update_settings_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"desc(release_date)",
"desc(rank)",
"release_date:desc",
"rank:desc",
},
DistinctAttribute: &distinctAttribute,
SearchableAttributes: []string{
Expand All @@ -96,6 +97,10 @@ update_settings_1: |-
"a",
"an",
},
SortableAttributes: []string{
"title",
"release_date",
},
Synonyms: map[string][]string{
"wolverine": []string{"xmen", "logan"},
"logan": []string{"wolverine"},
Expand Down Expand Up @@ -130,9 +135,10 @@ update_ranking_rules_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"asc(release_date)",
"desc(rank)",
"release_date:asc",
"rank:desc",
}
client.Index("movies").UpdateRankingRules(&rankingRules)
reset_ranking_rules_1: |-
Expand Down Expand Up @@ -176,6 +182,16 @@ update_displayed_attributes_1: |-
client.Index("movies").UpdateDisplayedAttributes(&displayedAttributes)
reset_displayed_attributes_1: |-
client.Index("movies").ResetDisplayedAttributes()
get_sortable_attributes_1: |-
client.Index("books").GetSortableAttributes()
update_sortable_attributes_1: |-
sortableAttributes := []string{
"price",
"author",
}
client.Index("books").UpdateSortableAttributes(&sortableAttributes)
reset_sortable_attributes_1: |-
client.Index("books").ResetSortableAttributes()
get_index_stats_1: |-
client.Index("movies").GetStats()
get_indexes_stats_1: |-
Expand Down Expand Up @@ -267,9 +283,10 @@ settings_guide_ranking_rules_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"asc(release_date)",
"desc(rank)",
"release_date:asc",
"rank:desc",
}
client.Index("movies").UpdateRankingRules(&rankingRules)
settings_guide_distinct_1: |-
Expand Down Expand Up @@ -408,3 +425,37 @@ get_dump_status_1: |-
resp, err := client.GetDumpStatus("dump-uid")
phrase_search_1: |-
resp, err := client.Index("movies").Search("\"african american\" horror", &meilisearch.SearchRequest{})
sorting_guide_update_sortable_attributes_1: |-
sortableAttributes := []string{
"author",
"price",
}
client.Index("books").UpdateSortableAttributes(&sortableAttributes)
sorting_guide_update_ranking_rules_1: |-
rankingRules := []string{
"words",
"sort",
"typo",
"proximity",
"attribute",
"exactness",
}
client.Index("books").UpdateRankingRules(&rankingRules)
sorting_guide_sort_parameter_1: |-
resp, err := client.Index("books").Search("science fiction", &meilisearch.SearchRequest{
Sort: []string{
"price:asc",
},
})
sorting_guide_sort_parameter_2: |-
resp, err := client.Index("books").Search("butler", &meilisearch.SearchRequest{
Sort: []string{
"author:desc",
},
})
search_parameter_guide_sort_1: |-
resp, err := client.Index("books").Search("science fiction", &meilisearch.SearchRequest{
Sort: []string{
"price:asc",
},
})
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ JSON output:

## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.21.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.21.0).
This package only guarantees the compatibility with the [version v0.22.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.22.0).

## 💡 Learn More

Expand Down
3 changes: 3 additions & 0 deletions index_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (i Index) Search(query string, request *SearchRequest) (*SearchResponse, er
if len(request.FacetsDistribution) != 0 {
searchPostRequestParams["facetsDistribution"] = request.FacetsDistribution
}
if len(request.Sort) != 0 {
searchPostRequestParams["sort"] = request.Sort
}

req := internalRequest{
endpoint: "/indexes/" + i.UID + "/search",
Expand Down
234 changes: 233 additions & 1 deletion index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func TestIndex_SearchFacets(t *testing.T) {
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{})["book_id"], got.Hits[len].(map[string]interface{})["book_id"])
Expand All @@ -360,7 +361,6 @@ func TestIndex_SearchFacets(t *testing.T) {
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)
})
Expand Down Expand Up @@ -660,3 +660,235 @@ func TestIndex_SearchWithFilters(t *testing.T) {
})
}
}


func TestIndex_SearchWithSort(t *testing.T) {
type args struct {
UID string
PrimaryKey string
client *Client
query string
sortableAttributes []string
request SearchRequest
}
tests := []struct {
name string
args args
want *SearchResponse
}{
{
name: "TestIndexBasicSearchWithSortIntParameter",
args: args{
UID: "indexUID",
client: defaultClient,
query: "and",
sortableAttributes: []string{
"year",
},
request: SearchRequest{
Sort: []string{
"year:asc",
},
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(123), "title": "Pride and Prejudice",
},
map[string]interface{}{
"book_id": float64(730), "title": "War and Peace",
},
map[string]interface{}{
"book_id": float64(1032), "title": "Crime and Punishment",
},
map[string]interface{}{
"book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
NbHits: 4,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchWithSortStringParameter",
args: args{
UID: "indexUID",
client: defaultClient,
query: "and",
sortableAttributes: []string{
"title",
},
request: SearchRequest{
Sort: []string{
"title:asc",
},
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(1032), "title": "Crime and Punishment",
},
map[string]interface{}{
"book_id": float64(123), "title": "Pride and Prejudice",
},
map[string]interface{}{
"book_id": float64(730), "title": "War and Peace",
},
map[string]interface{}{
"book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
NbHits: 4,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchWithSortMultipleParameter",
args: args{
UID: "indexUID",
client: defaultClient,
query: "and",
sortableAttributes: []string{
"title",
"year",
},
request: SearchRequest{
Sort: []string{
"title:asc",
"year:asc",
},
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(1032), "title": "Crime and Punishment",
},
map[string]interface{}{
"book_id": float64(123), "title": "Pride and Prejudice",
},
map[string]interface{}{
"book_id": float64(730), "title": "War and Peace",
},
map[string]interface{}{
"book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
NbHits: 4,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchWithSortMultipleParameterReverse",
args: args{
UID: "indexUID",
client: defaultClient,
query: "and",
sortableAttributes: []string{
"title",
"year",
},
request: SearchRequest{
Sort: []string{
"year:asc",
"title:asc",
},
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(123), "title": "Pride and Prejudice",
},
map[string]interface{}{
"book_id": float64(730), "title": "War and Peace",
},
map[string]interface{}{
"book_id": float64(1032), "title": "Crime and Punishment",
},
map[string]interface{}{
"book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
NbHits: 4,
Offset: 0,
Limit: 20,
ExhaustiveNbHits: false,
},
},
{
name: "TestIndexBasicSearchWithSortMultipleParameterPlaceHolder",
args: args{
UID: "indexUID",
client: defaultClient,
query: "",
sortableAttributes: []string{
"title",
"year",
},
request: SearchRequest{
Sort: []string{
"year:asc",
"title:asc",
},
Limit: 4,
},
},
want: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(56), "title": "The Divine Comedy",
},
map[string]interface{}{
"book_id": float64(32), "title": "The Odyssey",
},
map[string]interface{}{
"book_id": float64(69), "title": "Hamlet",
},
map[string]interface{}{
"book_id": float64(7), "title": "Don Quixote",
},
},
NbHits: 20,
Offset: 0,
Limit: 4,
ExhaustiveNbHits: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpIndexForFaceting()
c := tt.args.client
i := c.Index(tt.args.UID)
t.Cleanup(cleanup(c))

updateFilter, err := i.UpdateSortableAttributes(&tt.args.sortableAttributes)
require.NoError(t, err)
testWaitForPendingUpdate(t, i, updateFilter)

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{})["book_id"], got.Hits[len].(map[string]interface{})["book_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)
})
}
}
Loading