Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
En plus d'indexer le texte, l'index de recherche Spanner permet d'indexer et d'interroger efficacement les documents JSON et JSONB. Utilisez des index de recherche pour les requêtes JSON et JSONB autonomes, ou pour enrichir d'autres requêtes de recherche dans le texte.
Vous pouvez utiliser la fonction TOKENIZE_JSON pour créer un indice JSON dans GoogleSQL ou la fonction TOKENIZE_JSONB pour créer un indice JSONB dans PostgreSQL. Pour en savoir plus, consultez TOKENIZE_JSON et TOKENIZE_JSONB.
Requêtes JSON et JSONB
Vous pouvez utiliser un indice de recherche pour accélérer les requêtes qui incluent des conditions de contenant JSON et d'existence de clé. La structuration JSON détermine si un document JSON est contenu dans un autre. L'existence de la clé détermine si une clé existe dans le schéma de la base de données.
Dans GoogleSQL:
Exprimez la structuration JSON dans votre schéma à l'aide de la fonction JSON_CONTAINS.
Créez des conditions d'existence de clé à l'aide des opérateurs d'accès au champ, des opérateurs de sous-scripteur de tableau et de IS NOT NULL. Les opérateurs d'accès aux champs et d'indice de tableau décrivent un chemin d'accès au document JSON. IS NOT NULL vérifie l'existence de ce chemin d'accès (par exemple, doc.sub.path[@index].key IS NOT NULL).
Dans PostgreSQL:
Exprimer la structuration JSONB à l'aide des opérateurs @> et <@ Pour en savoir plus, consultez la section Opérateurs JSONB.
Créez des conditions d'existence de clé à l'aide des opérateurs ?, ?| et ?&.
Pour en savoir plus, consultez la section Opérateurs JSONB.
Dans vos requêtes, vous pouvez inclure plusieurs conditions JSON de n'importe quel type dans l'index de recherche. Vous pouvez également inclure les conditions JSON dans une combinaison logique à l'aide de AND, OR et NOT.
Vérifier l'utilisation de l'index de recherche
Pour vérifier que votre requête utilise un index de recherche, recherchez un nœud Analyse de l'index de recherche dans le plan d'exécution de la requête.
Restrictions
Les index de recherche, y compris les index de recherche JSON et JSONB, ne sont utilisés que dans les transactions en lecture seule. Spanner peut utiliser des index secondaires pertinents dans une transaction en lecture/écriture. Si vous essayez de forcer l'utilisation d'un indice de recherche dans une transaction en lecture-écriture, l'erreur suivante se produit :
ERROR: spanner: code = "InvalidArgument", desc = "The search index
AlbumsIndex cannot be used in transactional queries by default."
Les tentatives de stockage de certains documents JSON volumineux ou très complexes dans un indice de recherche peuvent renvoyer une erreur too many search token bytes. La taille du jeton de sortie de ce document JSON doit être inférieure à 10 Mo. Si vous n'avez pas besoin que l'intégralité du document soit accessible par recherche, envisagez d'extraire un sous-ensemble plus petit du document (par exemple, à l'aide d'une colonne générée) et de rechercher dans la colonne à la place.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/05 (UTC)."],[],[],null,["# JSON search indexes\n\n| **Note:** This feature is available with the Spanner Enterprise edition and Enterprise Plus edition. For more information, see the [Spanner editions overview](/spanner/docs/editions-overview).\n\n\u003cbr /\u003e\n\nIn addition to indexing text, the Spanner [search index](/spanner/docs/full-text-search/search-indexes)\nprovides an efficient way to index and query JSON and JSONB documents. Use\nsearch indexes for standalone JSON and JSONB queries, or to augment other\n[full-text search](/spanner/docs/full-text-search) queries.\n\nFor more information, see [Index JSON data](/spanner/docs/working-with-json#index) and\n[Index JSONB data](/spanner/docs/working-with-jsonb#index).\n\nTokenize JSON and JSONB\n-----------------------\n\nYou can use the `TOKENIZE_JSON` function to create a JSON index in\nGoogleSQL, or the `TOKENIZE_JSONB` function to create a JSONB index in\nPostgreSQL. For details, see [`TOKENIZE_JSON`](/spanner/docs/reference/standard-sql/search_functions#tokenize_json)\nand [`TOKENIZE_JSONB`](/spanner/docs/reference/postgresql/functions-and-operators#indexing).\n\nJSON and JSONB queries\n----------------------\n\nYou can use a search index to accelerate queries that include\n*JSON containment* and *key existence* conditions. JSON containment determines\nif one JSON document is contained within another. Key existence determines if a\nkey exists in the database schema.\n\n- In GoogleSQL:\n\n - Express JSON containment in your schema by using the [`JSON_CONTAINS`](/spanner/docs/reference/standard-sql/json_functions#json_contains) function.\n - Construct key existence conditions using the field access, array subscript operators, and `IS NOT NULL`. The field access and array subscript operators describe a JSON document path. `IS NOT NULL` checks for the existence of this path (for example, `doc.sub.path[@index].key IS NOT NULL`).\n- In PostgreSQL:\n\n - Express JSONB containment using the `@\u003e` and `\u003c@` operators. For more information, see [JSONB operators](/spanner/docs/reference/postgresql/functions-and-operators#jsonb_operators).\n - Construct key existence conditions using the `?`, `?|`, and `?&` operators. For more information, see [JSONB operators](/spanner/docs/reference/postgresql/functions-and-operators#jsonb_operators).\n\nIn your queries, you can include multiple JSON conditions of any type in the\nsearch index. You can also include the JSON conditions in a logical combination\nusing `AND`, `OR`, and `NOT`.\n\n### Check search index usage\n\nTo check that that your query uses a search index, look for a\n*Search index scan* node in the [query execution plan](/spanner/docs/query-execution-plans).\n\nRestrictions\n------------\n\n- Search indexes, including JSON and JSONB search indexes, are used only in read-only transactions. Spanner might use relevant secondary indexes in a read-write transaction. If you attempt to force the use of a search index in a read-write transaction, the following error occurs: `ERROR: spanner: code = \"InvalidArgument\", desc = \"The search index\n AlbumsIndex cannot be used in transactional queries by default.\"`\n- Attempts to store certain large or very complex JSON documents in a search index might return a `too many search token bytes` error. The output token size from this JSON document must be smaller than 10 MB. If you don't need the entire document to be searchable, consider extracting a smaller subset of the document (for example, by using a generated column) and searching over the column instead.\n\nWhat's next\n-----------\n\n- Learn about [tokenization and tokenizers](/spanner/docs/full-text-search/tokenization).\n- Learn about [search indexes](/spanner/docs/full-text-search/search-indexes).\n- Learn about [indexing JSON data](/spanner/docs/working-with-json#index).\n- Learn about [indexing JSONB data](/spanner/docs/working-with-jsonb#index)."]]