Skip to content

Commit

Permalink
Merge #558
Browse files Browse the repository at this point in the history
558: Feat sort facets value by alphanumerical or count order r=curquiza a=Ja7ad

# Pull Request

## Related issue
Fixes #469

## What does this PR do?
- Sort facets value by alphanumerical or count order

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Javad <ja7ad@live.com>
  • Loading branch information
meili-bors[bot] and Ja7ad authored Aug 20, 2024
2 parents 17cedba + 05e0174 commit 310e6b9
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 172 deletions.
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

0 comments on commit 310e6b9

Please sign in to comment.