Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 01eb6e7
Author: Hayden <hayden.hhoang@gmail.com>
Date:   Wed Aug 7 18:26:08 2024 +0700

    update README: add stopwords documentation

commit ba8256b
Author: Hayden <hayden.hhoang@gmail.com>
Date:   Wed Aug 7 18:23:22 2024 +0700

    feat: add stopwords endpoint
  • Loading branch information
haydenhoang committed Aug 7, 2024
1 parent 96b8eb6 commit 5e62e20
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@ switch data?.value {
let (data, response) = try await client.preset("listing_view").delete()
```

### Create or update a stopwords set

```swift
let schema = StopwordsSetUpsertSchema(
stopwords: ["states","united"],
locale: "en"
)
let (data, response) = try await client.stopwords().upsert(stopwordsSetId: "stopword_set1", params: schema)
```

### Retrieve all stopwords sets

```swift
let (data, response) = try await client.stopwords().retrieve()
```

### Retrieve a stopwords set

```swift
let (data, response) = try await client.stopword("stopword_set1").retrieve()
```

### Delete a preset

```swift
let (data, response) = try await client.stopword("stopword_set1").delete()
```

## Contributing

Issues and pull requests are welcome on GitHub at [Typesense Swift](https://github.com/typesense/typesense-swift). Do note that the Models used in the Swift client are generated by [Swagger-Codegen](https://github.com/swagger-api/swagger-codegen) and are automated to be modified in order to prevent major errors. So please do use the shell script that is provided in the repo to generate the models:
Expand Down
8 changes: 8 additions & 0 deletions Sources/Typesense/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ public struct Client {
public func preset(_ presetName: String) -> Preset {
return Preset(apiCall: apiCall, presetName: presetName)
}

public func stopwords() -> Stopwords {
return Stopwords(apiCall: apiCall)
}

public func stopword(_ stopwordsSetId: String) -> Stopword {
return Stopword(apiCall: apiCall, stopwordsSetId: stopwordsSetId)
}
}
16 changes: 16 additions & 0 deletions Sources/Typesense/Models/StopwordsSetDeleteSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation



public struct StopwordsSetDeleteSchema: Codable {
public var _id: String

public init(_id: String) {
self._id = _id
}

public enum CodingKeys: String, CodingKey {
case _id = "id"
}

}
21 changes: 21 additions & 0 deletions Sources/Typesense/Models/StopwordsSetRetrieveSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// StopwordsSetRetrieveSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetRetrieveSchema: Codable {

public var stopwords: StopwordsSetSchema

public init(stopwords: StopwordsSetSchema) {
self.stopwords = stopwords
}


}
30 changes: 30 additions & 0 deletions Sources/Typesense/Models/StopwordsSetSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// StopwordsSetSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetSchema: Codable {

public var _id: String
public var stopwords: [String]
public var locale: String?

public init(_id: String, stopwords: [String], locale: String? = nil) {
self._id = _id
self.stopwords = stopwords
self.locale = locale
}

public enum CodingKeys: String, CodingKey {
case _id = "id"
case stopwords
case locale
}

}
23 changes: 23 additions & 0 deletions Sources/Typesense/Models/StopwordsSetUpsertSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// StopwordsSetUpsertSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetUpsertSchema: Codable {

public var stopwords: [String]
public var locale: String?

public init(stopwords: [String], locale: String? = nil) {
self.stopwords = stopwords
self.locale = locale
}


}
21 changes: 21 additions & 0 deletions Sources/Typesense/Models/StopwordsSetsRetrieveAllSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// StopwordsSetsRetrieveAllSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetsRetrieveAllSchema: Codable {

public var stopwords: [StopwordsSetSchema]

public init(stopwords: [StopwordsSetSchema]) {
self.stopwords = stopwords
}


}
39 changes: 39 additions & 0 deletions Sources/Typesense/Stopword.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public struct Stopword {
private var apiCall: ApiCall
private var stopwordsSetId: String


init(apiCall: ApiCall, stopwordsSetId: String) {
self.apiCall = apiCall
self.stopwordsSetId = stopwordsSetId
}

public func retrieve() async throws -> (StopwordsSetSchema?, URLResponse?) {
let (data, response) = try await apiCall.get(endPoint: endpointPath())
if let result = data {
let decodedData = try decoder.decode(StopwordsSetRetrieveSchema.self, from: result)
return (decodedData.stopwords, response)
}
return (nil, response)
}

public func delete() async throws -> (StopwordsSetDeleteSchema?, URLResponse?) {
let (data, response) = try await apiCall.delete(endPoint: endpointPath())
if let result = data {
let decodedData = try decoder.decode(StopwordsSetDeleteSchema.self, from: result)
return (decodedData, response)
}
return (nil, response)
}

private func endpointPath() -> String {
return "\(Stopwords.RESOURCEPATH)/\(stopwordsSetId)"
}


}
44 changes: 44 additions & 0 deletions Sources/Typesense/Stopwords.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public struct Stopwords {
static let RESOURCEPATH = "stopwords"
private var apiCall: ApiCall


init(apiCall: ApiCall) {
self.apiCall = apiCall
}

public func upsert(stopwordsSetId: String, params: StopwordsSetUpsertSchema) async throws -> (StopwordsSetSchema?, URLResponse?) {
let schemaData = try encoder.encode(params)
let (data, response) = try await self.apiCall.put(endPoint: endpointPath(stopwordsSetId), body: schemaData)
if let result = data {
let decodedData = try decoder.decode(StopwordsSetSchema.self, from: result)
return (decodedData, response)
}
return (nil, response)
}

public func retrieve() async throws -> ([StopwordsSetSchema]?, URLResponse?) {
let (data, response) = try await self.apiCall.get(endPoint: endpointPath())
if let result = data {
let decodedData = try decoder.decode(StopwordsSetsRetrieveAllSchema.self, from: result)
return (decodedData.stopwords, response)
}
return (nil, response)
}

private func endpointPath(_ operation: String? = nil) -> String {
let baseEndpoint = "\(Stopwords.RESOURCEPATH)"
if let operation = operation {
return "\(baseEndpoint)/\(operation)"
} else {
return baseEndpoint
}
}


}
42 changes: 42 additions & 0 deletions Tests/TypesenseTests/StopwordTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import XCTest
@testable import Typesense

final class StopwordTests: XCTestCase {
override func tearDown() async throws {
try! await tearDownStopwords()
}

func testStopwordRetrieve() async {
try! await createStopwordSet()
do {
let (result, _) = try await client.stopword("test-id-stopword-set").retrieve()
XCTAssertNotNil(result)
guard let validResult = result else {
throw DataError.dataNotFound
}
print(validResult)
XCTAssertEqual("test-id-stopword-set", validResult._id)
XCTAssertEqual(["states","united"], validResult.stopwords)
XCTAssertEqual("en", validResult.locale)
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}
}

func testStopwordDelete() async {
try! await createStopwordSet()
do {
let (result, _) = try await client.stopword("test-id-stopword-set").delete()
guard let validResult = result else {
throw DataError.dataNotFound
}
print(validResult)
XCTAssertEqual("test-id-stopword-set", validResult._id)
} catch (let error) {
print(error)
XCTAssertTrue(false)
}
}

}
46 changes: 46 additions & 0 deletions Tests/TypesenseTests/StopwordsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import XCTest
@testable import Typesense

final class StopwordsTests: XCTestCase {
override func tearDown() async throws {
try! await tearDownStopwords()
}

func testStopwordsUpsert() async {
let schema = StopwordsSetUpsertSchema(
stopwords: ["states","united"],
locale: "en"
)
do {
let (result, _) = try await client.stopwords().upsert(stopwordsSetId: "test-id", params: schema)
XCTAssertNotNil(result)
guard let validResult = result else {
throw DataError.dataNotFound
}
print(validResult)
XCTAssertEqual("test-id", validResult._id)
XCTAssertEqual(["states","united"], validResult.stopwords)
XCTAssertEqual("en", validResult.locale)
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}
}

func testStopwordsRetrieveAll() async {
try! await createStopwordSet()
do {
let (result, _) = try await client.stopwords().retrieve()
guard let validResult = result else {
throw DataError.dataNotFound
}
print(validResult)
XCTAssertEqual(1, validResult.count)
XCTAssertEqual("test-id-stopword-set", validResult[0]._id)
} catch (let error) {
print(error)
XCTAssertTrue(false)
}
}

}
20 changes: 20 additions & 0 deletions Tests/TypesenseTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func tearDownPresets() async throws {
}
}

func tearDownStopwords() async throws {
let (data, _) = try await client.stopwords().retrieve()
guard let validData = data else {
throw DataError.dataNotFound
}
for item in validData {
let _ = try! await client.stopword(item._id).delete()
}
}

func setUpCollection() async throws{
let schema = CollectionSchema(name: "test-utils-collection", fields: [Field(name: "company_name", type: "string"), Field(name: "num_employees", type: "int32"), Field(name: "country", type: "string", facet: true)], defaultSortingField: "num_employees")
let (collResp, _) = try! await client.collections.create(schema: schema)
Expand Down Expand Up @@ -56,6 +66,16 @@ func createMultiSearchPreset() async throws {
)
}

func createStopwordSet() async throws {
let _ = try! await client.stopwords().upsert(
stopwordsSetId: "test-id-stopword-set",
params: StopwordsSetUpsertSchema(
stopwords: ["states","united"],
locale: "en"
)
)
}

struct Product: Codable, Equatable {
var name: String?
var price: Int?
Expand Down

0 comments on commit 5e62e20

Please sign in to comment.