I.Tyger
I.Tyger

Reputation: 805

Objectify entity model design

i am using a design model that i'm not sure is totally right so i thought maybe i should throw a question out there to find out the best approach to go about it . I created a base entity with two fields like:

     public abstract  class BaseEntity
     @Index private Key<User> createdBy;
     @Index private DateTime creationDate = new DateTime();

Now i also have another sub class called User with its own indexes e.g :

     @Entity
     @Cache
     public class user extends BaseEntity
     @Index private String email ;
     @Index private String dob 

Now when i am writing my datastore-indexes.xml file is it right to do it this way :

    <datastore-index kind="User"  ancestor="false" source="manual">
    <property name="createdBy" direction="asc"/>
    <property name="creationDate" direction="asc"/>
    <property name="email" direction="asc"/>
    <property name="dob" direction="asc"/>
   </datastore-index>

      or 

     <datastore-index kind="User" ancestor="false" source="manual">
    <property name="email" direction="asc"/>
    <property name="dob" direction="asc"/>
</datastore-index>

Thanks .

Upvotes: 1

Views: 73

Answers (1)

Patrick Costello
Patrick Costello

Reputation: 3626

In Cloud Datastore, the indexes that you need are entirely dependent on the queries you want to run, not the data model. The index definition section in the documentation is very helpful. For example, if you wanted to run the query:

Query<User> q = ofy().load().type(User.class) .filter("email", "[email protected]").order("createdDate");

you would need the index:

<datastore-index kind="User" ancestor="false" source="manual"> <property name="email" direction="asc"/> <property name="createdDate" direction="asc"/> </datastore-index>

Neither of the indexes that you listed would satisfy the query (even though the properties listed in the second index is a superset of those listed in the necessary index).

You should run your application using the App Engine development server and run any queries you will need your application to run in your production service. This will automatically generate a datastore-indexes-auto.xml containing exactly the indexes required to serve those queries.

Upvotes: 1

Related Questions