Skip to content

Commit

Permalink
feat: allow multi-search and single collection search to return raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Aug 22, 2024
1 parent 1dd56f9 commit 97e6e00
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Typesense/Documents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public struct Documents {

}

public func search(_ searchParameters: SearchParameters) async throws -> (Data?, URLResponse?) {
let queryParams = try createURLQuery(forSchema: searchParameters)
return try await apiCall.get(endPoint: "\(RESOURCEPATH)/search", queryParameters: queryParams)
}

public func search<T>(_ searchParameters: SearchParameters, for: T.Type) async throws -> (SearchResult<T>?, URLResponse?) {
var searchQueryParams: [URLQueryItem] = []

Expand Down
7 changes: 7 additions & 0 deletions Sources/Typesense/MultiSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public struct MultiSearch {
self.apiCall = apiCall
}

public func perform(searchRequests: [MultiSearchCollectionParameters], commonParameters: MultiSearchParameters) async throws -> (Data?, URLResponse?) {
let queryParams = try createURLQuery(forSchema: commonParameters)
let searchesData = try encoder.encode(MultiSearchSearchesParameter(searches: searchRequests))

return try await apiCall.post(endPoint: "\(RESOURCEPATH)", body: searchesData, queryParameters: queryParams)
}

public func perform<T>(searchRequests: [MultiSearchCollectionParameters], commonParameters: MultiSearchParameters, for: T.Type) async throws -> (MultiSearchResult<T>?, URLResponse?) {
var searchQueryParams: [URLQueryItem] = []

Expand Down
22 changes: 22 additions & 0 deletions Tests/TypesenseTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ final class DocumentTests: XCTestCase {

}

func testDocumentSearchReturnRawData() async {
let searchParams = SearchParameters(q: "stark", queryBy: "company_name", filterBy: "num_employees:>100", sortBy: "num_employees:desc")

do {
try await createDocument()
let (data, _) = try await client.collection(name: "companies").documents().search(searchParams)
guard let validData = data else {
throw DataError.dataNotFound
}
if let json = try JSONSerialization.jsonObject(with: validData, options: []) as? [String: Any]{
XCTAssertNotNil(json["hits"])
XCTAssertNotNil(json["found"])
} else{
XCTAssertTrue(false)
}
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}

}

func testDocumentSearchWithPreset() async {
let productSchema = CollectionSchema(name: "products", fields: [
Field(name: "name", type: "string"),
Expand Down
64 changes: 64 additions & 0 deletions Tests/TypesenseTests/MultiSearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,70 @@ final class MultiSearchTests: XCTestCase {

}

func testMultiSearchReturnRawData() async {
let productSchema = CollectionSchema(name: "products", fields: [
Field(name: "name", type: "string"),
Field(name: "price", type: "int32"),
Field(name: "brand", type: "string"),
Field(name: "desc", type: "string"),
])

let brandSchema = CollectionSchema(name: "brands", fields: [
Field(name: "name", type: "string"),
])

let searchRequests = [
MultiSearchCollectionParameters(q: "shoe", filterBy: "price:=[50..120]", collection: "products"),
MultiSearchCollectionParameters(q: "Nike", collection: "brands"),
]

let brand1 = Brand(name: "Nike")
let product1 = Product(name: "Jordan", price: 70, brand: "Nike", desc: "High quality shoe")

let commonParams = MultiSearchParameters(queryBy: "name")

do {
do {
let _ = try await client.collections.create(schema: productSchema)
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}

do {
let _ = try await client.collections.create(schema: brandSchema)
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}

let (_,_) = try await client.collection(name: "products").documents().create(document: encoder.encode(product1))

let (_,_) = try await client.collection(name: "brands").documents().create(document: encoder.encode(brand1))

let (data, _) = try await client.multiSearch().perform(searchRequests: searchRequests, commonParameters: commonParams)

guard let validData = data else {
throw DataError.dataNotFound
}
if let json = try JSONSerialization.jsonObject(with: validData, options: []) as? [String: Array<[String: Any]>]{
print(json)
XCTAssertNotNil(json["results"]?[0])
XCTAssertNotNil(json["results"]?[1])
} else{
XCTAssertTrue(false)
}
} catch HTTPError.serverError(let code, let desc) {
print(desc)
print("The response status code is \(code)")
XCTAssertTrue(false)
} catch (let error) {
print(error.localizedDescription)
XCTAssertTrue(false)
}

}

func testMultiSearchWithPreset() async {
let productSchema = CollectionSchema(name: "products", fields: [
Field(name: "name", type: "string"),
Expand Down

0 comments on commit 97e6e00

Please sign in to comment.