سؤال

I have an existing datastore entity as below:

@Data
@Entity
public class Data
{
    @Id @Index long id;
    boolean expired;
}

I am in need to filter the data based on expired field, so I have to change the entity to have the filed expired now indexed. The modified entity is as below:

@Data
@Entity
public class Data
{
    @Id @Index long id;
    @Index boolean expired;
}

Below is the existing data in the datastore before the Index created:

Column : Data

id : 1

expired : true

id : 2

expired : false

id : 3

expired : false

My intention is to fetch the data using objectify with a filter on expired, so I have my code modified to use the below objectify quering:

return (List<Data>) ofy.load().type( Data.class ).filter( "expired = ", false ).list();

It is supposed to return two records, however it does return nothing. BTW, after Index created, I added a new record into the entity as below

id : 4

expired : false

If I query back, I now see one result and that is with id = 4. Seems the filter works only on the newly added records and the index dint apply on all the old records? How to resolve this problem?

هل كانت مفيدة؟

المحلول

The reason the existing entities are not being returned is because they have not been indexed since you modified your entity class.The Objectify documentation states that:

Single property indexes are created/updated when you save an entity.

To get the existing existing entity instances to appear in your queries you will have to resave them, which will consequently force the Datastore to create their indexes.

You have to do the following for each existing entity:

  1. Retrieve (load) the existing entity from the Datastore
  2. Write (save) the existing entity back to the Datastore
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top