Reputation: 81
currently I am trying to query for location entities, filtered by two range filters that restrict latitude and longitude to the current view.
The query is supposed to return a limited number of the entities in a random distribution. Querying as-it-is returns a set ordered by Latitude.
Location Entity: Latitude, Longitude, ...
My approach so far is the following: I introduced a randomKey property that is of type double and has a range [0...1]. I store it in the Datastore with each Location entity and try to sort by that property:
Location Entity: Latitude, Longitude, ..., randomKey
But after executing this query:
Query query = new Query("Location");
query.addSort("randomKey", Query.SortDirection.ASCENDING);
query.setFilter(latitudeRangeFilter);
query.setFilter(longitudeRangeFilter);
I get the a IllegalArgumentException:
java.lang.IllegalArgumentException: The first sort property must be the same as the property to which the inequality filter is applied. In your query the first sort property is randkey but the inequality filter is on longitude
Can someone give me an advice on how to proceed? Is my approach right or is there a better way?
Thanks in Advance!
Upvotes: 0
Views: 348
Reputation: 41089
Your query is not going to work because you specified inequality filters on two different properties. You may need to use a GeoSpacial query.
As for randomness, it may be easier to retrieve all entities within a specified range, and then choose randomly the number of results that you need.
If the number of locations is manageable, you may want to keep all of them in memcache as a list, instead of querying the datastore each time.Then you can iterate through that list to get your results, which will be very fast and incur no reading costs.
Upvotes: 2