I am trying to query the Datastore, and my query looks like this:
SELECT *
FROM mydb
WHERE Latitude = "18.1" AND Number > "1"
It doesn't work though. I get this error in the Datastore query box:
GQL query error: Your Datastore does not have the composite index (developer-supplied) required for this query.
And this error when I run my code:
no matching index found. recommended index is:\n- kind: mydb\n properties:\n - name: Location\n - name: Number\n
Simple requests like this work:
SELECT *
FROM mydb
WHERE Number > "1" AND Number < "5"
I am only accessing a single column here maybe that is why?
Nope,
Then I tried a request like this:
SELECT *
FROM mydb
WHERE Latitude = "18.1" AND Number = "1"
This worked.
I tried to look up a solution, and I came across this page: https://cloud.google.com/datastore/docs/tools/indexconfig#Datastore_About_index_yaml
After going through that page, I gathered that I need an index.yaml file somewhere. It is supposed to go in a folder called WEB-INF. But I don't have this folder.
This is a little snippet of my code:
Query<Entity> query = Query
.gqlQueryBuilder(Query.ResultType.ENTITY,
"SELECT * FROM " + kind + " WHERE Location = @location AND Number <= @number")
.setBinding("number", "5").setBinding("location", "18.1").build();
QueryResults<Entity> results = datastore.run(query);
A GQL query returns zero or more entity results of the requested kind, with each result consisting of an entire entity or some subset of the properties of the entity, or even just the entity key. For example, SELECT * FROM myKind. SELECT __key__ FROM myKind.
The Python Datastore API provides two classes for preparing and executing queries: Query uses method calls to prepare the query. GqlQuery uses a SQL-like query language called GQL to prepare the query from a query string.
Ancestors and Entity Groups For highly related or hierarchical data, Datastore allows entities to be stored in a parent/child relationship. This is known as an entity group or ancestor/descendent relationship. This is an example of an entity group with kinds of types person, pet, and toy.
Updating entities To update an existing entity, modify the attributes of the Entity object, then pass it to the DatastoreService. put() method. The object data overwrites the existing entity. The entire object is sent to Datastore with every call to put() .
The error you get is because the query you're attempting requires Composite indexes which are not available by default. They must be specified within index.yaml
.
The article Creating index files which is somewhat different than the one posted is specifically for Java applications running in the flexible environment.
There are 2 ways to create an index.yaml
:
gcloud beta emulators datastore start
command. You can also use the --data-dir <dir>
option to specify where the generated index.yaml
should be written.Then, once you have index.yaml
and the same directory as app.yaml
, you can deploy it with gcloud preview app deploy index.yaml
from that directory. This process is briefly documented in Deploying the index file.
I would also recommend reading Organizing yaml Configuration Files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With