1

I am using Google Cloud datastore with (C#) .NET Code, I have table called Audit in Datastore with following columns

ID/Name (long), ID (long), ActionType (String), GroupType (String), DateTime (TimeStamp), CompanyID (long), UserID (long), ObjectID (long), IsDeleted (boolean)

FYI, Both ID/Name and ID contains same information

I want to run GQL query like this

SELECT * FROM Audit WHERE GroupType='XYZ' AND ActionType='CREATED' AND CompanyID=152738292 and IsDeleted=false Order by ID

I don't seem be get right composite key and keep getting "Your Datastore does not have the composite index (developer-supplied) required for this query"

Here is the composite index created using index.yaml.

- kind: Audit

  properties:
  - name: ID
    direction: desc
  - name: IsDeleted
  - name: CompanyID
    direction: desc
  - name: GroupType
  - name: ActionType

Any Help on this subject would be appreciated!!

Let me know if you need additional information

Hardik Patel
  • 123
  • 10

1 Answers1

2

For this query you need a composite index where ID is the last element, and its sort order matches the sort order in your query. For example:

- kind: Audit
  properties:
  - name: IsDeleted
  - name: CompanyID
    direction: desc
  - name: GroupType
  - name: ActionType
  - name: ID

(ID is implicitly sorted ascending in this index definition.)

Ed Davisson
  • 2,927
  • 11
  • 11
  • Thanks Ed, It worked!! I can now run query like SELECT * FROM Audit WHERE CompanyID=167282 AND GroupType='XYZ' AND ActionType='CREATED' AND IsDeleted=false Order by ID desc LIMIT 5 – Hardik Patel Nov 22 '16 at 09:34
  • @ED - whats the secret sauce here? I had the same problem and moving the order by field to the bottom of the index definition worked out..it must be something silly I miss.. trying to understand what that is.. – Sahas Jan 28 '17 at 01:29
  • There's some info here: https://cloud.google.com/datastore/docs/concepts/indexes#index_configuration. Or you can use `gcloud` to help you determine what indexes you'll need (https://cloud.google.com/datastore/docs/tools/). If you make a query that requires an index you don't have, the error message should also recommend an index for you. – Ed Davisson Jan 30 '17 at 17:42