Google App Engine Error: No matching index found. (Java)

坚强是说给别人听的谎言 提交于 2019-12-06 14:41:08

问题


I am writing a query but it always says "No matching index found". I don't know why. My code is as below:

Query query = pm.newQuery(Classified.class);
query.setFilter("emp_Id == emp");
query.setOrdering("upload_date desc");
query.declareParameters("String emp");
List<Classified> results = (List<Classified>)query.execute(session.getAttribute("emp_Id").toString()); 

<?xml version="1.0" encoding="utf-8"?>
<datastore-indexes autoGenerate="true">
  <datastore-index kind="Classified" ancestor="false">
    <property name="emp_Id" direction="asc" />
    <property name="category" direction="asc" />
    <property name="upload_date" direction="desc" />
  </datastore-index>
</datastore-indexes> 

I have added the above index, but it did not help.


回答1:


I believe you need to configure a Datastore Index. There's probably one already generated for you in Eclipse at WEB-INF/appengine-generated/datastore-indexes-auto.xml that you just need to copy to WEB-INF/datastore-indexes.xml and deploy again.




回答2:


Because this needs to be somewhere on the internet...

I kicked myself when I found this out The error is you do not have a index matching what the query would like to perform. You can have multiple indexes for each entity.

In the Logcat, error, it will tell you exactly what index to set and what order the elements need to be.

ie, if the error says it wants (it wont be nicely formatted):

<datastore-index kind="Classified" ancestor="false">
    <property name="category" direction="desc" />
    <property name="upload_date" direction="desc" />
  </datastore-index>

then Project -> war -> WEB-INF -> appengine-generated -> datastore-indexes-auto.xml and add exactly that. Then, redeploy the project.

Next go into your Google Cloud Console and look at Datastore -> indexes. It should say that the index is being prepared (This goes quicker if you can kill all apps connected and shut down the instance in the console).

Once this has moved into the list of other indexes, rerun the your application and it wont error out with regards to the index anymore.

Go get it Gentlemen/Ladies




回答3:


The index you define must hold all possible results in the order they will be returned. Your query asks for a particular emp_Id, ordered by upload_date, but your index is ordered primarily by category.

Try removing the category line from your index definition, or swapping the order of category and upload_date, to make upload_date the primary sort order for the index. If another part of your code relies on the category line, you may have to make two separate indices (which incurs some computational cost).

Edit: see comment below by Nick Johnson re. extra parameters.



来源:https://stackoverflow.com/questions/5403080/google-app-engine-error-no-matching-index-found-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!