Retroactive indexing in Google Cloud Datastore

。_饼干妹妹 提交于 2019-12-28 06:33:06

问题


There are many properties in my model that I currently don't need indexed but can imagine I might want indexed at some unknown point in the future. If I explicitly set indexed=False for a property now but change my mind down the road, will Datastore rebuild the entire indices automatically at that point, including for previously written data? Are there any other repercussions for taking this approach?


回答1:


No, changing indexed=True to indexed=False (and vice-versa) will only affect entities written after that point to the datastore. Here is the documentation that talks about it and the relevant paragraph:

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.

If you decide later that you want to starting indexing properties, you'll have to go through your entities and re-put them into the datastore.

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.




回答2:


To index properties of existing entities (as per the documentation):

  1. Retrieve (get) the entity from the Datastore.
  2. Write (put) the entity back to the Datastore.

didn't work for me. I employed appengine-mapreduce library and wrote a MapOnlyMapper<Entity, Void> using DatastoreMutationPool for indexing all the existing entities in Datastore.

Lets assume the property name was unindexed and I want to index this in all the existing entities. What I had to do is:

@Override
public void map(Entity value) {
    String property = "name";
    Object existingValue = value.getProperty(property);
    value.setIndexedProperty(property, existingValue);
    datastoreMutationPool.put(value);
}

Essentially, you will have to set the property as indexed property using setIndexedProperty(prop, value) and then save (put) the entity.

I know I am very late in posting an answer. I thought I could help someone who might be struggling with this problem.



来源:https://stackoverflow.com/questions/21008580/retroactive-indexing-in-google-cloud-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!