Search code examples
javagwtgoogle-cloud-datastorerequestfactoryobjectify

Objectify: sort results of a query ("no matching index found" error)


I am getting this error on the browser executing the query below, but without the ".order("position") it works (but results are obviously not sorted).

Uncaught com.google.web.bindery.event.shared.UmbrellaException: Exception caught: Server Error: no matching index found.
The suggested index for this query is:
    <datastore-index kind="Box" ancestor="false" source="manual">
        <property name="diagram_id" direction="asc"/>
        <property name="position" direction="asc"/>
    </datastore-index>

BoxDao.java

public List<Box> listFromDiagram(String diagramId)
{
    Objectify ofy = ObjectifyService.begin();
    // List of boxes of that diagram ordered by position asc
    Query<Box> q=ofy.query(Box.class).filter("diagram_id",diagramId).order("position");
    List<Box> results = q.list();

    return results;
}

Box.java

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Indexed;

@Entity
public class Box extends DatastoreObject{
    @Indexed private String boxId;
    @Indexed private String diagram_id;
    private String title;
    @Indexed private int position;
[...]
}

The file "datastore-indexes.xml" is empty... Any ideas? Thanks


Solution

  • You need a multi-property index (you are filtering by one property and sorting by another). The error message tells you what you need to put in datastore-indexes.xml. For a conceptual overview see:

    https://code.google.com/p/objectify-appengine/wiki/Concepts#Indexes

    I recommend following the links into the Google documentation as well.