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

Feat sort facets value by alphanumerical or count order #558

Merged
merged 8 commits into from
Aug 20, 2024
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
12 changes: 11 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,19 @@ get_faceting_settings_1: |-
update_faceting_settings_1: |-
client.Index("books").UpdateFaceting(&meilisearch.Faceting{
MaxValuesPerFacet: 2,
SortFacetValuesBy: {
"*": SortFacetTypeAlpha,
"genres": SortFacetTypeCount,
}
})
reset_faceting_settings_1: |-
client.Index("books").ResetFaceting()
facet_search_2: |-
client.Index("books").UpdateFaceting(&meilisearch.Faceting{
SortFacetValuesBy: {
"genres": SortFacetTypeCount,
}
})
get_index_stats_1: |-
client.Index("movies").GetStats()
get_indexes_stats_1: |-
Expand Down Expand Up @@ -652,7 +662,7 @@ getting_started_faceting: |-
client.Index("movies").UpdateFaceting(&meilisearch.Faceting{
MaxValuesPerFacet: 2,
SortFacetValuesBy: {
"*": "count"
"*": SortFacetTypeCount,
}
})

Expand Down
88 changes: 83 additions & 5 deletions index_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,9 @@ func TestIndex_UpdateSettings(t *testing.T) {
},
Faceting: &Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
SearchCutoffMs: 150,
SeparatorTokens: make([]string, 0),
Expand Down Expand Up @@ -1749,6 +1752,9 @@ func TestIndex_UpdateSettings(t *testing.T) {
},
Faceting: &Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
SearchCutoffMs: 150,
SeparatorTokens: make([]string, 0),
Expand Down Expand Up @@ -2559,6 +2565,9 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) {
Pagination: &defaultPagination,
Faceting: &Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
SeparatorTokens: make([]string, 0),
NonSeparatorTokens: make([]string, 0),
Expand All @@ -2583,6 +2592,9 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) {
Pagination: &defaultPagination,
Faceting: &Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
SeparatorTokens: make([]string, 0),
NonSeparatorTokens: make([]string, 0),
Expand Down Expand Up @@ -3075,6 +3087,9 @@ func TestIndex_UpdateFaceting(t *testing.T) {
client: meili,
request: Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
},
wantTask: &TaskInfo{
Expand All @@ -3089,13 +3104,80 @@ func TestIndex_UpdateFaceting(t *testing.T) {
client: customMeili,
request: Faceting{
MaxValuesPerFacet: 200,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
},
},
wantTask: &TaskInfo{
TaskUID: 1,
},
wantResp: &defaultFaceting,
},
{
name: "TestIndexGetStartedFaceting",
args: args{
UID: "indexUID",
client: meili,
request: Faceting{
MaxValuesPerFacet: 2,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeCount,
},
},
},
wantTask: &TaskInfo{
TaskUID: 1,
},
wantResp: &Faceting{
MaxValuesPerFacet: 2,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeCount,
},
},
},
{
name: "TestIndexSortFacetValuesByCountFaceting",
args: args{
UID: "indexUID",
client: meili,
request: Faceting{
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
"indexUID": SortFacetTypeCount,
},
},
},
wantTask: &TaskInfo{
TaskUID: 1,
},
wantResp: &Faceting{
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
"indexUID": SortFacetTypeCount,
},
},
},
{
name: "TestIndexSortFacetValuesAllIndexFaceting",
args: args{
UID: "indexUID",
client: meili,
request: Faceting{
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeCount,
},
},
},
wantTask: &TaskInfo{
TaskUID: 1,
},
wantResp: &Faceting{
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeCount,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -3104,16 +3186,12 @@ func TestIndex_UpdateFaceting(t *testing.T) {
i := c.Index(tt.args.UID)
t.Cleanup(cleanup(c))

gotResp, err := i.GetFaceting()
require.NoError(t, err)
require.Equal(t, tt.wantResp, gotResp)

gotTask, err := i.UpdateFaceting(&tt.args.request)
require.NoError(t, err)
require.GreaterOrEqual(t, gotTask.TaskUID, tt.wantTask.TaskUID)
testWaitForTask(t, i, gotTask)

gotResp, err = i.GetFaceting()
gotResp, err := i.GetFaceting()
require.NoError(t, err)
require.Equal(t, &tt.args.request, gotResp)
})
Expand Down
3 changes: 3 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (
}
defaultFaceting = Faceting{
MaxValuesPerFacet: 100,
SortFacetValuesBy: map[string]SortFacetType{
"*": SortFacetTypeAlpha,
},
}
)

Expand Down
15 changes: 11 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type Pagination struct {
// Faceting is the type that represents the faceting setting in meilisearch
type Faceting struct {
MaxValuesPerFacet int64 `json:"maxValuesPerFacet"`
// SortFacetValuesBy index_name: alpha|count
SortFacetValuesBy map[string]SortFacetType `json:"sortFacetValuesBy"`
}

type Embedder struct {
Expand Down Expand Up @@ -114,8 +116,11 @@ type Stats struct {
Indexes map[string]StatsIndex `json:"indexes"`
}

// TaskStatus is the status of a task.
type TaskStatus string
type (
TaskType string // TaskType is the type of a task
SortFacetType string // SortFacetType is type of facet sorting, alpha or count
TaskStatus string // TaskStatus is the status of a task.
)

const (
// TaskStatusUnknown is the default TaskStatus, should not exist
Expand All @@ -132,8 +137,10 @@ const (
TaskStatusCanceled TaskStatus = "canceled"
)

// TaskType is the type of a task
type TaskType string
const (
SortFacetTypeAlpha SortFacetType = "alpha"
SortFacetTypeCount SortFacetType = "count"
)

const (
// TaskTypeIndexCreation represents an index creation
Expand Down
Loading
Loading