Skip to content

Commit

Permalink
adds sortBy support
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jul 20, 2024
1 parent a8dd6a2 commit 2f2040f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Performing full-text, vector, or hybrid search:
```swift
import OramaClient

struct MyDoc: Encodable & Decodable {
struct MyDoc: Codable {
let title: String
let description: String
}
Expand All @@ -69,7 +69,7 @@ Performing an answer session:
```swift
import OramaClient

struct MyDoc: Encodable & Decodable {
struct MyDoc: Codable {
let title: String
let description: String
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/oramacloud-client/answer-session.swift
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import Foundation

@available(macOS 13.0, *)
struct AnswerParams<Doc: Encodable & Decodable> {
struct AnswerParams<Doc: Codable> {
let initialMessages: [Message]
let inferenceType: InferenceType
let oramaClient: OramaClient
let userContext: UserSpecs?
let events: Events?

enum InferenceType: Encodable, Decodable {
enum InferenceType: Codable {
case documentation
}

enum RelatedFormat: Encodable, Decodable {
enum RelatedFormat: Codable {
case question
case query
}

enum Role: Encodable, Decodable {
enum Role: Codable {
case user
case assistant
}

struct Message: Encodable, Decodable {
struct Message: Codable {
let role: Role
var content: String
}

enum UserSpecs: Encodable, Decodable {
enum UserSpecs: Codable {
case string
case JSObject
}

struct Related: Encodable, Decodable {
struct Related: Codable {
var howMany: Int? = 3
var format: RelatedFormat? = .question
}

struct AskParams: Encodable, Decodable {
struct AskParams: Codable {
let query: String
let userData: UserSpecs?
let related: Related?
Expand Down Expand Up @@ -80,7 +80,7 @@ struct AnswerParams<Doc: Encodable & Decodable> {
}

@available(macOS 13.0, *)
class AnswerSession<Doc: Encodable & Decodable> {
class AnswerSession<Doc: Codable> {
struct SSEMessage: Codable {
let type: String
let message: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
struct ClientSearchParams: Encodable, Decodable {
struct ClientSearchParams: Codable {
enum Order: String, Codable {
case asc
case desc
}

struct FacetLimitOrder: Codable {
let limit: Int?
let order: Order?
let offset: Int?
}

enum FacetsString: Codable {
case JSObject(FacetLimitOrder)
}

struct FacetsNumberRange: Codable {
let from: Int
let to: Int
}

struct FacetsNumber: Codable {
let ranges: [FacetsNumberRange]
}

struct FacetsBoolean: Codable {
let isTrue: Bool?
let isFalse: Bool?
}

struct Facets: Codable {
let string: JSObject<FacetsString>?
let number: JSObject<FacetsNumber>?
let boolean: JSObject<FacetsBoolean>?
}

struct SortByDirective: Codable {
let property: String
var order: Order = .asc
}

let term: String
let mode: SearchMode
var mode: SearchMode = .fulltext
let limit: Int?
let offset: Int?
let returning: [String]?
let facets: Facets?
let sortBy: [SortByDirective]?

private init(term: String, mode: SearchMode, limit: Int?, offset: Int?, returning: [String]?, facets: Facets?) {
private init(term: String, mode: SearchMode, limit: Int?, offset: Int?, returning: [String]?, facets: Facets?, sortBy: [SortByDirective]?) {
self.term = term
self.mode = mode
self.limit = limit
self.offset = offset
self.returning = returning
self.facets = facets
self.sortBy = sortBy
}

static func builder(term: String, mode: SearchMode) -> Builder {
Expand All @@ -26,6 +68,7 @@ struct ClientSearchParams: Encodable, Decodable {
private var offset: Int
private var returning: [String]?
private var facets: Facets?
private var sortBy: [SortByDirective]?

fileprivate init(term: String, mode: SearchMode) {
self.term = term
Expand Down Expand Up @@ -55,14 +98,20 @@ struct ClientSearchParams: Encodable, Decodable {
return self
}

func sortBy(_ sortBy: [SortByDirective]) -> Self {
self.sortBy = sortBy
return self
}

func build() -> ClientSearchParams {
return ClientSearchParams(
term: term,
mode: mode,
limit: limit,
offset: offset,
returning: returning,
facets: facets
facets: facets,
sortBy: sortBy
)
}
}
Expand Down
47 changes: 5 additions & 42 deletions Sources/oramacloud-client/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ extension Dictionary {

// ======================== CLIENT TYPES ========================

struct OramaClientParams: Encodable, Decodable {
struct OramaClientParams: Codable {
let endpoint: String
let apiKey: String
}

enum SearchMode: String, Encodable, Decodable {
enum SearchMode: String, Codable {
case fulltext
case vector
case hybrid
}

// ======================== SEARCH TYPES ========================

struct Elapsed: Encodable, Decodable {
struct Elapsed: Codable {
let raw: Int
let formatted: String
}

struct Hit<T>: Encodable, Decodable where T: Encodable & Decodable {
struct Hit<T>: Codable where T: Codable {
let id: String
let score: Float
let document: T
}

struct SearchResults<T>: Encodable, Decodable where T: Encodable & Decodable {
struct SearchResults<T>: Codable where T: Codable {
let count: Int
let hits: [Hit<T>]
var elapsed: Elapsed
Expand All @@ -77,40 +77,3 @@ struct SearchRequestPayload: Encodable {
return jsonObject
}
}

// ======================== FACETS TYPES ========================

enum Order: String, Decodable, Encodable {
case asc
case desc
}

struct FacetLimitOrder: Encodable, Decodable {
let limit: Int?
let order: Order?
let offset: Int?
}

enum FacetsString: Encodable, Decodable {
case JSObject(FacetLimitOrder)
}

struct FacetsNumberRange: Encodable, Decodable {
let from: Int
let to: Int
}

struct FacetsNumber: Encodable, Decodable {
let ranges: [FacetsNumberRange]
}

struct FacetsBoolean: Encodable, Decodable {
let isTrue: Bool?
let isFalse: Bool?
}

struct Facets: Encodable, Decodable {
let string: JSObject<FacetsString>?
let number: JSObject<FacetsNumber>?
let boolean: JSObject<FacetsBoolean>?
}
2 changes: 1 addition & 1 deletion Tests/oramacloud-clientTests/oramacloud_clientTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import oramacloud_client
import XCTest

struct E2ETest1Document: Encodable & Decodable {
struct E2ETest1Document: Codable {
let breed: String
}

Expand Down

0 comments on commit 2f2040f

Please sign in to comment.