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

Throw better errors, allow search grouping of multiple fields with different data type, bug fixes 🐛 , ... #35

Merged
merged 19 commits into from
Aug 26, 2024

Conversation

phiHero
Copy link
Contributor

@phiHero phiHero commented Aug 20, 2024

Change Summary

Fixes:

  • Change ApiCall struct to a class and instead of passing the node configuration down to each method and each initializes a new ApiCall, pass the apiCall by reference to keep the node health state in sync.
  • Fix various tests that weren't written properly.
  • Multiple new instances of ApiCall still use the same currentNodeIndex.
  • Analytic methods weren't made public so the user can't access it.
  • Make the ApiKey _id field required.
  • Errors were not thrown on http status 5xx
  • Search grouping of multiple fields with different data type cause swift decode error

Features:

  • added update and update by filter for /documents endpoint
  • allow configuration of dirty data behaviors & import actions
  • allow multi-search & single collection search to return raw data Allow search and multi-search to return raw data #31
  • add analytics events endpoint

Note

Breaking changes:

  • Allow search grouping of multiple fields with different data type. Currently we can only group string fields (because Swift array can only hold one type). This PR add support for mixing string fields with numeric, boolean fields and fields with missing values. Requires a lightweight dependency AnyCodable.
// type of num_employees is Int, country is String and metadata is Boolean
// before, if we try grouping these fields together, Swift will throw a decoding error 
let searchParams = SearchParameters(q: "*", queryBy: "company_name", groupBy: "num_employees,country,metadata")
let (data, _) =  try await client.collection(name: "companies").documents().search(searchParams, for: Company.self)

if let groupKey = data?.groupedHits?.first?.groupKey{
    if let num_employees = groupKey[0].value as? Int{
        print(num_employees)
    }
   // do the same for country and metadata
}
  • Throw better http client errors with status code and error message inside ApiCall. This will reduce a lot of boilerplate code as we don't have to manually define and throw error for each endpoint (in which the error is also not correct since it did not check for the status code and throw error accordingly).
do {
    let (data, _) = try await client.keys().retrieve(id: 1)
    // all endpoints will now throw HTTPError client & server error
} catch HTTPError.clientError(let code, let desc){
    print(code, desc)
} catch HTTPError.serverError(let code, let desc){
    print(code, desc)
    // instead of ResponseError
    // catch ResponseError.apiKeyNotFound(let desc) {
    //            print(desc)
    // }
} catch (let error) {
    print(error)
}
  • Update analytics rules schemas
// the `type` field is now an enum
AnalyticsRuleSchema(
    name: "product_queries_aggregation",
    type: .popularQueries,
    // type: "popular_queries",
)

Deprecation

// `/documents` endpoint
// deprecated 
func delete(filter: String, batchSize: Int? = nil) async throws -> (Data?, URLResponse?)
func importBatch(_ documents: Data, action: ActionModes? = ActionModes.create) async throws -> (Data?, URLResponse?)
// in favor of
func delete(options: DeleteDocumentsParameters) async throws -> (DeleteDocumentsResponse?, URLResponse?)
func importBatch(_ documents: Data, options: ImportDocumentsParameters) async throws -> (Data?, URLResponse?)

PR Checklist

@phiHero phiHero changed the title Throw better errors, allow search grouping of multiple fields with different data type and bug fixes 🐛 Throw better errors, allow search grouping of multiple fields with different data type, bug fixes 🐛 , ... Aug 22, 2024
@jasonbosco jasonbosco merged commit 126815b into typesense:master Aug 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants