Sort Items Objectify Getting error No matching Ind

2019-08-17 07:07发布

I am trying to sort a list of items by my property 'price' but I'm getting error.

This is my filter

items = ofy().load().type(MyItem.class)
            .filter("OtherEntityRef", tmpEntity)
            .order("price").limit(100)
            .list();

But we I run it, the log shows the erro:

 Uncaught exception from servlet 
java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: no matching index found.
The suggested index for this query is:
    <datastore-index kind="MyClass" ancestor="false" source="manual">
        <property name="OtherEntityRef" direction="asc"/>
        <property name="price" direction="asc"/>
    </datastore-index>

 (through reference chain: java.util.HashMap["items"])
    at com.google.api.server.spi.response.ServletResponseResultWriter.writeValueAsString(ServletResponseResultWriter.java:187)
    at com.google.api.server.spi.response.ServletResponseResultWriter.write(ServletResponseResultWriter.java:74)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:394)
    at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:160)
    at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:118)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:260)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:78)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:148)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:468)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:235)
    at java.lang.Thread.run(Thread.java:745)

I have defined the price as @Index, so it should be indexed.

@Index
 double price;

Do someone knows what it could be?

1条回答
干净又极端
2楼-- · 2019-08-17 07:32

After the help of @Dan Cornilescu I found the problem.

I had not had any composite Indexes. My Datastore -> Indexes was empty. To solve it I created a XML file under the WEB-INF folder called datastore-indexes.xml and there I explicitly defined my index.

<?xml version="1.0" encoding="utf-8"?>
<datastore-indexes
    autoGenerate="true">
    <datastore-index kind="MyEntity" ancestor="false" source="manual">
        <property name="OtherEntityRef" direction="asc"/>
        <property name="price" direction="asc"/>
    </datastore-index>
   </datastore-indexes>

As I understood was that if you use order if you have to add your own index. The built-in does not work in this case.(Here I am expressing my basic knowledge).

Thank you @Dan Cornilescu for the time you take to help.

查看更多
登录 后发表回答