问题
I'm working through the appengine+go tutorial, which connects in with Firebase: https://cloud.google.com/appengine/docs/standard/go/building-app/. The code is available at https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/gophers/gophers-6, which aside from my Firebase keys is identical.
I have it working locally just fine under dev_appserver.py
, and it queries the Vision API and adds labels. However, after I deploy to appengine I get an index error on datastore. If I go to the Firebase console, I see the collection (Post) and the field (Posted) which is a timestamp.
If I change this line: https://github.com/GoogleCloudPlatform/golang-samples/blob/master/appengine/gophers/gophers-6/main.go#L193 to remove the Order("-Posted")
then everything works (it's important to note that any Order
call causes it to error, except the test records I've posted come in random order.
The error message when running in appengine is: "Getting posts: API error 4 (datastore_v3: NEED_INDEX): no matching index found."
I've attempted to create a composite index, or test locally with --require_indexes=true
and it hasn't helped me debug the issue.
Edit: I've moved this over to use Firebase's Datastore libraries directly, instead of the GCP updates. I never solved this particular issue, but was able to move forward with my app actually working :)
回答1:
By default the local development server automatically creates the composite indexes needed for the actual queries invoked in your app. From Creating indexes using the development server:
The development web server (dev_appserver.py) automatically adds items to this file when the application tries to execute a query that needs an index that does not have an appropriate entry in the configuration file.
In the development server, if you exercise every query that your app will make, the development server will generate a complete list of entries in the
index.yaml
file.When the development web server adds a generated index definition to
index.yaml
, it does so below the following line, inserting it if necessary:# AUTOGENERATED
The development web server considers all index definitions below this line to be automatic, and it might update existing definitions below this line as the application makes queries.
But you also need to deploy the generated index configurations to the Datastore and let the Datastore update indexing information (i.e. the indexes to get into the Serving
state) for the respective queries to not hit the NEED_INDEX
error. From Updating indexes:
You upload your
index.yaml
configuration file to Cloud Datastore with thegcloud
command. If theindex.yaml
file defines any indexes that don't exist in Cloud Datastore, those new indexes are built.It can take a while for Cloud Datastore to create all the indexes and therefore, those indexes won't be immediately available to App Engine. If your app is already configured to receive traffic, then exceptions can occur for queries that require an index that is still in the process of being built.
To avoid exceptions, you must allow time for all the indexes to build. For more information and examples about creating indexes, see Deploying a Go App.
To upload your index configuration to Cloud Datastore, run the following command from the directory where your index.yaml is located:
gcloud datastore create-indexes index.yaml
For information, see the gcloud datastore reference.
You can use the GCP Console, to check the status of your indexes.
来源:https://stackoverflow.com/questions/50482669/firebase-datastore-need-index