Javascript parse.com在数组中搜索部分字符串
我正在尝试搜索Parse.com字段,该字段是部分字符串的数组 当字段为字符串格式时,我可以执行以下操作:Javascript parse.com在数组中搜索部分字符串,javascript,arrays,parse-platform,Javascript,Arrays,Parse Platform,我正在尝试搜索Parse.com字段,该字段是部分字符串的数组 当字段为字符串格式时,我可以执行以下操作: // Update the filtered array based on the search text and scope. // Remove all objects from the filtered search array [self.searchResults removeAllObjects]; // Filter the array using NSPredica
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searchResults removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.busnumber contains[c] %@", searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.objects filteredArrayUsingPredicate:predicate]];
PFQuery * query = [PFQuery queryWithClassName:@"Bus"];
[query whereKey:@"route" equalTo:[NSString stringWithFormat:@"%@", searchText]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"Objects: %@", objects);
if (error)
{
NSLog(@"ERROR: %@", error.localizedDescription);
}
else
{
[self.searchResults removeAllObjects];
[self.searchResults addObjectsFromArray:objects];
[self.searchDisplayController.searchResultsTableView reloadData];
}}];
这是可行的,但是我要搜索的新字段是一个数组。
当我将其更改为以下内容时,它会工作:
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searchResults removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.busnumber contains[c] %@", searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.objects filteredArrayUsingPredicate:predicate]];
PFQuery * query = [PFQuery queryWithClassName:@"Bus"];
[query whereKey:@"route" equalTo:[NSString stringWithFormat:@"%@", searchText]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"Objects: %@", objects);
if (error)
{
NSLog(@"ERROR: %@", error.localizedDescription);
}
else
{
[self.searchResults removeAllObjects];
[self.searchResults addObjectsFromArray:objects];
[self.searchDisplayController.searchResultsTableView reloadData];
}}];
但是,我需要这个字符串的精确值
我希望能够搜索字符串的某些部分,但当我将其更改为:
[query whereKey:@"route" containsString:[NSString stringWithFormat:@"%@", searchText]];
我得到:
[Error]: $regex only works on string fields (Code: 102, Version: 1.7.4)
有什么想法吗?谢谢:)您尝试的是rational,但是PFQuery上的字符串限定符只对字符串起作用 我经常看到这个主题:PFQuery只提供简单属性的基本比较。要做更多的事情,必须查询超集并进行应用程序级计算,以将超集减少到所需的集。这样做代价高昂,原因有二:应用程序级的计算速度/空间,以及超集的网络传输 通过使用云函数对超集进行应用程序级缩减,第一项费用得到缓解,第二项费用得到消除。除非您需要客户端上的超集记录,否则请考虑将此查询移动到云。 针对这个问题,我认为云函数类似于:
// very handy to have underscore available
var _ = require('underscore');
// return Bus objects whose route array contains strings which contain
// the passed routeSubstring (request.params.routeSubstring)
Parse.Cloud.define("busWithRouteContaining", function(request, response) {
// for now, don't deal with counts > 1k
// there's a simple adjustment (using step recursively) to get > 1k results
var query = new Parse.Query("Bus");
query.find().then(function(buses) {
var matching = _.select(buses, function(bus) {
var route = bus.get("route");
var routeSubstring = request.params.routeSubstring;
return _.contains(route, function(routeString) {
return routeString.includes(routeSubstring);
});
});
response.success(matching);
}, function(error) {
response.error(error);
});
});
如果您决定在客户机上执行缩减,并且需要代码方面的帮助,我可以在中对其进行编辑。这将是一个非常简单的切换,切换到
predicateWithBlock:
,使用一个块来迭代数组属性并检查每个属性上的rangeOfString:
。我不完全确定在何处以及如何实现这一点。。。我已经添加了云代码,并使用PFCloud callFunctionInBackground在-(void)filterContentForSearchText中调用了它,但是结果是空的…可能没有匹配的?你记得分析部署吗?一个好的调试步骤是只返回查询结果(即注释掉find result函数中的所有内容,然后说response.success(总线)
)。如果失败,尝试硬编码一个结果,比如response.success(“helloworld”)代码>并查看它是如何工作的。我很有信心这种方法是有效的(并且没有更好的替代方法)。好的,所以改为response.success(bus)
将返回所有总线的结果。但是,我无法在搜索时获取任何数据,即使我1000%确定数组中存在字符串(或其部分)。“route”数组的示例:[“Pak Thang Khlong Kwang”、“Mooban Sethakit”、“Mall Bang Khae”]
我的代码:[PFCloud调用函数background:@“busWithRouteContaining”,带参数:@{“routeSubstring”:searchText}块:^(NSArray*结果,NSError*错误){如果(!error){//结果是@“Hello world!”NSLog(@“result:%@”,result);}}}]代码>