搜索功能解析IOS

时间:2014-01-16 14:50:08

标签: ios search parse-platform

我想创建一个搜索功能,查询来自同一个类的两个不同的键。我可以使用哪种正则表达式返回最准确的值。例如:

 - (void)searchTableList {

[self.results removeAllObjects];

PFQuery *query = [PFQuery queryWithClassName:@"TC"];


CLLocationCoordinate2D coordinate = [self.delegate.location coordinate];
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude
                                              longitude:coordinate.longitude];

PFQuery *query1 = [PFQuery queryWithClassName:@"TC"];

[query1 setLimit:1000];
[query1 whereKey:@"location"
   nearGeoPoint:geoPoint
withinKilometers:5];
  [query1 whereKey:@"courses" matchesRegex:[NSString stringWithFormat:@"%@",[self.searchBar.text    lowercaseString]]];

 [query1 whereKey:@"owner" notEqualTo:[PFUser currentUser].username];
 [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
   for( PFObject * object in objects)
   {
       [self.results addObject:object];
   }
 [query setLimit:1000];
 [query whereKey:@"location"
   nearGeoPoint:geoPoint
   withinKilometers:5];
     [query whereKey:@"owner" matchesRegex:[NSString stringWithFormat:@"%@",[self.searchBar.text lowercaseString]]];

   [query whereKey:@"owner" notEqualTo:[PFUser currentUser].username];
  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for( PFObject * object in objects)
    {
        [self.results addObject:object];
    }


   [self.tableView reloadData];
}];


}];
}

我怎样才能接受这两个查询并以最准确的信息优先顺序排序。

1 个答案:

答案 0 :(得分:2)

如果要查找与多个查询之一匹配的对象,可以使用 orQueryWithSubqueries:方法。例如,如果你想找到有很多胜利或几场胜利的球员,你可以这样做:

PFQuery *lotsOfWins = [PFQuery queryWithClassName:@"Player"];
[lotsOfWins whereKey:@"wins" greaterThan:@150];

PFQuery *fewWins = [PFQuery queryWithClassName:@"Player"];
[fewWins whereKey:@"wins" lessThan:@5];
PFQuery *query = [PFQuery orQueryWithSubqueries:@[fewWins,lotsOfWins]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
  // results contains players with lots of wins or only a few wins.
}];