多个索引 GAE 对象化

Multiple indexes GAE 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 需要一些时间来生成索引,但我认为这不是问题所在。

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

谢谢

Datastore builds automatic indexes for queries of the following forms: ... Queries with no filters and only one sort order on a property, either ascending or descending ..

documentation source

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

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

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