11import Foundation
22
33/**
4- `SearchParameters` instances represent query setup for a search request.
4+ `SearchParameters` instances represent query setup for a search request.
55 Use `SearchParameters.query` to directly create a search query with the
66 default search configuration.
77 */
@@ -10,25 +10,25 @@ public struct SearchParameters: Codable, Equatable {
1010 // MARK: Properties
1111
1212 /// Query string (mandatory).
13- public let query : String
13+ public let query : String ?
1414
1515 /// Number of documents to take.
16- public let limit : Int
16+ public let limit : Int ?
1717
1818 /// Number of documents to skip.
19- public let offset : Int
19+ public let offset : Int ?
2020
2121 /// Document attributes to show.
2222 public let attributesToRetrieve : [ String ] ?
2323
2424 /// Which attributes to crop.
25- public let attributesToCrop : [ String ]
25+ public let attributesToCrop : [ String ] ?
2626
2727 /// Limit length at which to crop specified attributes.
28- public let cropLength : Int
28+ public let cropLength : Int ?
2929
3030 /// Which attributes to highlight.
31- public let attributesToHighlight : [ String ]
31+ public let attributesToHighlight : [ String ] ?
3232
3333 /// Attribute with an exact match.
3434 public let filters : String ?
@@ -40,22 +40,22 @@ public struct SearchParameters: Codable, Equatable {
4040 public let facetsDistribution : [ String ] ?
4141
4242 /// Whether to return the raw matches or not.
43- public let matches : Bool
43+ public let matches : Bool ?
4444
4545 // MARK: Initializers
4646
4747 init (
48- query: String ,
49- offset: Int = Default . offset . rawValue ,
50- limit: Int = Default . limit . rawValue ,
48+ query: String ? ,
49+ offset: Int ? = nil ,
50+ limit: Int ? = nil ,
5151 attributesToRetrieve: [ String ] ? = nil ,
52- attributesToCrop: [ String ] = [ ] ,
53- cropLength: Int = Default . cropLength . rawValue ,
54- attributesToHighlight: [ String ] = [ ] ,
52+ attributesToCrop: [ String ] ? = nil ,
53+ cropLength: Int ? = nil ,
54+ attributesToHighlight: [ String ] ? = nil ,
5555 filters: String ? = nil ,
5656 facetFilters: [ [ String ] ] ? = nil ,
5757 facetsDistribution: [ String ] ? = nil ,
58- matches: Bool = false ) {
58+ matches: Bool ? = false ) {
5959 self . query = query
6060 self . offset = offset
6161 self . limit = limit
@@ -97,54 +97,4 @@ public struct SearchParameters: Codable, Equatable {
9797 case matches
9898 }
9999
100- // MARK: Default value for keys
101-
102- fileprivate enum Default : Int {
103- case offset = 0
104- case limit = 20
105- case cropLength = 200
106- }
107-
108- }
109-
110- extension SearchParameters {
111-
112- // MARK: Codable
113-
114- /// Encodes the `SearchParameters` to a JSON payload, removing any non necessary implicit parameter.
115- public func encode( to encoder: Encoder ) throws {
116- var container = encoder. container ( keyedBy: CodingKeys . self)
117- try container. encode ( query, forKey: . query)
118- if limit != Default . limit. rawValue {
119- try container. encode ( limit, forKey: . limit)
120- }
121- if offset != Default . offset. rawValue {
122- try container. encode ( offset, forKey: . offset)
123- }
124- if let attributesToRetrieve: [ String ] = self . attributesToRetrieve, !attributesToRetrieve. isEmpty {
125- try container. encode ( attributesToRetrieve, forKey: . attributesToRetrieve)
126- }
127- if !attributesToCrop. isEmpty {
128- try container. encode ( attributesToCrop, forKey: . attributesToCrop)
129- }
130- if cropLength != Default . cropLength. rawValue {
131- try container. encode ( cropLength, forKey: . cropLength)
132- }
133- if !attributesToHighlight. isEmpty {
134- try container. encode ( attributesToHighlight, forKey: . attributesToHighlight)
135- }
136- if let filters: String = self . filters, !filters. isEmpty {
137- try container. encode ( filters, forKey: . filters)
138- }
139- if let facetFilters: [ [ String ] ] = self . facetFilters {
140- try container. encode ( facetFilters, forKey: . facetFilters)
141- }
142- if let facetsDistribution = self . facetsDistribution, !facetsDistribution. isEmpty {
143- try container. encode ( facetsDistribution, forKey: . facetsDistribution)
144- }
145- if matches {
146- try container. encode ( matches, forKey: . matches)
147- }
148- }
149-
150100}
0 commit comments