Skip to content

Commit 4913acb

Browse files
Merge #417
417: [1.4.0] Refactor Settings and Add Pagination, Separator and Dictionary Settings r=curquiza a=Sherlouk # Pull Request ## Related issue Fixes #308 Fixes changes raised under meilisearch/integration-guides#286 (#406) - ⚠️ Wait for v1.4.0 ## What does this PR do? - Refactors index settings to improve reuse (functionally identical, but around 300 less lines of code) - Cleaning up some unsafe practices in unit tests - Adds support for three new types of index-level settings (improving parity) All integration tests have been run against v1.4.0 (pre-release) RC 2 and is running locally. CI uses the latest version of the executable (1.3.0) and thus will not work and will throw errors. This is expected. This PR won't be merged until after the 1.4.0 release due to Swift being a tier 3 project, as such we can simply re-run CI once that's released. This PR starts to contain a little bit of cleanup with some various nit picks (nothing that changes the code style though). ## 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: James Sherlock <[email protected]>
2 parents 4c6d2f4 + d15cc4b commit 4913acb

File tree

12 files changed

+944
-535
lines changed

12 files changed

+944
-535
lines changed

.code-samples.meilisearch.yaml

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,44 @@ async_guide_canceled_by: |-
168168
print(error)
169169
}
170170
}
171+
get_dictionary_1: |-
172+
client.index("books").getDictionary { result in
173+
// handle result
174+
}
175+
update_dictionary_1: |-
176+
client.index("books").updateDictionary(["J. R. R.", "W. E. B."]) { result in
177+
// handle result
178+
}
179+
reset_dictionary_1: |-
180+
client.index("books").resetDictionary { result in
181+
// handle result
182+
}
183+
get_separator_tokens_1: |-
184+
client.index("books").getSeparatorTokens { result in
185+
// handle result
186+
}
187+
update_separator_tokens_1: |-
188+
client.index("books").updateSeparatorTokens(["|", "&hellip;"]) { result in
189+
// handle result
190+
}
191+
reset_separator_tokens_1: |-
192+
client.index("books").resetSeparatorTokens { result in
193+
// handle result
194+
}
195+
get_non_separator_tokens_1: |-
196+
client.index("books").getNonSeparatorTokens { result in
197+
// handle result
198+
}
199+
update_non_separator_tokens_1: |-
200+
client.index("books").updateNonSeparatorTokens(["@", "#"]) { result in
201+
// handle result
202+
}
203+
reset_non_separator_tokens_1: |-
204+
client.index("books").resetNonSeparatorTokens { result in
205+
// handle result
206+
}
171207
search_parameter_guide_hitsperpage_1: |-
172-
let searchParameters = SearchParameters.query("", hitsPerPage: 15)
208+
let searchParameters = SearchParameters(query: "", hitsPerPage: 15)
173209
client.index("movies").search(searchParameters) { (result: Result<Searchable<Movie>, Swift.Error>) in
174210
switch result {
175211
case .success(let searchResult):
@@ -179,7 +215,7 @@ search_parameter_guide_hitsperpage_1: |-
179215
}
180216
}
181217
search_parameter_guide_page_1: |-
182-
let searchParameters = SearchParameters.query("", page: 15)
218+
let searchParameters = SearchParameters(query: "", page: 15)
183219
client.index("movies").search(searchParameters) { (result: Result<Searchable<Movie>, Swift.Error>) in
184220
switch result {
185221
case .success(let searchResult):
@@ -362,7 +398,7 @@ delete_documents_1: |-
362398
}
363399
}
364400
search_post_1: |-
365-
let searchParameters = SearchParameters.query("American ninja")
401+
let searchParameters = SearchParameters(query: "American ninja")
366402
client.index("movies").search(searchParameters) { (result: Result<Searchable<Movie>, Swift.Error>) in
367403
switch result {
368404
case .success(let searchResult):
@@ -458,7 +494,7 @@ delete_a_key_1: |-
458494
security_guide_search_key_1: |-
459495
client = try MeiliSearch(host: "http://localhost:7700", apiKey: "apiKey")
460496
client.index("patient_medical_records")
461-
.search(SearchParameters.query("")) { (result: Result<Searchable<Patient>, Swift.Error>) in
497+
.search(SearchParameters(query: "")) { (result: Result<Searchable<Patient>, Swift.Error>) in
462498
switch result {
463499
case .success(let searchResult):
464500
print(searchResult)
@@ -1454,7 +1490,7 @@ geosearch_guide_filter_usage_2: |-
14541490
}
14551491
geosearch_guide_filter_usage_3: |-
14561492
let searchParameters = SearchParameters(
1457-
filter: '_geoBoundingBox([45.494181, 9.214024], [45.449484, 9.179175])'
1493+
filter: "_geoBoundingBox([45.494181, 9.214024], [45.449484, 9.179175])"
14581494
)
14591495
client.index("restaurants").search(searchParameters) { (result: Result<Searchable<Restaurant>, Swift.Error>) in
14601496
switch result {

Sources/MeiliSearch/Indexes.swift

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,167 @@ public struct Indexes {
867867
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
868868
self.settings.resetSortableAttributes(self.uid, completion)
869869
}
870+
871+
// MARK: Separator Tokens
872+
873+
/**
874+
Fetch the `separatorTokens` setting of a Meilisearch index.
875+
876+
- parameter completion: The completion closure is used to notify when the server
877+
completes the query request, it returns a `Result` object that contains an `[String]`
878+
value if the request was successful, or `Error` if a failure occurred.
879+
*/
880+
public func getSeparatorTokens(
881+
_ completion: @escaping (Result<[String], Swift.Error>) -> Void) {
882+
self.settings.getSeparatorTokens(self.uid, completion)
883+
}
884+
885+
/**
886+
Modify the `separatorTokens` setting of a Meilisearch index.
887+
888+
- parameter attributes: List of tokens that will be considered as word separators
889+
- parameter completion: The completion closure is used to notify when the server
890+
completes the query request, it returns a `Result` object that contains `TaskInfo`
891+
value if the request was successful, or `Error` if a failure occurred.
892+
*/
893+
public func updateSeparatorTokens(
894+
_ attributes: [String],
895+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
896+
self.settings.updateSeparatorTokens(self.uid, attributes, completion)
897+
}
898+
899+
/**
900+
Reset the `separatorTokens` setting of a Meilisearch index to the default value `[]`.
901+
902+
- parameter completion: The completion closure is used to notify when the server
903+
completes the query request, it returns a `Result` object that contains `TaskInfo`
904+
value if the request was successful, or `Error` if a failure occurred.
905+
*/
906+
public func resetSeparatorTokens(
907+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
908+
self.settings.resetSeparatorTokens(self.uid, completion)
909+
}
910+
911+
// MARK: Non Separator Tokens
912+
913+
/**
914+
Fetch the `nonSeparatorTokens` setting of a Meilisearch index.
915+
916+
- parameter completion: The completion closure is used to notify when the server
917+
completes the query request, it returns a `Result` object that contains an `[String]`
918+
value if the request was successful, or `Error` if a failure occurred.
919+
*/
920+
public func getNonSeparatorTokens(
921+
_ completion: @escaping (Result<[String], Swift.Error>) -> Void) {
922+
self.settings.getNonSeparatorTokens(self.uid, completion)
923+
}
924+
925+
/**
926+
Modify the `nonSeparatorTokens` setting of a Meilisearch index.
927+
928+
- parameter attributes: List of tokens that will not be considered as word separators
929+
- parameter completion: The completion closure is used to notify when the server
930+
completes the query request, it returns a `Result` object that contains `TaskInfo`
931+
value if the request was successful, or `Error` if a failure occurred.
932+
*/
933+
public func updateNonSeparatorTokens(
934+
_ attributes: [String],
935+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
936+
self.settings.updateNonSeparatorTokens(self.uid, attributes, completion)
937+
}
938+
939+
/**
940+
Reset the `nonSeparatorTokens` setting of a Meilisearch index to the default value `[]`.
941+
942+
- parameter completion: The completion closure is used to notify when the server
943+
completes the query request, it returns a `Result` object that contains `TaskInfo`
944+
value if the request was successful, or `Error` if a failure occurred.
945+
*/
946+
public func resetNonSeparatorTokens(
947+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
948+
self.settings.resetNonSeparatorTokens(self.uid, completion)
949+
}
950+
951+
// MARK: Dictionary
952+
953+
/**
954+
Fetch the `dictionary` setting of a Meilisearch index.
955+
956+
- parameter completion: The completion closure is used to notify when the server
957+
completes the query request, it returns a `Result` object that contains an `[String]`
958+
value if the request was successful, or `Error` if a failure occurred.
959+
*/
960+
public func getDictionary(
961+
_ completion: @escaping (Result<[String], Swift.Error>) -> Void) {
962+
self.settings.getDictionary(self.uid, completion)
963+
}
964+
965+
/**
966+
Modify the `dictionary` setting of a Meilisearch index.
967+
968+
- parameter attributes: List of words on which the segmentation will be overridden
969+
- parameter completion: The completion closure is used to notify when the server
970+
completes the query request, it returns a `Result` object that contains `TaskInfo`
971+
value if the request was successful, or `Error` if a failure occurred.
972+
*/
973+
public func updateDictionary(
974+
_ attributes: [String],
975+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
976+
self.settings.updateDictionary(self.uid, attributes, completion)
977+
}
978+
979+
/**
980+
Reset the `dictionary` setting of a Meilisearch index to the default value `[]`.
981+
982+
- parameter completion: The completion closure is used to notify when the server
983+
completes the query request, it returns a `Result` object that contains `TaskInfo`
984+
value if the request was successful, or `Error` if a failure occurred.
985+
*/
986+
public func resetDictionary(
987+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
988+
self.settings.resetDictionary(self.uid, completion)
989+
}
990+
991+
// MARK: Pagination
992+
993+
/**
994+
Get the pagination settings for the current index.
995+
996+
- parameter completion: The completion closure is used to notify when the server
997+
completes the query request, it returns a `Result` object that contains an `Pagination`
998+
value if the request was successful, or `Error` if a failure occurred.
999+
*/
1000+
public func getPaginationSettings(
1001+
_ completion: @escaping (Result<Pagination, Swift.Error>) -> Void) {
1002+
self.settings.getPaginationSettings(self.uid, completion)
1003+
}
1004+
1005+
/**
1006+
Updates the pagination setting for the index.
1007+
1008+
- parameter settings: The new preferences to use for pagination.
1009+
- parameter completion: The completion closure is used to notify when the server
1010+
completes the query request, it returns a `Result` object that contains `TaskInfo`
1011+
value if the request was successful, or `Error` if a failure occurred.
1012+
*/
1013+
public func updatePaginationSettings(
1014+
_ settings: Pagination,
1015+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
1016+
self.settings.updatePaginationSettings(self.uid, settings, completion)
1017+
}
1018+
1019+
/**
1020+
Reset the pagination settings for the index.
1021+
1022+
- parameter completion: The completion closure is used to notify when the server
1023+
completes the query request, it returns a `Result` object that contains `TaskInfo`
1024+
value if the request was successful, or `Error` if a failure occurred.
1025+
*/
1026+
public func resetPaginationSettings(
1027+
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
1028+
self.settings.resetPaginationSettings(self.uid, completion)
1029+
}
1030+
8701031
// MARK: Stats
8711032

8721033
/**

Sources/MeiliSearch/Model/FacetStats.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public struct FacetStats: Codable, Equatable {
88

99
/// The minimum value found in the given facet
1010
public let min: Double
11-
11+
1212
/// The maximum value found in the given facet
1313
public let max: Double
1414
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
/**
4+
`Pagination` is a wrapper used in the index pagination setting routes to handle the returned data.
5+
*/
6+
public struct Pagination: Codable, Equatable {
7+
/// The maximum number of hits (document records) which can be returned by a single search request.
8+
/// By default, Meilisearch returns 1000 results per search. This limit protects your database from malicious scraping.
9+
public let maxTotalHits: Int
10+
11+
public init(maxTotalHits: Int) {
12+
self.maxTotalHits = maxTotalHits
13+
}
14+
}

Sources/MeiliSearch/Model/SearchResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Searchable<T>: Equatable, Codable where T: Codable, T: Equatable {
1515

1616
/// Distribution of the given facets.
1717
public var facetDistribution: [String: [String: Int]]?
18-
18+
1919
/// Maximum & minimum stats of a numeric facet.
2020
public var facetStats: [String: FacetStats]?
2121

Sources/MeiliSearch/Model/Setting.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public struct Setting: Codable, Equatable {
3333
/// List of attributes used for sorting
3434
public let sortableAttributes: [String]?
3535

36+
/// List of tokens that will be considered as word separators by Meilisearch.
37+
public let separatorTokens: [String]?
38+
39+
/// List of tokens that will not be considered as word separators by Meilisearch.
40+
public let nonSeparatorTokens: [String]?
41+
42+
/// List of words on which the segmentation will be overridden.
43+
public let dictionary: [String]?
44+
45+
/// Pagination settings for the current index
46+
public let pagination: Pagination?
47+
3648
// MARK: Initializers
3749

3850
public init(
@@ -43,7 +55,11 @@ public struct Setting: Codable, Equatable {
4355
synonyms: [String: [String]]? = nil,
4456
distinctAttribute: String? = nil,
4557
filterableAttributes: [String]? = nil,
46-
sortableAttributes: [String]? = nil
58+
sortableAttributes: [String]? = nil,
59+
separatorTokens: [String]? = nil,
60+
nonSeparatorTokens: [String]? = nil,
61+
dictionary: [String]? = nil,
62+
pagination: Pagination? = nil
4763
) {
4864
self.rankingRules = rankingRules
4965
self.searchableAttributes = searchableAttributes
@@ -53,5 +69,9 @@ public struct Setting: Codable, Equatable {
5369
self.distinctAttribute = distinctAttribute
5470
self.filterableAttributes = filterableAttributes
5571
self.sortableAttributes = sortableAttributes
72+
self.nonSeparatorTokens = nonSeparatorTokens
73+
self.separatorTokens = separatorTokens
74+
self.dictionary = dictionary
75+
self.pagination = pagination
5676
}
5777
}

Sources/MeiliSearch/Model/SettingResult.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ public struct SettingResult: Codable, Equatable {
2727

2828
/// List of attributes used for sorting
2929
public let sortableAttributes: [String]
30+
31+
/// List of tokens that will be considered as word separators by Meilisearch.
32+
public let separatorTokens: [String]
33+
34+
/// List of tokens that will not be considered as word separators by Meilisearch.
35+
public let nonSeparatorTokens: [String]
36+
37+
/// List of words on which the segmentation will be overridden.
38+
public let dictionary: [String]
39+
40+
/// Pagination settings for the current index
41+
public let pagination: Pagination
3042
}

Sources/MeiliSearch/Model/Task.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public struct Task: Codable, Equatable {
7676
/// Distinct attribute on settings actions
7777
public let distinctAttribute: String?
7878

79+
/// List of tokens that will be considered as word separators by Meilisearch.
80+
public let separatorTokens: [String]?
81+
82+
/// List of tokens that will not be considered as word separators by Meilisearch.
83+
public let nonSeparatorTokens: [String]?
84+
85+
/// List of words on which the segmentation will be overridden.
86+
public let dictionary: [String]?
87+
88+
/// Settings for index level pagination rules
89+
public let pagination: Pagination?
90+
7991
}
8092
/// Error information in case of failed update.
8193
public let error: MeiliSearch.MSErrorResponse?

0 commit comments

Comments
 (0)