Skip to content

Commit 670e17a

Browse files
author
Clément Le Provost
committed
Merge branch 'master' of github.com:algolia/algoliasearch-client-swift
2 parents ab74291 + cc6d6ad commit 670e17a

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Source/Index.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,30 @@ import Foundation
363363
}
364364
}
365365

366+
/// Search for facet values.
367+
/// This searches inside a facet's values, optionally restricting the returned values to those contained in objects
368+
/// matching other (regular) search criteria.
369+
///
370+
/// - parameter facetName: Name of the facet to search. It must have been declared in the index's
371+
/// `attributesForFaceting` setting with the `searchable()` modifier.
372+
/// - parameter text: Text to search for in the facet's values.
373+
/// - parameter query: An optional query to take extra search parameters into account. These parameters apply to
374+
/// index objects like in a regular search query. Only facet values contained in the matched objects will be
375+
/// returned.
376+
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.
377+
/// - returns: A cancellable operation.
378+
///
379+
@objc(searchForFacetValuesOf:matching:query:completionHandler:)
380+
@discardableResult public func searchForFacetValues(of facetName: String, matching text: String, query: Query? = nil, completionHandler: @escaping CompletionHandler) -> Operation {
381+
let path = "1/indexes/\(urlEncodedName)/facets/\(facetName.urlEncodedPathComponent())/query"
382+
let params = query != nil ? Query(copy: query!) : Query()
383+
params["facetQuery"] = text
384+
let requestBody = [
385+
"params": params.build()
386+
]
387+
return client.performHTTPQuery(path: path, method: .POST, body: requestBody, hostnames: client.readHosts, isSearchQuery: true, completionHandler: completionHandler)
388+
}
389+
366390
/// Get this index's settings.
367391
///
368392
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.

Tests/IndexTests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,4 +1158,82 @@ class IndexTests: OnlineTestCase {
11581158
}
11591159
self.waitForExpectations(timeout: expectationTimeout + timeout * 4, handler: nil)
11601160
}
1161+
1162+
func testSearchForFacetValues() {
1163+
let expectation = self.expectation(description: #function)
1164+
let settings = [
1165+
"attributesForFaceting": [
1166+
"searchable(series)",
1167+
"kind"
1168+
]
1169+
]
1170+
let objects: [String: JSONObject] = [
1171+
"snoopy": [
1172+
"objectID": "1",
1173+
"name": "Snoopy",
1174+
"kind": [ "dog", "animal" ],
1175+
"born": 1950,
1176+
"series": "Peanuts"
1177+
],
1178+
"woodstock": [
1179+
"objectID": "2",
1180+
"name": "Woodstock",
1181+
"kind": ["bird", "animal" ],
1182+
"born": 1960,
1183+
"series": "Peanuts"
1184+
],
1185+
"charlie": [
1186+
"objectID": "3",
1187+
"name": "Charlie Brown",
1188+
"kind": [ "human" ],
1189+
"born": 1950,
1190+
"series": "Peanuts"
1191+
],
1192+
"hobbes": [
1193+
"objectID": "4",
1194+
"name": "Hobbes",
1195+
"kind": [ "tiger", "animal", "teddy" ],
1196+
"born": 1985,
1197+
"series": "Calvin & Hobbes"
1198+
],
1199+
"calvin": [
1200+
"objectID": "5",
1201+
"name": "Calvin",
1202+
"kind": [ "human" ],
1203+
"born": 1985,
1204+
"series": "Calvin & Hobbes"
1205+
]
1206+
]
1207+
1208+
// Populate index.
1209+
index.setSettings(settings) { (content, error) in
1210+
guard error == nil else { XCTFail(error!.localizedDescription); expectation.fulfill(); return }
1211+
self.index.addObjects(Array(objects.values)) { (content, error) in
1212+
guard error == nil, let taskID = content!["taskID"] as? Int else { XCTFail(error!.localizedDescription); expectation.fulfill(); return }
1213+
self.index.waitTask(withID: taskID) { (content, error) in
1214+
guard error == nil else { XCTFail(error!.localizedDescription); expectation.fulfill(); return }
1215+
// Query with no extra search parameters.
1216+
self.index.searchForFacetValues(of: "series", matching: "Hobb") { (content, error) in
1217+
guard error == nil else { XCTFail(error!.localizedDescription); expectation.fulfill(); return }
1218+
guard let facetHits = content!["facetHits"] as? [JSONObject] else { XCTFail("No facet hits"); expectation.fulfill(); return }
1219+
XCTAssertEqual(facetHits.count, 1)
1220+
XCTAssertEqual(facetHits[0]["value"] as? String, "Calvin & Hobbes")
1221+
XCTAssertEqual(facetHits[0]["count"] as? Int, 2)
1222+
// Query with extra search parameters.
1223+
let query = Query()
1224+
query.facetFilters = ["kind:animal"]
1225+
query.numericFilters = ["born >= 1955"]
1226+
self.index.searchForFacetValues(of: "series", matching: "Peanutz", query: query) { (content, error) in
1227+
guard error == nil else { XCTFail(error!.localizedDescription); expectation.fulfill(); return }
1228+
guard let facetHits = content!["facetHits"] as? [JSONObject] else { XCTFail("No facet hits"); expectation.fulfill(); return }
1229+
XCTAssertEqual(facetHits[0]["value"] as? String, "Peanuts")
1230+
XCTAssertEqual(facetHits[0]["count"] as? Int, 1)
1231+
expectation.fulfill()
1232+
}
1233+
}
1234+
}
1235+
}
1236+
}
1237+
self.waitForExpectations(timeout: expectationTimeout, handler: nil)
1238+
}
11611239
}

Tests/ObjectiveCBridging.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ - (void)testIndex {
237237
[index search:[Query new] completionHandler:^(NSDictionary<NSString*,id>* content, NSError* error) {
238238
// Do nothing.
239239
}];
240+
[index searchForFacetValuesOf:@"facet" matching:@"text" query:nil completionHandler:^(NSDictionary<NSString *,id>* content, NSError* error) {
241+
// Do nothing.
242+
}];
240243
[index getSettings:^(NSDictionary<NSString*,id>* content, NSError* error) {
241244
// Do nothing.
242245
}];

0 commit comments

Comments
 (0)