[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eDatastore utilizes indexes for every query, which are updated upon entity changes to ensure rapid query results.\u003c/p\u003e\n"],["\u003cp\u003eComplex queries in Datastore require predefined indexes, which are specified in an \u003ccode\u003eindex.yaml\u003c/code\u003e configuration file.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eindex.yaml\u003c/code\u003e file, written in YAML format, defines indexes with properties like \u003ccode\u003ekind\u003c/code\u003e, \u003ccode\u003eproperties\u003c/code\u003e (including \u003ccode\u003ename\u003c/code\u003e and \u003ccode\u003edirection\u003c/code\u003e), and \u003ccode\u003eancestor\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe Datastore emulator can automatically generate and update the \u003ccode\u003eindex.yaml\u003c/code\u003e file during local testing, adding necessary index definitions below the \u003ccode\u003e# AUTOGENERATED\u003c/code\u003e line.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egcloud\u003c/code\u003e command-line tool is used to deploy the \u003ccode\u003eindex.yaml\u003c/code\u003e file to the App Engine (\u003ccode\u003egcloud app deploy index.yaml\u003c/code\u003e) and to delete unused indexes (\u003ccode\u003egcloud datastore indexes cleanup index.yaml\u003c/code\u003e).\u003c/p\u003e\n"]]],[],null,["# Configuring Datastore Indexes with index.yaml\n\nYou can use [Firestore in Datastore mode (Datastore)](/appengine/docs/legacy/standard/go111/using-cloud-datastore)\nfor storing data for your applications that run in the standard\nenvironment. Datastore uses\n[indexes](/datastore/docs/concepts/indexes)\nfor every query your application makes. These indexes are updated whenever an\nentity changes, so the results can be returned quickly when the app makes a\nquery. To do this, Datastore needs to know in advance which\nqueries the application will make. You specify which indexes your app needs in a\n`index.yaml` configuration file. You can use the Datastore\nemulator to generate the file automatically as you test your app, or write the\nfile yourself. The `index.yaml` file must be uploaded when you deploy your app.\n\nAbout `index.yaml`\n------------------\n\nEvery Datastore query made by an application needs a\ncorresponding index. Indexes for simple queries, such as queries over a single\nproperty, are created automatically. Indexes for complex queries must be defined\nin a configuration file named `index.yaml`. This file is uploaded with the\napplication to create indexes in Datastore.\n\nThe following is an example of an `index.yaml` file: \n\n indexes:\n\n - kind: Cat\n ancestor: no\n properties:\n - name: name\n - name: age\n direction: desc\n\n - kind: Cat\n properties:\n - name: name\n direction: asc\n - name: whiskers\n direction: desc\n\n - kind: Store\n ancestor: yes\n properties:\n - name: business\n direction: asc\n - name: owner\n direction: asc\n\nThe syntax of `index.yaml` is the YAML format. For more information about this\nsyntax, see [the YAML website](http://www.yaml.org/) for more information.\n| **Note:** The YAML format supports comments. A line that begins with a pound (`#`) character is ignored: \n| `# This is a comment.`\n\nIndex definitions\n-----------------\n\n`index.yaml` has a single list element called `indexes`. Each element in the\nlist represents an index for the application.\n\nAn index element can have the following elements:\n\n`kind`\n: The kind of the entity for the query. This\n should be the `kind` argument given to [`datastore.NewKey`](/appengine/docs/standard/go/datastore/reference#NewKey)\n when creating the entity. This element is required.\n\n`properties`\n\n: A list of properties to include as columns of the index, in the order to be\n sorted: properties used in equality filters first, followed by the property used\n in inequality filters, then the sort orders and their directions.\n\n Each element in this list has the following elements:\n\n `name`\n : The datastore name of the property.\n\n `direction`\n : The direction to sort, either `asc` for ascending or `desc` for descending. This is only required for properties used in sort orders of the query, and must match the direction used by the query. The default is `asc`.\n\n`ancestor`\n\n: `yes` if the query has an ancestor clause ([Query.Ancestor](/appengine/docs/standard/go/datastore/reference#Query.Ancestor)). The default is `no`.\n\nCreating index files\n--------------------\n\nYou can create an index file manually, using a text editor and following the\nfile layout described above. A more efficient approach is to automatically\ngenerate the file as you test your app locally. You can combine the two methods.\n\nWhen you are testing in your local environment, you can use the\n[gcloud emulator command](/sdk/gcloud/reference/beta/emulators/datastore/start)\nto start a service that emulates Datastore before you run your\napp: \n\n gcloud beta emulators datastore start --data-dir DATA-DIR\n\nUse the `--data-dir` flag to specify the directory where the auto-generated\n`index.yaml` file will appear.\n\nAs you test your app, each time you generate a Datastore query,\nthe emulator adds a generated index definition to `index.yaml`. All the\nautomatically generated index definitions will appear in the file below the\nfollowing line: \n\n # AUTOGENERATED\n\nAll index definitions above this line are considered to be under manual\ncontrol, and are not updated by the development web server. The web server\nwill only make changes below the line, and will only do so if the complete\n`index.yaml` file does not describe an index that accounts for a query executed\nby the application. To take control of an automatic index definition, move it\nabove this line.\n\nThe emulator may update existing definitions below this line as the application\nmakes queries. If the app generates every query it will make while you test it,\nthen the generated entries in `index.yaml` will be complete. You might need to\nedit the file manually to delete indexes that are not used in production, or to\ndefine indexes that were not created while testing.\n\nDeploying the index configuration file\n--------------------------------------\n\nTo deploy the `index.yaml` configuration file, run the following command:\n\n\u003cbr /\u003e\n\n gcloud app deploy index.yaml\n\nDeleting unused indexes\n-----------------------\n\nWhen you change or remove an index from the index configuration, the original\nindex is not deleted from App Engine automatically. This gives you the\nopportunity to leave an older version of the app running while new indexes are\nbeing built, or to revert to the older version immediately if a problem is\ndiscovered with a newer version.\n\nWhen you are sure that old indexes are no longer needed, you can delete them\nfrom App Engine as follows: \n\n gcloud datastore indexes cleanup index.yaml"]]