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

Add typo-tolerance APIs #411

Merged
merged 8 commits into from
Sep 27, 2023
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
40 changes: 40 additions & 0 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,46 @@ public struct Indexes {
self.settings.resetPaginationSettings(self.uid, completion)
}

// MARK: Typo Tolerance

/**
Get the typo tolerance settings.

- parameter completion: The completion closure is used to notify when the server
completes the query request, it returns a `Result` object that contains a `TypoToleranceResult`
value if the request was successful, or `Error` if a failure occurred.
*/
public func getTypoTolerance(
_ completion: @escaping (Result<TypoToleranceResult, Swift.Error>) -> Void) {
self.settings.getTypoTolerance(self.uid, completion)
}

/**
Update the typo tolerance settings.

- parameter typoTolerance: An object containing the settings for the `Index`.
- parameter completion: The completion closure is used to notify when the server
completes the query request, it returns a `Result` object that contains `TaskInfo`
value if the request was successful, or `Error` if a failure occurred.
*/
public func updateTypoTolerance(
_ typoTolerance: TypoTolerance,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
self.settings.updateTypoTolerance(self.uid, typoTolerance, completion)
}

/**
Reset the typo tolerance settings.

- parameter completion: The completion closure is used to notify when the server
completes the query request, it returns a `Result` object that contains `TaskInfo`
value if the request was successful, or `Error` if a failure occurred.
*/
public func resetTypoTolerance(
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {
self.settings.resetTypoTolerance(self.uid, completion)
}

// MARK: Stats

/**
Expand Down
9 changes: 7 additions & 2 deletions Sources/MeiliSearch/Model/Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
#endif

/**
Settings object provided byb the user
Settings object provided by the user
*/
public struct Setting: Codable, Equatable {
// MARK: Properties
Expand Down Expand Up @@ -33,6 +33,9 @@ public struct Setting: Codable, Equatable {
/// List of attributes used for sorting
public let sortableAttributes: [String]?

/// Settings for typo tolerance
public let typoTolerance: TypoTolerance?

/// List of tokens that will be considered as word separators by Meilisearch.
public let separatorTokens: [String]?

Expand All @@ -59,7 +62,8 @@ public struct Setting: Codable, Equatable {
separatorTokens: [String]? = nil,
nonSeparatorTokens: [String]? = nil,
dictionary: [String]? = nil,
pagination: Pagination? = nil
pagination: Pagination? = nil,
typoTolerance: TypoTolerance? = nil
) {
self.rankingRules = rankingRules
self.searchableAttributes = searchableAttributes
Expand All @@ -73,5 +77,6 @@ public struct Setting: Codable, Equatable {
self.separatorTokens = separatorTokens
self.dictionary = dictionary
self.pagination = pagination
self.typoTolerance = typoTolerance
}
}
3 changes: 3 additions & 0 deletions Sources/MeiliSearch/Model/SettingResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public struct SettingResult: Codable, Equatable {

/// Pagination settings for the current index
public let pagination: Pagination

/// Settings for typo tolerance
public let typoTolerance: TypoToleranceResult
}
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/Model/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public struct Task: Codable, Equatable {
/// Settings for index level pagination rules
public let pagination: Pagination?

/// Typo tolerance on settings actions
public let typoTolerance: TypoTolerance?
}
/// Error information in case of failed update.
public let error: MeiliSearch.MSErrorResponse?
Expand Down
48 changes: 48 additions & 0 deletions Sources/MeiliSearch/Model/TypoTolerance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation

/**
`TypoTolerance` settings provided by the user.
*/
public struct TypoTolerance: Codable, Equatable {
// MARK: Properties

/// Whether typo tolerance is enabled or not
public let enabled: Bool?

/// The minimum word size for accepting typos
public let minWordSizeForTypos: MinWordSize?

/// An array of words for which the typo tolerance feature is disabled
public let disableOnWords: [String]?

/// An array of attributes for which the typo tolerance feature is disabled
public let disableOnAttributes: [String]?

public struct MinWordSize: Codable, Equatable {
/// The minimum word size for accepting 1 typo; must be between 0 and `twoTypos`
public let oneTypo: Int?

/// The minimum word size for accepting 2 typos; must be between `oneTypo` and 255
public let twoTypos: Int?

public init(
oneTypo: Int? = nil,
twoTypos: Int? = nil
) {
self.oneTypo = oneTypo
self.twoTypos = twoTypos
}
}

public init(
enabled: Bool? = nil,
minWordSizeForTypos: TypoTolerance.MinWordSize? = nil,
disableOnWords: [String]? = nil,
disableOnAttributes: [String]? = nil
) {
self.enabled = enabled
self.minWordSizeForTypos = minWordSizeForTypos
self.disableOnWords = disableOnWords
self.disableOnAttributes = disableOnAttributes
}
}
28 changes: 28 additions & 0 deletions Sources/MeiliSearch/Model/TypoToleranceResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

/**
`TypoToleranceResult` instances represent the current typo tolerance settings.
*/
public struct TypoToleranceResult: Codable, Equatable {
// MARK: Properties

/// Whether typo tolerance is enabled or not
public let enabled: Bool

/// The minimum word size for accepting typos
public let minWordSizeForTypos: MinWordSize

/// An array of words for which the typo tolerance feature is disabled
public let disableOnWords: [String]

/// An array of attributes for which the typo tolerance feature is disabled
public let disableOnAttributes: [String]

public struct MinWordSize: Codable, Equatable {
/// The minimum word size for accepting 1 typo; must be between 0 and `twoTypos`
public let oneTypo: Int

/// The minimum word size for accepting 2 typos; must be between `oneTypo` and 255
public let twoTypos: Int
}
}
45 changes: 45 additions & 0 deletions Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,51 @@ struct Settings {
resetSetting(uid: uid, key: "pagination", completion: completion)
}

// MARK: Typo Tolerance

func getTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TypoToleranceResult, Swift.Error>) -> Void) {

getSetting(uid: uid, key: "typo-tolerance", completion: completion)
}

func updateTypoTolerance(
_ uid: String,
_ setting: TypoTolerance,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
data = try JSONEncoder().encode(setting)
} catch {
completion(.failure(error))
return
}

// this uses patch instead of put for networking, so shouldn't use the reusable 'updateSetting' function
self.request.patch(api: "/indexes/\(uid)/settings/typo-tolerance", data) { result in
switch result {
case .success(let data):
do {
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

func resetTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

resetSetting(uid: uid, key: "typo-tolerance", completion: completion)
}

// MARK: Reusable Requests

private func getSetting<ResponseType: Decodable>(
Expand Down
121 changes: 116 additions & 5 deletions Tests/MeiliSearchIntegrationTests/SettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
private let defaultStopWords: [String] = []
private let defaultSynonyms: [String: [String]] = [:]
private let defaultPagination: Pagination = .init(maxTotalHits: 1000)
private let defaultTypoTolerance: TypoTolerance = .init(
enabled: true,
minWordSizeForTypos: .init(oneTypo: 5, twoTypos: 9),
disableOnWords: [],
disableOnAttributes: []
)
private let defaultTypoToleranceResult: TypoToleranceResult = .init(
enabled: true,
minWordSizeForTypos: .init(oneTypo: 5, twoTypos: 9),
disableOnWords: [],
disableOnAttributes: []
)
private var defaultGlobalSettings: Setting?
private var defaultGlobalReturnedSettings: SettingResult?

Expand Down Expand Up @@ -68,7 +80,8 @@
separatorTokens: self.defaultSeparatorTokens,
nonSeparatorTokens: self.defaultNonSeparatorTokens,
dictionary: self.defaultDictionary,
pagination: self.defaultPagination
pagination: self.defaultPagination,
typoTolerance: self.defaultTypoTolerance
)

self.defaultGlobalReturnedSettings = SettingResult(
Expand All @@ -83,7 +96,8 @@
separatorTokens: self.defaultSeparatorTokens,
nonSeparatorTokens: self.defaultNonSeparatorTokens,
dictionary: self.defaultDictionary,
pagination: self.defaultPagination
pagination: self.defaultPagination,
typoTolerance: self.defaultTypoToleranceResult
)
}

Expand Down Expand Up @@ -324,11 +338,11 @@
case .success(let task):
self.client.waitForTask(task: task) { result in
switch result {
case .success(let task):

Check warning on line 341 in Tests/MeiliSearchIntegrationTests/SettingsTests.swift

View workflow job for this annotation

GitHub Actions / linter-check

Switch and Case Statement Alignment Violation: Case statements should vertically aligned with their closing brace (switch_case_alignment)
XCTAssertEqual("settingsUpdate", task.type)
XCTAssertEqual(Task.Status.succeeded, task.status)
expectation.fulfill()
case .failure(let error):

Check warning on line 345 in Tests/MeiliSearchIntegrationTests/SettingsTests.swift

View workflow job for this annotation

GitHub Actions / linter-check

Switch and Case Statement Alignment Violation: Case statements should vertically aligned with their closing brace (switch_case_alignment)
dump(error)
XCTFail("Failed to wait for task")
expectation.fulfill()
Expand Down Expand Up @@ -525,6 +539,100 @@
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Typo Tolerance

func testGetTypoTolerance() {
let expectation = XCTestExpectation(description: "Get current typo tolerance")

self.index.getTypoTolerance { result in
switch result {
case .success(let typoTolerance):
XCTAssertEqual(self.defaultTypoToleranceResult, typoTolerance)
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to get typo tolerance")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

func testUpdateTypoTolerance() {
let expectation = XCTestExpectation(description: "Update settings for typo tolerance")

let newTypoTolerance: TypoTolerance = .init(
minWordSizeForTypos: .init(
oneTypo: 1,
twoTypos: 2
),
disableOnWords: ["to"],
disableOnAttributes: ["genre"]
)

self.index.updateTypoTolerance(newTypoTolerance) { result in
switch result {
case .success(let task):
self.client.waitForTask(task: task) { result in
switch result {
case .success(let task):
XCTAssertEqual("settingsUpdate", task.type)
XCTAssertEqual(Task.Status.succeeded, task.status)
if let details = task.details {
if let typoTolerance = details.typoTolerance {
XCTAssertEqual(newTypoTolerance, typoTolerance)
} else {
XCTFail("typoTolerance should not be nil")
}
} else {
XCTFail("details should exists in details field of task")
}
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to wait for task")
expectation.fulfill()
}
}
case .failure(let error):
dump(error)
XCTFail("Failed updating typo tolerance")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

func testResetTypoTolerance() {
let expectation = XCTestExpectation(description: "Reset settings for typo tolerance")

self.index.resetTypoTolerance { result in
switch result {
case .success(let task):
self.client.waitForTask(task: task) { result in
switch result {
case .success(let task):
XCTAssertEqual("settingsUpdate", task.type)
XCTAssertEqual(Task.Status.succeeded, task.status)
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to wait for task")
expectation.fulfill()
}
}
case .failure(let error):
dump(error)
XCTFail("Failed reseting typo tolerance")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Separator Tokens

func testGetSeparatorTokens() {
Expand Down Expand Up @@ -1260,7 +1368,8 @@
separatorTokens: [],
nonSeparatorTokens: [],
dictionary: [],
pagination: .init(maxTotalHits: 1000)
pagination: .init(maxTotalHits: 1000),
typoTolerance: defaultTypoToleranceResult
)

let expectation = XCTestExpectation(description: "Update settings")
Expand Down Expand Up @@ -1341,7 +1450,8 @@
separatorTokens: ["&"],
nonSeparatorTokens: ["#"],
dictionary: ["J.K"],
pagination: .init(maxTotalHits: 500)
pagination: .init(maxTotalHits: 500),
typoTolerance: nil
)

let expectedSettingResult = SettingResult(
Expand All @@ -1356,7 +1466,8 @@
separatorTokens: ["&"],
nonSeparatorTokens: ["#"],
dictionary: ["J.K"],
pagination: .init(maxTotalHits: 500)
pagination: .init(maxTotalHits: 500),
typoTolerance: defaultTypoToleranceResult
)

self.index.updateSettings(newSettings) { result in
Expand Down
Loading
Loading