1
votes

I am trying to sort a few entities I pulled from a dataset according to score, but currently it returns only 1 entity instead of the 4 currently in the datastore.

The model is defined below:

class Place(ndb.Model):
    """Model for places."""
    title = ndb.StringProperty()
    url = ndb.StringProperty()
    longitude = ndb.StringProperty()
    latitude = ndb.StringProperty()
    score = ndb.IntegerProperty()
    votes = ndb.KeyProperty(repeated = True, kind = 'Vote')
    reviews = ndb.KeyProperty(repeated = True, kind = 'Review')

And the query I'm trying to fetch the 10 highest scored places is:

places = Place.query().order(Place.score).fetch(10)

The current dataset that I have: Screenshot of datastore

This results in only place A being returned and not the other entities.

How can I sort the entities from highest to lowest score?

PS: I have Googled all over and could not find any working solution.

1

1 Answers

5
votes

What you're doing is fine, so the issue is with the data itself.

From the looks of the datastore console I can tell the writes were done in different ways, the shape of the data is very different between the entity you get and the rest, and there's also the Write Ops column, which tells us a great deal.

The entities with only 2 writes don't have any property indexed, other than the ID itself, which is written in ascending and descending order, resulting in said 2 write operations.

The entity you're getting has 4 writes, clearly there's other index being written, and it must be score, otherwise you wouldn't be getting any data. Queries only work for indexed properties, entities can exist but be "invisible" if the filter property is not indexed at the time of execution.

If the model looks the same way in your code as in this question, put()ing the entities again should write 12 indexes at least.

Couldn't tell you what caused this, maybe the properties were indexed=False initially?