|
| 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 | +} |
0 commit comments