Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Oltre a indicizzare il testo, l'indice di ricerca di Spanner offre un modo efficiente per indicizzare e eseguire query sui documenti JSON e JSONB. Utilizza gli indici di ricerca per query JSON e JSONB autonome o per integrare altre query di ricerca full-text.
Puoi utilizzare la funzione TOKENIZE_JSON per creare un indice JSON in
GoogleSQL o la funzione TOKENIZE_JSONB per creare un indice JSONB in
PostgreSQL. Per maggiori dettagli, vedi TOKENIZE_JSON
e TOKENIZE_JSONB.
Query JSON e JSONB
Puoi utilizzare un indice di ricerca per accelerare le query che includono condizioni di contenimento JSON ed esistenza di chiavi. Il contenimento JSON determina se un documento JSON è contenuto in un altro. L'esistenza della chiave determina se una chiave esiste nello schema del database.
In GoogleSQL:
Esprimere il contenimento JSON nello schema utilizzando la funzione JSON_CONTAINS.
Costruisci condizioni di esistenza della chiave utilizzando l'accesso ai campi, gli operatori di indice di array e IS NOT NULL. Gli operatori di accesso ai campi e di indice dell'array descrive
un percorso del documento JSON. IS NOT NULL verifica l'esistenza di questo percorso (ad esempio doc.sub.path[@index].key IS NOT NULL).
In PostgreSQL:
Esprimere il contenimento JSONB utilizzando gli operatori @> e <@. Per ulteriori informazioni, consulta gli operatori JSONB.
Costruisci condizioni di esistenza della chiave utilizzando gli operatori ?, ?| e ?&.
Per ulteriori informazioni, consulta gli operatori JSONB.
Nelle query, puoi includere più condizioni JSON di qualsiasi tipo nell'indice di ricerca. Puoi anche includere le condizioni JSON in una combinazione logica utilizzando AND, OR e NOT.
Controllare l'utilizzo dell'indice della Ricerca
Per verificare che la query utilizzi un indice di ricerca, cerca un Scansione dell'indice di ricerca nel piano di esecuzione della query.
Limitazioni
Gli indici di ricerca, inclusi gli indici di ricerca JSON e JSONB, vengono utilizzati solo nelle transazioni di sola lettura. Spanner potrebbe utilizzare indici secondari pertinenti in una transazione di lettura-scrittura. Se tenti di forzare l'utilizzo di un indice di ricerca in una transazione di lettura/scrittura, si verifica il seguente errore:ERROR: spanner: code = "InvalidArgument", desc = "The search index
AlbumsIndex cannot be used in transactional queries by default."
I tentativi di archiviare determinati documenti JSON di grandi dimensioni o molto complessi in un indice di ricerca potrebbero restituire un errore too many search token bytes. Le dimensioni del token di output di questo documento JSON devono essere inferiori a 10 MB. Se non hai bisogno che l'intero documento sia disponibile per la ricerca, valuta la possibilità di estrarre un sottoinsieme più piccolo del documento (ad esempio utilizzando una colonna generata) e di eseguire la ricerca all'interno della colonna.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)."]]