Skip to content

Commit 3ff9144

Browse files
committed
Add snuppets for Managing Results section. Update Indexing and Personalization snippets
1 parent 468faed commit 3ff9144

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//
2+
// DetectingIntentSnippets.swift
3+
//
4+
//
5+
// Created by Vladislav Fitc on 25/08/2020.
6+
//
7+
8+
import Foundation
9+
import AlgoliaSearchClient
10+
11+
struct DetectingIntentSnippets: SnippetsCollection {
12+
13+
14+
}
15+
16+
//MARK: - Applying a custom filter for a specific query
17+
18+
extension DetectingIntentSnippets {
19+
20+
func api_faceting_negative_example() {
21+
let settings = Settings()
22+
.set(\.attributesForFaceting, to: ["allergens"])
23+
24+
index.setSettings(settings) { result in
25+
if case .success(let response) = result {
26+
print("Response: \(response)")
27+
}
28+
}
29+
}
30+
31+
func api_save_rule_negative_example() {
32+
let rule = Rule(objectID: "gluten-free-rule")
33+
.set(\.conditions, to: [
34+
Rule.Condition()
35+
.set(\.anchoring, to: .contains)
36+
.set(\.pattern, to: .literal("gluten-free"))
37+
])
38+
.set(\.consequence, to: Rule.Consequence()
39+
.set(\.queryTextAlteration, to: .edits([.remove("gluten-free")]))
40+
.set(\.query, to: Query().set(\.filters, to: "NOT allergens:gluten"))
41+
)
42+
43+
index.saveRule(rule) { result in
44+
if case .success(let response) = result {
45+
print("Response: \(response)")
46+
}
47+
}
48+
}
49+
50+
func api_save_rule_positive_example() {
51+
let rule = Rule(objectID: "diet-rule")
52+
.set(\.conditions, to: [
53+
Rule.Condition()
54+
.set(\.anchoring, to: .contains)
55+
.set(\.pattern, to: .literal("diet"))
56+
])
57+
.set(\.consequence, to: Rule.Consequence()
58+
.set(\.queryTextAlteration, to: .edits([.remove("diet")]))
59+
.set(\.query, to: Query().set(\.filters, to: "\"low-carb\" OR \"low-fat\""))
60+
)
61+
62+
index.saveRule(rule) { result in
63+
if case .success(let response) = result {
64+
print("Response: \(response)")
65+
}
66+
}
67+
}
68+
69+
func api_save_rule_optional_example() {
70+
let rule = Rule(objectID: "asap-rule")
71+
.set(\.conditions, to: [
72+
Rule.Condition()
73+
.set(\.anchoring, to: .contains)
74+
.set(\.pattern, to: .literal("asap"))
75+
])
76+
.set(\.consequence, to: Rule.Consequence()
77+
.set(\.queryTextAlteration, to: .edits([.remove("asap")]))
78+
.set(\.query, to: Query().set(\.optionalFilters, to: ["can_deliver_quickly:true"]))
79+
)
80+
81+
index.saveRule(rule) { result in
82+
if case .success(let response) = result {
83+
print("Response: \(response)")
84+
}
85+
}
86+
}
87+
88+
}
89+
90+
//MARK: - Adding search parameters dynamically
91+
92+
extension DetectingIntentSnippets {
93+
94+
func api_settings_example() {
95+
let settings = Settings()
96+
.set(\.customRanking, to: [.desc("nb_airline_liaisons")])
97+
.set(\.attributesForFaceting, to: ["city", "country"])
98+
99+
index.setSettings(settings) { result in
100+
if case .success(let response) = result {
101+
print("Response: \(response)")
102+
}
103+
}
104+
}
105+
106+
func api_rules_example() {
107+
let rules: [Rule] = [
108+
Rule(objectID: "country")
109+
.set(\.conditions, to: [
110+
Rule.Condition()
111+
.set(\.anchoring, to: .contains)
112+
.set(\.pattern, to: .facet("country"))
113+
])
114+
.set(\.consequence, to: Rule.Consequence()
115+
.set(\.query, to: Query()
116+
.set(\.aroundLatLngViaIP, to: false)
117+
)
118+
),
119+
Rule(objectID: "country")
120+
.set(\.conditions, to: [
121+
Rule.Condition()
122+
.set(\.anchoring, to: .contains)
123+
.set(\.pattern, to: .facet("city"))
124+
])
125+
.set(\.consequence, to: Rule.Consequence()
126+
.set(\.query, to: Query()
127+
.set(\.aroundLatLngViaIP, to: false)
128+
)
129+
)
130+
]
131+
132+
index.saveRules(rules) { result in
133+
if case .success(let response) = result {
134+
print("Response: \(response)")
135+
}
136+
}
137+
}
138+
139+
func api_query_example() {
140+
let query = Query()
141+
.set(\.query, to: "query")
142+
.set(\.aroundLatLngViaIP, to: true)
143+
144+
index.search(query: query) { result in
145+
if case .success(let response) = result {
146+
print("Response: \(response)")
147+
}
148+
}
149+
}
150+
151+
}
152+
153+
//MARK: - Detecting keywords
154+
155+
extension DetectingIntentSnippets {
156+
157+
func detectingKeywords() {
158+
// Create the rule
159+
let rule = Rule(objectID: "article-id")
160+
.set(\.conditions, to: [
161+
Rule.Condition()
162+
.set(\.anchoring, to: .startsWith)
163+
.set(\.pattern, to: .literal("article"))
164+
])
165+
.set(\.consequence, to: Rule.Consequence()
166+
.set(\.query, to: Query()
167+
.set(\.restrictSearchableAttributes, to: [
168+
"title",
169+
"book_id"
170+
])
171+
)
172+
)
173+
// Save the rule
174+
index.saveRule(rule, forwardToReplicas: true) { result in
175+
if case .success(let response) = result {
176+
print("Response: \(response)")
177+
}
178+
}
179+
}
180+
181+
}

Tests/AlgoliaSearchClientTests/Doc/Methods/IndexingSnippets.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,38 @@ extension IndexingSnippets {
267267
}
268268
}
269269

270+
func increment_existing_partial() {
271+
index.partialUpdateObject(withID: "myID", with: .increment(attribute: "count", value: 2)) { result in
272+
if case .success(let response) = result {
273+
print("Response: \(response)")
274+
}
275+
}
276+
}
277+
278+
func addunique_existing_partial() {
279+
index.partialUpdateObject(withID: "myID", with: .add(attribute: "_tags", value: "public", unique: true)) { result in
280+
if case .success(let response) = result {
281+
print("Response: \(response)")
282+
}
283+
}
284+
}
285+
286+
func increment_from() {
287+
index.partialUpdateObject(withID: "myID", with: .incrementFrom(attribute: "version", value: 2)) { result in
288+
if case .success(let response) = result {
289+
print("Response: \(response)")
290+
}
291+
}
292+
}
293+
294+
func increment_set() {
295+
index.partialUpdateObject(withID: "myID", with: .incrementSet(attribute: "lastmodified", value: 1593431913)) { result in
296+
if case .success(let response) = result {
297+
print("Response: \(response)")
298+
}
299+
}
300+
}
301+
270302
}
271303

272304
//MARK: - Delete Objects Update

Tests/AlgoliaSearchClientTests/Doc/Methods/PersonalizationSnippets.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ struct PersonalizationSnippets: SnippetsCollection {
1313
let recommendationClient = RecommendationClient(appID: "", apiKey: "")
1414
}
1515

16+
//MARK: - Set region
17+
extension PersonalizationSnippets {
18+
19+
func personalization_region() {
20+
let recommendationClient = RecommendationClient(
21+
appID: "YourApplicationID",
22+
apiKey: "YourAdminAPIKey",
23+
region: .init(rawValue: "eu") // defaults to 'us'
24+
)
25+
26+
recommendationClient.getPersonalizationStrategy { result in
27+
if case .success(let response) = result {
28+
print("Response: \(response)")
29+
}
30+
}
31+
}
32+
33+
}
34+
1635
//MARK: - Add strategy
1736
extension PersonalizationSnippets {
1837

0 commit comments

Comments
 (0)