Nathaniel MacIver Nathaniel MacIver - 1 year ago 120
JSON Question

Datastore API Filter by field then sort ascending

I'm playing around with the Google Datastore API (runQuery method), and I am trying to run the gQuery String

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


Sending that JSON Object gives me the following error:



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"
}
}





Alternatively, If I run this string:

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

}

}


I get a 200 Response



200

- Show headers -

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





or if I run this one to just order all the results:

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


I get a 200 Response as well:



200

- Show headers -

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





So how can I do both operations in one line?

UPDATE:

As recommended below, I have used the google cloud platform to update the indexes manually. You create a valid yaml file in notepad and then use the upload tool (three vertical dots button on the right side of the cloud control command line tool) to place it on the server and direct to it with the comand line. Here are my results so far:

[email protected]:~$ 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
[email protected]:~$


This is the Yaml file I used:

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


Still unable to complete the query, but it may take time to populate. I'll check back through the day and update my results. As of 1:35PM EST, The indexes still don't seem to have updated, as shown below:

enter image description here

Answer Source

Like the error message says - in order to run the query with a WHERE and ORDER, you need a composite index on User and Start properties for Transaction kind. You can learn more about the indexes at https://cloud.google.com/datastore/docs/concepts/indexes.

You can create indexes using the gcloud command line tool. Refer to the documentation at - https://cloud.google.com/sdk/gcloud/reference/datastore/create-indexes

Once your index is created/active, which may take a while depending on the amount of data you have, you should be able to run the first query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download