The usual datastore queries based on keys and index.yaml are useful, but don't address all needs. I sometimes leverage the
Search API when I need to retrieve entities (or entities fragments) based on custom criteria.
It requires a little plumbing, here is how I do it :
- Whenever an entity is created or updated, create or update a corresponding Document. This can be done in an async delayed task if perf matters.
- Whenever an entity is deleted, delete a corresponding Document. This can be done in an async delayed task if perf matters.
- Design your searchable Document struct so it contains the datastore key, and the fields that you want to search.
- Design your searchable Document struct so that it already contains the subset of fields that you want to display in result list. Then the list wan be displayed directly, not needing a subsequent datastore query.
- If you need the true entities (e.g. to update them), then make a single subsequent datastore query GetMulti.
- The "source of truth" is still the database, i.e. the datastore entities. The searchable documents are just redundant data stored in a search.Index . Consider extra safety code to reindex everything in case the data accidentaly diverges.
This usually works well for my needs. Hope it helps.
Cheers
Valentin