[ios sdk 0.9] Help ordering by distance

117 views
Skip to first unread message

Manar

unread,
Feb 25, 2015, 4:02:22 PM2/25/15
to baa...@googlegroups.com

So I'm trying to have Baasbox sort my query by distance and I'm unable to do so no matter what I try.  Here is my code, I've tried numerous variations of it, but I can't' get it to give me an array sorted by distance....



Please help...


Thank you





NSString *fields =  [NSString stringWithFormat:@"user,distance(lat,long,%f, %f) < 5  as dist", self.myLat, self.myLong];

   

  NSDictionary *parameters = @{kPageNumberKey : @0, kPageSizeKey : @3, @"fields": distanceKey, @"orderBy":@"dist desc" };

    [PFPost getObjectsWithParams:parameters

                   completion:^(NSArray *pois, NSError *error) {

                       if (error == nil) {

                           

                           NSMutableArray *data = [[NSMutableArray alloc]initWithArray:pois];


                           self.data = data;

                           [self.tableView reloadData];

                       } else {

                           // deal with error


                       }

                   }];

    

 



Cesare

unread,
Feb 26, 2015, 6:19:11 AM2/26/15
to baa...@googlegroups.com
Shouldn't it be:

NSDictionary *parameters = @{kPageNumberKey : @0kPageSizeKey : @3@"fields": fields, @"orderBy":@"dist desc" };

?

Manar

unread,
Feb 26, 2015, 9:29:58 AM2/26/15
to baa...@googlegroups.com
Yes, it should be - I actually just forgot to change it when copying the code over here...but I've tried that and it doesn't work...

Manar

unread,
Mar 14, 2015, 1:43:59 PM3/14/15
to baa...@googlegroups.com
Is there any ideas of how to get this to work?  I'm still having problems, it would be very appreciated.

Stanislav Dmitrenko

unread,
Mar 14, 2015, 2:01:47 PM3/14/15
to baa...@googlegroups.com
I also trying to get this working but without success. The problem is OrientDB doesn't work with this kind of query. I don't know why but I have solutions.
1. You can change your code and delete "< 5". And when you get all results just delete those of them who have distance > 5 via plugin JavaScript.
2. Second solution for know doesn't work with Baasbox because it doesn't allow us to make complex queries to db. But on the future I'll show you the right code.
All you should to do is to make two selects in one call.
Select from (select user,distance(lat,long,%f, %f) < 5 as dist from Collection) order by dist desc (or asc).

I think we should ask our friends in Baasbox team to help us and make a method that will make complex queries to OrientDB.

giastfader

unread,
Mar 16, 2015, 6:51:51 AM3/16/15
to baa...@googlegroups.com
Hi,
if you want to filter, you have to use the where clause.
I see in your code that you put the condition into the fields variable instead.

For example this raw api call works:
http://localhost:9000/document/test_distance?where=distance(lat,long,43.722839,10.401689) < 5

and if you want also other info and order by distance:

http://localhost:9000/document/test_distance?fields=user, distance(lat,long,43.722839,10.401689) as dist&where=distance(lat,long,43.722839,10.401689) < 5&orderBy=dist desc

So you should use three variables: one for projection (fields), one for selection (where), one for sorting (orderBy)



Stanislav Dmitrenko

unread,
Mar 16, 2015, 6:58:59 AM3/16/15
to baa...@googlegroups.com
giastfader, your code should work but it looks like orientdb needs to make a harder job with two equals queries. Is it true?
First:
distance(lat,long,43.722839,10.401689)
Orientdb are getting distance

Second:
distance(lat,long,43.722839,10.401689) < 5
Orientdb are getting distance < 5. It should rescan all documents twice.

giastfader

unread,
Mar 16, 2015, 7:10:59 AM3/16/15
to baa...@googlegroups.com
Why do you think OrientDB will scan the class twice?
The API call I wrote is transformed in the following OrientDB query:

select user, distance(lat,long,43.722839,10.401689) as dist from test_distance where distance(lat,long,43.722839,10.401689) < 5

The lat and long fields belong to the record that is analyzed at that moment. 
The db engine performs the first calculation and if it is clever enough,seeing that the second one is the same, applies directly the condition.
But, even if the parser is not so smart, it will execute just the distance() function twice, not a new class scan.

Stanislav Dmitrenko

unread,
Mar 16, 2015, 7:12:18 AM3/16/15
to baa...@googlegroups.com
Super! Thanks!)

Manar

unread,
Apr 17, 2015, 12:59:16 PM4/17/15
to baa...@googlegroups.com
Hi I'm still having some issues trying to figure this out under iOS objective-c.

I keep getting an array with with the right length I am expecting, except all of the values are nil.... any thoughts?
Reply all
Reply to author
Forward
0 new messages