google-app-enginegoogle-cloud-datastoregquery

How to use projection properties along with equality filter in GQuery?


My gquery goes something like this :

SELECT  Distinct Path,Value FROM Entity WHERE PID="chichi"

I get error as

GQL query error: Your Datastore does not have the composite index (developer-supplied) required for this query.

I know I am using projections with equality query , I have added path,value and plan id in the index.yaml(composite index file). How do I execute this query on gcloud datastore?

I have included all possible combination in the index.yaml file

index.yaml:
indexes:
- kind: Entity
  properties:
  - name: PID  
  - name: Value
  - name: Path

Solution

  • I have been able to reproduce your error myself creating a set of entities with the properties you specified. In order to get rid of the error, as you pointed out, I had to create an index. Queries that work with multiple properties at the same time are not automatically defined as an index, so they have to be manually indexed. Indexes are defined in the index.yaml file, which should look something like this one:

    indexes:
    - kind: Entity
      properties:
      - name: PID
      - name: Path
      - name: Value
    

    Of course, it can contain more indexes in case you are using any other different one. Once you have this index.yaml file created, you can upload it to your project with the following command:

    gcloud datastore create-indexes index.yaml
    

    It will take some time to load, but once it is ready, you will be able to find it in the Datastore > Indexes tab in the Console:

    enter image description here

    Once the green tick is shown, the index will be ready to serve, and you can use GQL to query your Datastore content:

    enter image description here

    Bear in mind that there are some best practices recommended for defining indexes, and the most important one which you should consider (in order to run an optimal query):

    The perfect index for a query, which allows the query to be executed most efficiently, is defined on the following properties, in order:

    1. Properties used in equality filters
    2. Property used in an inequality filter
    3. Properties used in sort orders

    So make sure that your index configuration looks like the one I shared, and that it is properly uploaded to your project.

    Finally, there are some limitations to projection queries in Datastore, put your query does not seem to be falling in any of them, so that should not be an issue in this case.