Todos os produtos
Search
Central de documentação

Tablestore:Consultas JSON

Última atualização: Jun 30, 2026

Os campos JSON aceitam os tipos Object e Nested. Para consultas do tipo Object, use diretamente o tipo de consulta desejado. Para consultas do tipo Nested, envolva as condições em um NestedQuery para garantir que múltiplas condições sejam avaliadas no mesmo elemento aninhado.

Pré-requisitos

Limites

Não é possível usar campos do tipo vetor em JSON.

Cenários de consulta

Considere uma tabela de dados com uma coluna id do tipo String e uma coluna address, também do tipo String. A coluna address armazena dados formatados em JSON.

Suponha que uma linha tenha a coluna address com o valor [{ "country": "China", "city": "hangzhou" }, { "country": "usa", "city": "Seattle" }]. Uma consulta por country="China" e city="Seattle" não retorna essa linha se a coluna address for do tipo Nested, mas a retorna se for do tipo Object.

Exemplos

Consultar dados JSON do tipo Nested

O exemplo a seguir consulta linhas em que um único elemento aninhado no campo address atende simultaneamente a duas condições: address.country é 'China' e address.city é 'Seattle'. Como o tipo Nested exige que todas as condições correspondam ao mesmo subelemento, a consulta deve usar NestedQuery para envolver o BoolQuery.

from tablestore import *

def nested_query(client):
    # Condition 1: The country in the address sub-row must be "China".
    term_query1 = TermQuery('address.country','China')

    # Condition 2: The city in the address sub-row must be "Seattle".
    term_query2 = TermQuery('address.city', 'Seattle')

    # Use BoolQuery with an AND condition to find sub-rows that meet both queries.
    bool_query = BoolQuery(
        must_queries=[term_query1, term_query2]
    )

    # Use BoolQuery inside NestedQuery to require a single sub-row to meet multiple conditions.
    nested_query = NestedQuery(
        path="address",  # The path to the nested column. This is the parent path of the field to query.
        query=bool_query,  # The inner query condition.
        score_mode=ScoreMode.NONE  # The scoring mode. Set to None.
    )

    # Build the search query.
    search_query = SearchQuery(
        query=nested_query
    )

    # Execute the search.
    resp = client.search(table_name='<TABLE_NAME>',index_name='<SEARCH_INDEX_NAME>',
                         search_query=search_query)
    print("Row:", resp.rows)
    

Consultar dados JSON do tipo Object

Este exemplo consulta linhas em que a coluna address satisfaz ambas as condições: address.country é 'China' e address.city é 'Seattle'. Como o tipo Object não exige que as condições coincidam no mesmo subelemento, use BoolQuery diretamente, sem NestedQuery.

from tablestore import *

def bool_query(client):
    # Condition 1: The country in the address sub-row must be "China".
    term_query1 = TermQuery('address.country','China')

    # Condition 2: The city in the address sub-row must be "Seattle".
    term_query2 = TermQuery('address.city','Seattle')

    # Use BoolQuery with an AND condition to find sub-rows that meet both queries.
    bool_query = BoolQuery(
        must_queries=[term_query1, term_query2]
    )

    # Build the search query.
    search_query = SearchQuery(
        query=bool_query
    )

    # Execute the search.
    resp = client.search(table_name='<TABLE_NAME>',index_name='<SEARCH_INDEX_NAME>',
                         search_query=search_query)
    print("Row:", resp.rows)