2016-08-19 76 views
2

我試圖將Google Datastore數據庫作爲後端數據庫使用,並使用Google的this教程中的地理空間查詢進行查詢,但所有查詢均不返回任何內容。我做了Java中的下一個演示調試的servlet:Datastore GeoSpatial查詢不會返回

private static final String tableName = "DEBUG"; 
private static final double radius = 100 * 1000; //100 km 
private static final String NameKey = "Name"; 
private static final String LocationKey = "location"; 

//Parameters from the http servlet request 
String name; 
float lat, lng; 

//Insert a new person to the DB 

Entity person = new Entity(tableName); 
person.setProperty(NameKey, name); 
GeoPt location = new GeoPt(lat, lng); 
person.setProperty(LocationKey, location); 
datastore.put(person); 

//returns the users in the radius 

Query.Filter filter = new Query.StContainsFilter(LocationKey, new Query.GeoRegion.Circle(location, radius)); 
Query query = new Query(tableName).setFilter(filter); 

PreparedQuery pq = datastore.prepare(query); 

JsonArray users = new JsonArray(); 
for (Entity entity : pq.asIterable()){ 
    users.add(new JsonObject() 
     .add(NameKey, (String)entity.getProperty(NameKey)) 
     .add(LocationKey, ((GeoPt)entity.getProperty(LocationKey)).toString())); 
} 

result.add("Users", users); 

插入到數據庫的作品,下面是來自谷歌控制檯的截圖:

screenshot

但查詢總是失敗,並拋出:

javax.servlet.ServletContext log: DebugGeoPtServlet: Returns: {Error=no matching index found. 
The suggested index for this query is: 
    <datastore-index kind="DEBUG" source="manual"> 
     <property name="location" mode="geospatial"/> 
    </datastore-index> 

} 

我不知道代碼中出了什麼問題。我將它複製到教程中,並且我插入的點非常接近(不是,即使關閉100公里半徑也是如此)。

回答

1

您需要將此索引定義添加到您的datastore-indexes.xml文件(位於/WEB_INF文件夾中)。它看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<datastore-indexes 
    autoGenerate="true"> 

    <datastore-index kind="DEBUG" source="manual"> 
     <property name="location" mode="geospatial"/> 
    </datastore-index> 

</datastore-indexes> 

通常,當你在你的開發服務器嘗試不同的查詢,它會自動創建必要的索引定義。然而,有時候,您可能需要手動添加它們。

+0

我看來,我沒有一個,在我的項目(Android的Studio IDE中): 後端 SRC 主要 的java 資源 的webapp WEB-INF 的AppEngine-web.xml中 logging.properties 網絡。 xml index.html 我應該手動添加它嗎? 謝謝:) –

+0

是的,添加它。我會用樣本更新答案。 –