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