|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public struct MultiSearch { |
| 4 | + var apiCall: ApiCall |
| 5 | + let RESOURCEPATH = "multi_search" |
| 6 | + |
| 7 | + public init(config: Configuration) { |
| 8 | + apiCall = ApiCall(config: config) |
| 9 | + } |
| 10 | + |
| 11 | + public func perform<T>(searchRequests: [MultiSearchCollectionParameters], commonParameters: MultiSearchParameters, for: T.Type) async throws -> (MultiSearchResult<T>?, URLResponse?) { |
| 12 | + var searchQueryParams: [URLQueryItem] = [] |
| 13 | + |
| 14 | + if let query = commonParameters.q { |
| 15 | + searchQueryParams.append(URLQueryItem(name: "q", value: query)) |
| 16 | + } |
| 17 | + |
| 18 | + if let queryBy = commonParameters.queryBy { |
| 19 | + searchQueryParams.append(URLQueryItem(name: "query_by", value: queryBy)) |
| 20 | + } |
| 21 | + |
| 22 | + if let queryByWeights = commonParameters.queryByWeights { |
| 23 | + searchQueryParams.append(URLQueryItem(name: "query_by_weights", value: queryByWeights)) |
| 24 | + } |
| 25 | + |
| 26 | + if let maxHits = commonParameters.maxHits { |
| 27 | + searchQueryParams.append(URLQueryItem(name: "max_hits", value: maxHits)) |
| 28 | + } |
| 29 | + |
| 30 | + if let _prefix = commonParameters._prefix { |
| 31 | + var fullString = "" |
| 32 | + for item in _prefix { |
| 33 | + fullString.append(String(item)) |
| 34 | + fullString.append(",") |
| 35 | + } |
| 36 | + |
| 37 | + searchQueryParams.append(URLQueryItem(name: "prefix", value: String(fullString.dropLast()))) |
| 38 | + } |
| 39 | + |
| 40 | + if let filterBy = commonParameters.filterBy { |
| 41 | + searchQueryParams.append(URLQueryItem(name: "filter_by", value: filterBy)) |
| 42 | + } |
| 43 | + |
| 44 | + if let sortBy = commonParameters.sortBy { |
| 45 | + searchQueryParams.append(URLQueryItem(name: "sort_by", value: sortBy)) |
| 46 | + } |
| 47 | + |
| 48 | + if let facetBy = commonParameters.facetBy { |
| 49 | + searchQueryParams.append(URLQueryItem(name: "facet_by", value: facetBy)) |
| 50 | + } |
| 51 | + |
| 52 | + if let maxFacetValues = commonParameters.maxFacetValues { |
| 53 | + searchQueryParams.append(URLQueryItem(name: "max_facet_values", value: String(maxFacetValues))) |
| 54 | + } |
| 55 | + |
| 56 | + if let facetQuery = commonParameters.facetQuery { |
| 57 | + searchQueryParams.append(URLQueryItem(name: "facet_query", value: facetQuery)) |
| 58 | + } |
| 59 | + |
| 60 | + if let numTypos = commonParameters.numTypos { |
| 61 | + searchQueryParams.append(URLQueryItem(name: "num_typos", value: String(numTypos))) |
| 62 | + } |
| 63 | + |
| 64 | + if let page = commonParameters.page { |
| 65 | + searchQueryParams.append(URLQueryItem(name: "page", value: String(page))) |
| 66 | + } |
| 67 | + |
| 68 | + if let perPage = commonParameters.perPage { |
| 69 | + searchQueryParams.append(URLQueryItem(name: "per_page", value: String(perPage))) |
| 70 | + } |
| 71 | + |
| 72 | + if let groupBy = commonParameters.groupBy { |
| 73 | + searchQueryParams.append(URLQueryItem(name: "group_by", value: groupBy)) |
| 74 | + } |
| 75 | + |
| 76 | + if let groupLimit = commonParameters.groupLimit { |
| 77 | + searchQueryParams.append(URLQueryItem(name: "group_limit", value: String(groupLimit))) |
| 78 | + } |
| 79 | + |
| 80 | + if let includeFields = commonParameters.includeFields { |
| 81 | + searchQueryParams.append(URLQueryItem(name: "include_fields", value: includeFields)) |
| 82 | + } |
| 83 | + |
| 84 | + if let excludeFields = commonParameters.excludeFields { |
| 85 | + searchQueryParams.append(URLQueryItem(name: "exclude_fields", value: excludeFields)) |
| 86 | + } |
| 87 | + |
| 88 | + if let highlightFullFields = commonParameters.highlightFullFields { |
| 89 | + searchQueryParams.append(URLQueryItem(name: "highlight_full_fields", value: highlightFullFields)) |
| 90 | + } |
| 91 | + |
| 92 | + if let highlightAffixNumTokens = commonParameters.highlightAffixNumTokens { |
| 93 | + searchQueryParams.append(URLQueryItem(name: "highlight_affix_num_tokens", value: String(highlightAffixNumTokens))) |
| 94 | + } |
| 95 | + |
| 96 | + if let highlightStartTag = commonParameters.highlightStartTag { |
| 97 | + searchQueryParams.append(URLQueryItem(name: "highlight_start_tag", value: highlightStartTag)) |
| 98 | + } |
| 99 | + |
| 100 | + if let highlightEndTag = commonParameters.highlightEndTag { |
| 101 | + searchQueryParams.append(URLQueryItem(name: "highlight_end_tag", value: highlightEndTag)) |
| 102 | + } |
| 103 | + |
| 104 | + if let snippetThreshold = commonParameters.snippetThreshold { |
| 105 | + searchQueryParams.append(URLQueryItem(name: "snippet_threshold", value: String(snippetThreshold))) |
| 106 | + } |
| 107 | + |
| 108 | + if let dropTokensThreshold = commonParameters.dropTokensThreshold { |
| 109 | + searchQueryParams.append(URLQueryItem(name: "drop_tokens_threshold", value: String(dropTokensThreshold))) |
| 110 | + } |
| 111 | + |
| 112 | + if let typoTokensThreshold = commonParameters.typoTokensThreshold { |
| 113 | + searchQueryParams.append(URLQueryItem(name: "typo_tokens_threshold", value: String(typoTokensThreshold))) |
| 114 | + } |
| 115 | + |
| 116 | + if let pinnedHits = commonParameters.pinnedHits { |
| 117 | + searchQueryParams.append(URLQueryItem(name: "pinned_hits", value: pinnedHits)) |
| 118 | + } |
| 119 | + |
| 120 | + if let hiddenHits = commonParameters.hiddenHits { |
| 121 | + searchQueryParams.append(URLQueryItem(name: "hidden_hits", value: hiddenHits)) |
| 122 | + } |
| 123 | + |
| 124 | + if let highlightFields = commonParameters.highlightFields { |
| 125 | + searchQueryParams.append(URLQueryItem(name: "highlight_fields", value: highlightFields)) |
| 126 | + } |
| 127 | + |
| 128 | + if let preSegmentedQuery = commonParameters.preSegmentedQuery { |
| 129 | + searchQueryParams.append(URLQueryItem(name: "pre_segmented_query", value: String(preSegmentedQuery))) |
| 130 | + } |
| 131 | + |
| 132 | + if let enableOverrides = commonParameters.enableOverrides { |
| 133 | + searchQueryParams.append(URLQueryItem(name: "enable_overrides", value: String(enableOverrides))) |
| 134 | + } |
| 135 | + |
| 136 | + if let prioritizeExactMatch = commonParameters.prioritizeExactMatch { |
| 137 | + searchQueryParams.append(URLQueryItem(name: "prioritize_exact_match", value: String(prioritizeExactMatch))) |
| 138 | + } |
| 139 | + |
| 140 | + if let exhaustiveSearch = commonParameters.exhaustiveSearch { |
| 141 | + searchQueryParams.append(URLQueryItem(name: "exhaustive_search", value: String(exhaustiveSearch))) |
| 142 | + } |
| 143 | + |
| 144 | + if let searchCutoffMs = commonParameters.searchCutoffMs { |
| 145 | + searchQueryParams.append(URLQueryItem(name: "search_cutoff_ms", value: String(searchCutoffMs))) |
| 146 | + } |
| 147 | + |
| 148 | + if let useCache = commonParameters.useCache { |
| 149 | + searchQueryParams.append(URLQueryItem(name: "use_cache", value: String(useCache))) |
| 150 | + } |
| 151 | + |
| 152 | + if let cacheTtl = commonParameters.cacheTtl { |
| 153 | + searchQueryParams.append(URLQueryItem(name: "cache_ttl", value: String(cacheTtl))) |
| 154 | + } |
| 155 | + |
| 156 | + if let minLen1typo = commonParameters.minLen1typo { |
| 157 | + searchQueryParams.append(URLQueryItem(name: "min_len1type", value: String(minLen1typo))) |
| 158 | + } |
| 159 | + |
| 160 | + if let minLen2typo = commonParameters.minLen2typo { |
| 161 | + searchQueryParams.append(URLQueryItem(name: "min_len2type", value: String(minLen2typo))) |
| 162 | + } |
| 163 | + |
| 164 | + let searches = MultiSearchSearchesParameter(searches: searchRequests) |
| 165 | + |
| 166 | + let searchesData = try encoder.encode(searches) |
| 167 | + |
| 168 | + let (data, response) = try await apiCall.post(endPoint: "\(RESOURCEPATH)", body: searchesData, queryParameters: searchQueryParams) |
| 169 | + |
| 170 | + if let validData = data { |
| 171 | + let searchRes = try decoder.decode(MultiSearchResult<T>.self, from: validData) |
| 172 | + return (searchRes, response) |
| 173 | + } |
| 174 | + |
| 175 | + return (nil, response) |
| 176 | + } |
| 177 | +} |
0 commit comments