Search code examples
c#google-app-enginegoogle-cloud-datastoregqlcomposite-index

Google Cloud Datastore : Composite Index : Order by does not seems to work


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


Solution

  • 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.)