Todos os produtos
Search
Central de documentação

Tablestore:Consulta booleana

Última atualização: Jul 03, 2026

Uma consulta booleana (BoolQuery) contém uma ou mais subconsultas e as utiliza para determinar se uma linha de dados atende às condições de consulta especificadas. Cada subconsulta pode ser de qualquer tipo, incluindo outra BoolQuery.

Pré-requisitos

Parâmetros

Parâmetro

Descrição

tableName

Nome da tabela de dados.

indexName

Nome do índice de pesquisa.

mustQueries

Lista de subconsultas que os resultados da consulta devem corresponder. Este parâmetro equivale ao operador AND.

mustNotQueries

Lista de subconsultas que os resultados da consulta não devem corresponder. Este parâmetro equivale ao operador NOT.

filterQueries

Lista de subconsultas. Apenas as linhas que correspondem a todos os subfiltros são retornadas. Um filtro é semelhante a uma consulta, exceto pelo fato de não calcular a pontuação de relevância com base no número de subfiltros correspondidos pela linha.

shouldQueries

Lista de subconsultas que os resultados podem ou não corresponder. Este parâmetro equivale ao operador OR.

Somente as linhas que atendem ao número mínimo de condições de subconsulta especificadas por shouldQueries são retornadas.

Uma pontuação de relevância geral mais alta indica que mais condições de subconsulta especificadas por shouldQueries foram atendidas.

minimumShouldMatch

Número mínimo de condições de subconsulta especificadas por shouldQueries que as linhas devem atender. Se nenhuma outra condição de subconsulta for especificada além daquelas definidas por shouldQueries, o valor padrão do parâmetro minimumShouldMatch será 1. Caso outras condições de subconsulta sejam definidas, como as especificadas por mustQueries, mustNotQueries e filterQueries, o valor padrão do parâmetro minimumShouldMatch será 0.

Exemplos

O código a seguir fornece um exemplo de como construir uma consulta booleana para consultar linhas com base em uma combinação de subconsultas:

var client = require('../client');
var TableStore = require('../../index.js');
var Long = TableStore.Long;
/**
 * Perform a Boolean query to implement the following operations: (col2 < 4 or col 3 < 5) or (col2 = 4 and (col3 = 5 or col3 = 6)). The following logic is used:
 * boolQuery1 = rangeQuery(col2<4) or rangeQuery(col3<5)
 * boolQuery2 = termQuery(col3=5) or (col3=6)
 * boolQuery3 = termQuery(col2=4) and boolquery2
 * boolQuery4 = boolQuery1 or boolQuery3
 */
client.search({
    tableName: "sampleTable",
    indexName: "sampleSearchIndex",
    searchQuery: {
        offset: 0, // Query the offset value. 
        limit: 10, // To query only the number of rows that meet the query conditions without returning specific data, you can set limit to 0. This way, Tablestore returns the number of rows that meet the query conditions without specific data from the table. 
        getTotalCount: false, // Specify whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which indicates that the total number of rows that meet the query conditions is not returned. 
        query: { // Construct boolQuery4. Specify the query condition to meet at least one of boolQuery1 and boolQuery3. 
            queryType: TableStore.QueryType.BOOL_QUERY,
            query: {
                shouldQueries: [ // Specify mustQueries, shouldQueries, or mustNotQueries. 
                    { // Construct boolQuery1. Specify the query condition to meet at least one of Query Condition 1 and Query Condition 2. 
                        queryType: TableStore.QueryType.BOOL_QUERY,
                        query: {
                            // Specify shouldQueries to query the rows that contain the col2 column whose value is smaller than 4 or contain the col3 column whose value is smaller than 5. 
                            shouldQueries:[
                                {
                                    // Query Condition 1: Perform a range query to query the rows that contain the col2 column whose value is smaller than 4. 
                                    queryType: TableStore.QueryType.RANGE_QUERY,
                                    query:{
                                        fieldName: "col2",
                                        rangeTo: 4
                                    }
                                },
                                {
                                    // Query Condition 2: Perform a range query to query the rows that contain the col3 column whose value is smaller than 5.            
                                    queryType: TableStore.QueryType.RANGE_QUERY,
                                    query:{
                                        fieldName: "col3",
                                        rangeTo: 5
                                    }
                                }
                            ],
                            minimumShouldMatch:1

                        }
                    },
                    { // Construct boolQuery3. Specify the query condition to meet the conditions of Query Condition 3 and boolQuery2. 
                        queryType: TableStore.QueryType.BOOL_QUERY,
                        query: {
                            mustQueries: [
                                // Specify mustQueries to query the rows that contain the col2 column whose value is equal to 4 and contain the col3 column whose value is equal to 5 or 6. 
                                {
                                    // Query Condition 3: Perform a term query to query the rows that contain the col2 column whose value is equal to 4. 
                                    queryType:TableStore.QueryType.TERM_QUERY,
                                    query: {
                                        fieldName : "col2",
                                        term: 4
                                    }
                                },
                                { // Construct boolQuery2: Specify the query condition to meet at least one of Query Condition 4 and Query Condition 5. 
                                    queryType: TableStore.QueryType.BOOL_QUERY,
                                    query: {
                                        // Specify shouldQueries to query the rows that contain the col3 column whose value is equal to 5 or 6. 
                                        shouldQueries:[
                                            {
                                                // Query Condition 4: Perform a term query to query the rows that contain the col3 column whose value is equal to 5. 
                                                queryType: TableStore.QueryType.TERM_QUERY,
                                                query:{
                                                    fieldName:"col3",
                                                    term: 5
                                                }
                                            },
                                            {
                                                // Query Condition 5: Perform a term query to query the rows that contain the col3 column whose value is equal to 6.          
                                                queryType: TableStore.QueryType.TERM_QUERY,
                                                query:{
                                                    fieldName:"col3",
                                                    term: 6
                                                }
                                            }
                                        ],
                                        minimumShouldMatch:1
                                    }
                                }
                            ]
                        }
                    }
                ],
                minimumShouldMatch: 1 // Specify the minimum number of conditions that must be met. This parameter is valid when subquery conditions are specified only by shouldQueries. 
            }
        },
    },
    columnToGet: { // Specify the columns that you want to return. You can configure the RETURN_SPECIFIED parameter to return specified columns, the RETURN_ALL parameter to return all columns, the RETURN_ALL_FROM_INDEX parameter to return all columns in the search index, or the RETURN_NONE parameter to return only the primary key columns.        
        returnType: TableStore.ColumnReturnType.RETURN_SPECIFIED,
        returnNames: ["col2", "col3", "col4"]
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:', JSON.stringify(data, null, 2));
});

Perguntas frequentes

Referências