From 13d1554400a8c995e6ecdbb30354334fa008ca2d Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 21 Aug 2024 10:35:48 +0330 Subject: [PATCH] fix: add match strategy frequency --- .code-samples.meilisearch.yaml | 9 +++++++-- index_search_test.go | 26 ++++++++++++++++++++++++-- main_test.go | 1 + types.go | 18 +++++++++++++++++- types_easyjson.go | 2 +- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index bd508ab3..22d5e8d8 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -470,12 +470,17 @@ search_parameter_guide_show_ranking_score_details_1: |- }) search_parameter_guide_matching_strategy_1: |- resp, err := client.Index("movies").Search("big fat liar", &meilisearch.SearchRequest{ - MatchingStrategy: "last", + MatchingStrategy: Last, }) search_parameter_guide_matching_strategy_2: |- resp, err := client.Index("movies").Search("big fat liar", &meilisearch.SearchRequest{ - MatchingStrategy: "all", + MatchingStrategy: All, }) +search_parameter_guide_matching_strategy_3: |- + client.Index("movies").Search("white shirt", &meilisearch.SearchRequest{ + MatchingStrategy: Frequency, + }) + search_parameter_guide_hitsperpage_1: |- client.Index("movies").Search("", &meilisearch.SearchRequest{ HitsPerPage: 15, diff --git a/index_search_test.go b/index_search_test.go index b81ea071..d0373eff 100644 --- a/index_search_test.go +++ b/index_search_test.go @@ -236,7 +236,7 @@ func TestIndex_Search(t *testing.T) { "book_id": float64(123), "title": "Pride and Prejudice", }, }, - EstimatedTotalHits: 20, + EstimatedTotalHits: 21, Offset: 0, Limit: 1, }, @@ -553,6 +553,28 @@ func TestIndex_Search(t *testing.T) { }, wantErr: false, }, + { + name: "TestIndexSearchWithMatchStrategyFrequency", + args: args{ + UID: "indexUID", + client: sv, + query: "white shirt", + request: &SearchRequest{ + MatchingStrategy: Frequency, + }, + }, + want: &SearchResponse{ + Hits: []interface{}{ + map[string]interface{}{ + "book_id": float64(1039), "title": "The Girl in the white shirt", + }, + }, + EstimatedTotalHits: 1, + Offset: 0, + Limit: 20, + }, + wantErr: false, + }, { name: "TestIndexSearchWithInvalidIndex", args: args{ @@ -1296,7 +1318,7 @@ func TestIndex_SearchWithSort(t *testing.T) { "book_id": float64(7), "title": "Don Quixote", }, }, - EstimatedTotalHits: 20, + EstimatedTotalHits: 21, Offset: 0, Limit: 4, }, diff --git a/main_test.go b/main_test.go index 13e7c035..7fc408e2 100644 --- a/main_test.go +++ b/main_test.go @@ -234,6 +234,7 @@ func setUpIndexForFaceting(client ServiceManager) { {BookID: 254, Title: "Lolita", Tag: "Novel", Year: 1955}, {BookID: 921, Title: "The Brothers Karamazov", Tag: "Novel", Year: 1879}, {BookID: 1032, Title: "Crime and Punishment", Tag: "Crime fiction", Year: 1866}, + {BookID: 1039, Title: "The Girl in the white shirt", Tag: "white shirt", Year: 1999}, } task, err := idx.AddDocuments(booksTest) if err != nil { diff --git a/types.go b/types.go index 77d7dbd0..f23f2d07 100644 --- a/types.go +++ b/types.go @@ -123,6 +123,22 @@ type ( SortFacetType string // SortFacetType is type of facet sorting, alpha or count TaskStatus string // TaskStatus is the status of a task. ProximityPrecisionType string // ProximityPrecisionType accepts one of the ByWord or ByAttribute + MatchingStrategy string // MatchingStrategy one of the Last, All, Frequency +) + +const ( + // Last returns documents containing all the query terms first. If there are not enough results containing all + // query terms to meet the requested limit, Meilisearch will remove one query term at a time, + // starting from the end of the query. + Last MatchingStrategy = "last" + // All only returns documents that contain all query terms. Meilisearch will not match any more documents even + // if there aren't enough to meet the requested limit. + All MatchingStrategy = "all" + // Frequency returns documents containing all the query terms first. If there are not enough results containing + //all query terms to meet the requested limit, Meilisearch will remove one query term at a time, starting + //with the word that is the most frequent in the dataset. frequency effectively gives more weight to terms + //that appear less frequently in a set of results. + Frequency MatchingStrategy = "frequency" ) const ( @@ -372,7 +388,7 @@ type SearchRequest struct { AttributesToHighlight []string `json:"attributesToHighlight,omitempty"` HighlightPreTag string `json:"highlightPreTag,omitempty"` HighlightPostTag string `json:"highlightPostTag,omitempty"` - MatchingStrategy string `json:"matchingStrategy,omitempty"` + MatchingStrategy MatchingStrategy `json:"matchingStrategy,omitempty"` Filter interface{} `json:"filter,omitempty"` ShowMatchesPosition bool `json:"showMatchesPosition,omitempty"` ShowRankingScore bool `json:"showRankingScore,omitempty"` diff --git a/types_easyjson.go b/types_easyjson.go index a26a8d76..b859ee72 100644 --- a/types_easyjson.go +++ b/types_easyjson.go @@ -3092,7 +3092,7 @@ func easyjson6601e8cdDecodeGithubComMeilisearchMeilisearchGo18(in *jlexer.Lexer, case "highlightPostTag": out.HighlightPostTag = string(in.String()) case "matchingStrategy": - out.MatchingStrategy = string(in.String()) + out.MatchingStrategy = MatchingStrategy(in.String()) case "filter": if m, ok := out.Filter.(easyjson.Unmarshaler); ok { m.UnmarshalEasyJSON(in)