How to add a composite index in Google Datastore?

2019-06-21 05:02发布

I'm now using Google Datastore for my company's database. Today, I made a index and it successfully listed in 'Index'. But the size and entities of index which I made is empty.

The documentation of google Datastore says that the index is auto-genarated, but it wasn't.

Is there any command or something to do to generate the index?

The image below is a screenshot.

The upper one is the new one. The below one is already used.

enter image description here

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-21 05:18

As a matter of fact existing entities will not be indexed automatically. You have to load and save all your old entities (without index) in order to have the necessary indexes created for these entities.

Note, however, that changing a property from unindexed to indexed does not affect any existing entities that may have been created before the change. Queries filtering on the property will not return such existing entities, because the entities weren't written to the query's index when they were created. To make the entities accessible by future queries, you must rewrite them to the Datastore so that they will be entered in the appropriate indexes. That is, you must do the following for each such existing entity:

Retrieve (get) the entity from the Datastore. Write (put) the entity back to the Datastore. Similarly, changing a property from indexed to unindexed only affects entities subsequently written to the Datastore. The index entries for any existing entities with that property will continue to exist until the entities are updated or deleted. To avoid unwanted results, you must purge your code of all queries that filter or sort by the (now unindexed) property. (source)

Note that the documentation doesn't explicitly say the same for composed indexes. When you deploy a new composite index the index will appear in the developers console as "building" until it reaches "serving" state. Not sure what exactly it's building there, i usually re-saved all my entities and everything worked as it should.

auto-generated is a keyword that tells you whether you have manually created this index or whether it was created by the dev server when you made a query that required this index. This is in no way linked to how and when the indexes are created for the entities.

The <datastore-indexes> element has an autoGenerate attribute that controls whether this file should be considered along with automatically generated index configuration. See Using Automatic Index Configuration below. (source)

When you created a new index and you want this index for all your existing entities I recommend you create a cursor query to handle this. Usually I expose this query in an admin backend and have the query run until there are no results anymore. Why expose the thing? If you have lots of entities this job may run longer than the allowed 60 seconds in the frontend or 10 minutes in the backend. By exposing this I can use the front end instance time and don't have to worry about the time restrictions.

查看更多
登录 后发表回答