2

我想创建一个查询,如果 2 个字段中存在某些内容(我想为每个字段显示 OR 而不是 AND),它将检查特定类这是我所做的,但它不起作用(基于我看到的一些示例)

    PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
   [query whereKeyExists:@"name"];  //this is based on whatever query you are trying to accomplish
    [query whereKeyExists:@"city"]; //this is based on whatever query you are trying to accomplish
    [query whereKey:@"description" containsString:searchTerm];

我该怎么做?

** 我想在我的查询中获取包含所有字段(名称、城市、描述)的 searchTeam 的所有结果

谢谢

4

3 回答 3

3

这个问题的答案很简单,只需要在同一个查询对象中添加多个 whereKey 方法调用即可。例如:

   PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
   [query whereKey:@"name" equal:@"someName"];
   [query whereKey:@"city" equal:@"someCity"];
   [query whereKey:@"description" containsString:searchTerm];

这将评估为原始所需的语句。

于 2014-10-27T19:07:45.897 回答
1

如果我正确理解了您的问题,您需要检查您的 searchTerm 是否在名称、城市或描述中。这样做的方法是:

PFQuery * nameQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"name" containsString:searchTerm];
PFQuery * cityQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"city" containsString:searchTerm];
PFQuery * descriptionQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"description" containsString:searchTerm];

PFQuery * query = [PFQuery orQueryWithSubqueries:@[nameQuery, cityQuery, descriptionQuery]];

这会从一组查询中创建一个复合或查询。您之前通过向同一查询添加更多约束所做的操作与编写 AND 查询相同。

IE: keyExists:@"name" AND keyExists:@"city" AND description CONTAINS searchTerm

于 2013-09-24T17:38:01.160 回答
0

你要这个:

NSString *className = @"YourParseClassName";
NSString *searchTerm = @"YourSearchTerm";
PFQuery *nameQuery = [PFQuery queryWithClassName:className];
[nameQuery whereKey:@"name" containsString:searchTerm];
PFQuery *cityQuery = [PFQuery queryWithClassName:className];
[cityQuery whereKey:@"city" containsString:searchTerm];
PFQuery *descriptionQuery = [PFQuery queryWithClassName:className];
[descriptionQuery whereKey:@"description" containsString:searchTerm];

PFQuery *allQuery = [PFQuery orQueryWithSubqueries:@[nameQuery, cityQuery, descriptionQuery]];
[allQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"Found objects: %@", objects);
    } else {
        NSLog(@"Received error: %@", error);
    }
}];

这样,您创建了三个单独的查询,然后将它们组合成一个更大的查询以一次返回所有对象。

但请记住,Parse Regex 搜索非常有限,如果您想要完成大量搜索,这可能不是最具可扩展性的选项。对于轻量级的工作或偶尔搜索的应用程序,这应该没问题。

于 2014-10-27T19:17:34.807 回答