多个索引GAE客观化

时间:2016-09-01 17:54:01

标签: android google-app-engine indexing entity objectify

我的Android项目中有以下实体:

@Entity
public class Journey {

    @Id
    private Long id;
    private Ref<User> driver;
    @Index private Ref<Event> event;
    private Long nbPlaces;
    @Index private String departureTime;
    private String destination;
    @Index private String departureDate;
}

我的端点上的以下查询:

@ApiMethod(
        name = "listJourneyByEvent",
        path = "journeyByEvent",
        httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Journey> listJourneyByEvent(@Named("idEvent") Long idEvent, @Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Key<Event> key = Key.create(Event.class, idEvent);

    Query<Journey> query = ofy().load().type(Journey.class).filter("event", key).order("departureDate").limit(limit);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<Journey> queryIterator = query.iterator();
    List<Journey> journeyList = new ArrayList<Journey>(limit);
    while (queryIterator.hasNext()) {
        journeyList.add(queryIterator.next());
    }
    return CollectionResponse.<Journey>builder().setItems(journeyList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}

但是当我执行该方法时,我收到以下错误:

E/ch.hevs.co_voituragefiesta.async.AsyncJourneyList: com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
                                                                                                               {
                                                                                                                 "code": 503,
                                                                                                                 "errors": [
                                                                                                                   {
                                                                                                                     "domain": "global",
                                                                                                                     "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Journey\n  properties:\n  - name: event\n  - name: departureDate\n\nThe suggested index for this query is:\n    <datastore-index kind=\"Journey\" ancestor=\"false\" source=\"manual\">\n        <property name=\"event\" direction=\"asc\"/>\n        <property name=\"departureDate\" direction=\"asc\"/>\n    </datastore-index>\n\n",
                                                                                                                     "reason": "backendError"
                                                                                                                   }
                                                                                                                 ],
                                                                                                                 "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Journey\n  properties:\n  - name: event\n  - name: departureDate\n\nThe suggested index for this query is:\n    <datastore-index kind=\"Journey\" ancestor=\"false\" source=\"manual\">\n        <property name=\"event\" direction=\"asc\"/>\n        <property name=\"departureDate\" direction=\"asc\"/>\n    </datastore-index>\n\n"
                                                                                                               }

我读过GAE需要一些时间来生成索引,但我认为这不是问题。

我还读到我们在一个实体中不能有多个索引。这是真的吗?

由于

1 个答案:

答案 0 :(得分:0)

  

数据存储区为以下表单的查询构建自动索引:   ...在属性上没有过滤器且只有一个排序顺序的查询,无论是升序还是降序......

documentation source

您正在使用带过滤和排序的查询,因此您必须手动指定索引。 Google为您提供例外情况下的索引定义:

<datastore-index kind="Journey" ancestor="false" source="manual">
        <property name="event" direction="asc"/>
        <property name="departureDate" direction="asc"/>
</datastore-index>

在datastore-indexes.xml文件中添加此索引,部署并等待构建索引(在控制台中检查数据存储索引状态)