Todos os produtos
Search
Central de documentação

Tablestore:Prefix query

Última atualização: Jul 03, 2026

Use a consulta de prefixo para buscar dados correspondentes a um prefixo especificado. Se o campo usado na condição de prefixo for do tipo TEXT, os valores serão tokenizados. Uma linha atenderá aos critérios da consulta quando pelo menos um token contiver o prefixo definido.

Pré-requisitos

Parâmetros

Parâmetro

Descrição

TableName

Nome da tabela de dados.

IndexName

Nome do índice de pesquisa.

Query

Tipo de consulta. Defina este parâmetro como PrefixQuery.

FieldName

Nome do campo para correspondência.

Prefix

Prefixo para correspondência.

Se o campo alvo for do tipo TEXT, os valores serão tokenizados antes da correspondência. Uma linha atenderá às condições da consulta quando pelo menos um token começar com o prefixo especificado.

GetTotalCount

Indica se o sistema deve retornar o número total de linhas correspondentes às condições da consulta. Valor padrão: false.

Definir este valor como true reduz o desempenho da consulta.

ColumnsToGet

Controla as colunas retornadas. Configure por meio dos subparâmetros ReturnAll e Columns.

Por padrão, ReturnAll é false e apenas as colunas de chave primária são retornadas. Para retornar colunas específicas fora da chave primária, defina ReturnAll como false e liste as colunas em Columns.

Para retornar todas as colunas, defina ReturnAll como true.

Código de exemplo

O exemplo abaixo consulta linhas nas quais o valor da coluna Col_Keyword começa com hangzhou:

/**
 * Query the rows in which the value of the Col_Keyword column contains the "hangzhou" prefix in a table. 
 */
func PrefixQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.PrefixQuery{} // Set the query type to PrefixQuery. 
    query.FieldName = "Col_Keyword" // Specify the name of the field that you want to match. 
    query.Prefix = "hangzhou" // Specify the prefix that is used to match the value of the column. 
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query)
    searchQuery.SetGetTotalCount(true)
    searchRequest.SetSearchQuery(searchQuery)
    // Return all columns in the rows that meet the query conditions. 
    searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
        ReturnAll:true,
    })
    searchResponse, err := client.Search(searchRequest)
    if err != nil {
        fmt.Printf("%#v", err)
        return
    }
    fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all rows that meet the query conditions are returned. 
    fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows. 
    fmt.Println("RowCount: ", len(searchResponse.Rows))
    for _, row := range searchResponse.Rows {
        jsonBody, err := json.Marshal(row)
        if err != nil {
            panic(err)
        }
        fmt.Println("Row: ", string(jsonBody))
    }
}

Perguntas frequentes

Referências