Skip to content

Commit fbcdb5a

Browse files
committed
add doc for Query properties
1 parent 91c1061 commit fbcdb5a

File tree

1 file changed

+126
-11
lines changed

1 file changed

+126
-11
lines changed

Source/Query.swift

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,143 @@
2323

2424
import Foundation
2525

26-
// TODO: rethink attributes. Use for example enum for queryType
27-
// TODO: add comment for all attributes
28-
2926
/// Describes all parameters of search query.
3027
public class Query : Printable {
28+
/// The type of query.
29+
///
30+
/// - PrefixAll: All query words are interpreted as prefixes.
31+
/// - PrefixLast: Only the last word is interpreted as a prefix (default behavior).
32+
/// - PrefixNone: No query word is interpreted as a prefix. This option is not recommended.
33+
public enum QueryType: String {
34+
case PrefixAll = "prefixAll"
35+
case PrefixLast = "prefixLast"
36+
case PrefixNone = "prefixNone"
37+
}
38+
39+
/// Remove words if no result.
40+
///
41+
/// - None: No specific processing is done when a query does not return any result.
42+
/// - LastWords: When a query does not return any result, the final word will be removed until there is results.
43+
/// - FirstWords: When a query does not return any result, the first word will be removed until there is results.
44+
/// - AllOptional: When a query does not return any result, a second trial will be made with all words as optional (which is equivalent to transforming the AND operand between query terms in a OR operand)
45+
public enum RemoveWordsIfNoResult: String {
46+
case None = "None"
47+
case LastWords = "LastWords"
48+
case FirstWords = "FirstWords"
49+
case AllOptional = "allOptional"
50+
}
51+
52+
/// Typo tolerance.
53+
///
54+
/// - Enabled: The typo-tolerance is enabled and all matching hits are retrieved. (Default behavior)
55+
/// - Disabled: The typo-tolerance is disabled.
56+
/// - Minimum: Only keep the results with the minimum number of typos.
57+
/// - Strict: Hits matching with 2 typos are not retrieved if there are some matching without typos.
58+
public enum TypoTolerance: String {
59+
case Enabled = "true"
60+
case Disabled = "false"
61+
case Minimum = "min"
62+
case Strict = "strict"
63+
}
64+
65+
// MARK: - Properties
66+
67+
/// The minimum number of characters in a query word to accept one typo in this word.
68+
/// Defaults to 3.
3169
public var minWordSizeForApprox1: UInt = 3
70+
71+
/// The minimum number of characters in a query word to accept two typos in this word.
72+
/// Defaults to 7.
3273
public var minWordSizeForApprox2: UInt = 7
74+
75+
/// If true, the result hits will contain ranking information in _rankingInfo attribute.
76+
/// Default to false.
3377
public var getRankingInfo = false
78+
79+
/// If true, plural won't be considered as a typo (for example car/cars will be considered as equals).
80+
/// Default to false.
3481
public var ignorePlural = false
82+
83+
/// If true, enable the distinct feature (disabled by default) if the attributeForDistinct index setting is set.
84+
/// This feature is similar to the SQL "distinct" keyword: when enabled in a query, all hits containing a duplicate value
85+
/// for the attributeForDistinct attribute are removed from results. For example, if the chosen attribute is show_name
86+
/// and several hits have the same value for show_name, then only the best one is kept and others are removed.
3587
public var distinct = false
88+
89+
/// The page to retrieve (zero base). Defaults to 0.
3690
public var page: UInt = 0
91+
92+
/// The number of hits per page. Defaults to 20.
3793
public var hitsPerPage: UInt = 20
94+
95+
/// If false, disable typo-tolerance on numeric tokens. Default to true.
3896
public var typosOnNumericTokens = true
97+
98+
/// If false, this query won't appear in the analytics. Default to true.
3999
public var analytics = true
100+
101+
/// If false, this query will not use synonyms defined in configuration. Default to true.
40102
public var synonyms = true
103+
104+
/// If false, words matched via synonyms expansion will not be replaced by the matched synonym in highlight result.
105+
/// Default to true.
41106
public var replaceSynonyms = true
42-
public var optionalWordsMinimumMatched: UInt = 0
107+
108+
/// The list of attribute names to highlight.
109+
/// By default indexed attributes are highlighted.
43110
public var attributesToHighlight: [String]?
111+
112+
/// The list of attribute names to retrieve.
113+
/// By default all attributes are retrieved.
44114
public var attributesToRetrieve: [String]?
115+
116+
/// Filter the query by a set of tags. You can AND tags by separating them by commas.
117+
/// To OR tags, you must add parentheses. For example tag1,(tag2,tag3) means tag1 AND (tag2 OR tag3).
118+
/// At indexing, tags should be added in the _tags attribute of objects (for example {"_tags":["tag1","tag2"]} ).
45119
public var tagFilters: String?
46-
public var numericFilters: String?
120+
121+
/// A list of numeric filters.
122+
/// The syntax of one filter is `attributeName` followed by `operand` followed by `value. Supported operands are `<`, `<=`, `=`, `>` and `>=`.
123+
/// You can have multiple conditions on one attribute like for example `numerics=price>100,price<1000`.
124+
public var numericFilters: [String]?
125+
126+
/// The full text query.
47127
public var fullTextQuery: String?
48-
public var queryType: String?
49-
public var removeWordsIfNoResult: String?
50-
public var typoTolerance: String?
128+
129+
/// How the query words are interpreted.
130+
public var queryType: QueryType?
131+
132+
/// The strategy to avoid having an empty result page.
133+
public var removeWordsIfNoResult: RemoveWordsIfNoResult?
134+
135+
/// Control the number of typo in the results set.
136+
public var typoTolerance: TypoTolerance?
137+
138+
/// The list of attributes to snippet alongside the number of words to return (syntax is 'attributeName:nbWords').
139+
/// By default no snippet is computed.
51140
public var attributesToSnippet: [String]?
141+
142+
/// Filter the query by a list of facets. Each facet is encoded as `attributeName:value`.
143+
/// For example: ["category:Book","author:John%20Doe"].
52144
public var facetFilters: [String]?
145+
146+
/// Filter the query by a list of facets encoded as one string by example "(category:Book,author:John)".
53147
public var facetFiltersRaw: String?
148+
149+
/// List of object attributes that you want to use for faceting.
150+
/// Only attributes that have been added in attributesForFaceting index setting can be used in this parameter.
151+
/// You can also use `*` to perform faceting on all attributes specified in attributesForFaceting.
54152
public var facets: [String]?
153+
154+
/// The minimum number of optional words that need to match. Defaults to 0.
155+
public var optionalWordsMinimumMatched: UInt = 0
156+
157+
/// The list of words that should be considered as optional when found in the query.
55158
public var optionalWords: [String]?
56-
public var restrictSearchableAttributes: String?
159+
160+
/// List of object attributes you want to use for textual search (must be a subset of the
161+
/// attributesToIndex index setting).
162+
public var restrictSearchableAttributes: [String]?
57163

58164
private var aroundLatLongViaIP = false
59165
private var aroundLatLong: String?
@@ -63,6 +169,8 @@ public class Query : Printable {
63169
get { return "Query = \(buildURL())" }
64170
}
65171

172+
// MARK: - Methods
173+
66174
public init(fullTextQuery: String? = nil) {
67175
self.fullTextQuery = fullTextQuery
68176
}
@@ -133,9 +241,16 @@ public class Query : Printable {
133241
}
134242

135243
if let facetFilters = facetFilters {
136-
// TODO: complete code (JSON)
244+
var error: NSError?
245+
let data = NSJSONSerialization.dataWithJSONObject(facetFilters, options: .PrettyPrinted, error: &error)
246+
if error == nil {
247+
let json: String = NSString(data: data!, encoding: NSUTF8StringEncoding)!
248+
url.append(Query.encodeForQuery(json, withKey: "facetFilters"))
249+
} else {
250+
NSException(name: "InvalidArgument", reason: "Invalid facetFilters (should be an array of string)", userInfo: nil).raise()
251+
}
137252
} else if let facetFiltersRaw = facetFiltersRaw {
138-
url.append(Query.encodeForQuery(facetFiltersRaw, withKey: "facetFilters="))
253+
url.append(Query.encodeForQuery(facetFiltersRaw, withKey: "facetFilters"))
139254
}
140255

141256
if let facets = facets {

0 commit comments

Comments
 (0)