数据存储API按字段筛选然后按升序排序

时间:2017-08-31 18:13:57

标签: sql json google-api google-cloud-datastore

我正在使用Google Datastore API(runQuery方法),我正在尝试运行gQuery字符串

'Select * from Transaction Where User = "[Me]" ORDER BY Start[date] ASC'

发送JSON对象会给我以下错误:



400
 
- Show headers -
  
{
 "error": {
  "code": 400,
  "message": "no matching index found. recommended index is:\n- kind: Transaction\n  properties:\n  - name: User\n  - name: Start\n",
  "status": "FAILED_PRECONDITION"
 }
}




或者,如果我运行此字符串:

 {
  "gqlQuery":
 {
  "allowLiterals":
  "queryString":"
     SELECT * FROM Transaction WHERE User = "[Me]"
    "

    }

  }

我得到200回复​​



200
 
- Show headers -
  
{
 "batch": {
  "entityResultType": "FULL",
  "entityResults": [
   {
    "entity": {
     "key": {
      "partitionId": {
       "projectId": "project-id-5200999099906492774"
      },
      "path": [
       {
        "kind": "Transaction",
        "id": "4693202737039992"
       }
      ]
     },....




或者如果我运行这个来命令所有结果:

'Select * from Transaction ORDER BY Start[date] ASC'

我也得到200响应:



200
 
- Show headers -
  
{
 "batch": {
  "entityResultType": "FULL",
  "entityResults": [
   {
    "entity": {
     "key": {
      "partitionId": {
       "projectId": "project-id-5200707080506492774"
      },
      "path": [
       {
        "kind": "Transaction",
        "id": "5641081148407808"
       }
      ]
     },...




那么如何在一行中完成两项操作呢?

更新

根据下面的建议,我使用谷歌云平台手动更新索引。您在记事本中创建一个有效的yaml文件,然后使用上传工具(云控制命令行工具右侧的三个垂直点按钮)将其放在服务器上并使用命令行指向它。以下是我到目前为止的结果:

nathaniel@project-id-5200707080555492774:~$ gcloud datastore create-indexes /home/nathaniel/index.yaml                                                                       

Configurations to update:
descriptor:      [/home/nathaniel/index.yaml]
type:            [datastore indexes]
target project:  [project-id-5200707044406492774]
Do you want to continue (Y/n)?  y
nathaniel@project-id-5200707080506492774:~$

这是我使用的Yaml文件:

indexes:
- kind: Transaction
  properties:
  - name: User
    direction: asc
  - name: Period
    direction: asc
  - name: Status
    direction: asc
  - name: auditStatus
    direction: asc
  - name: role
    direction: asc
  - name: Start
    direction: desc
  - name: End
    direction: asc

仍无法完成查询,但可能需要一段时间才能填充。我会在当天检查并更新我的结果。截至美国东部时间下午1点35分,索引似乎还没有更新,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

与错误消息一样 - 为了使用WHEREORDER运行查询,您需要UserStart属性的综合索引Transaction 1}}善良。您可以在https://cloud.google.com/datastore/docs/concepts/indexes了解有关索引的更多信息。

您可以使用gcloud命令行工具创建索引。请参阅 - https://cloud.google.com/sdk/gcloud/reference/datastore/create-indexes

上的文档

创建/激活索引后,可能需要一段时间,具体取决于您拥有的数据量,您应该能够运行第一个查询。