精确查询

更新时间:
复制为 MD 格式

精确查询(TermQuery)采用完整精确匹配的方式查询表中的数据,类似于字符串匹配。

前提条件

参数

参数

说明

FieldName

要匹配的字段名称。

Term

查询关键词,即要匹配的值。该词不会被分词,直接与字段值进行精确匹配。

对于 Text 类型字段,Tablestore 会对字段值进行分词,只要分词后存在与关键词完全一致的词条即可命中。例如,字段值为"tablestore is cool",分词后得到"tablestore"、"is"、"cool"三个词条,查询这三个词条中的任意一个均满足条件。

GetTotalCount

是否返回匹配的总行数。默认值为 false,表示不返回总行数。设置为 true 会影响查询性能。

Query

查询类型,设置为 TermQuery。

TableName

数据表名称。

IndexName

多元索引名称。

ColumnsToGet

返回列配置,包含 ReturnAll、Columns 和 ReturnAllFromIndex 三个子参数。

ReturnAll默认为false,表示不返回所有列。此时可以通过如下任一设置返回所需列。如果未设置ColumnsReturnAllFromIndex,则只返回主键列。

  • 设置Columns指定返回的列。

  • 设置ReturnAllFromIndextrue返回多元索引中的所有列。

当设置ReturnAlltrue时,表示返回所有列。

示例

以下示例查询 Keyword_type_col 列精确匹配"SearchIndex"的行数据。

/// <summary>
/// 查询 Keyword_type_col 列精确匹配"SearchIndex"的行数据。
/// </summary>
/// <param name="otsClient"></param>
public static void TermQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // 返回匹配的总行数。
    searchQuery.GetTotalCount = true;
    // 查询 Keyword_type_col 列值为"SearchIndex"的行。
    searchQuery.Query = new TermQuery("Keyword_type_col", new ColumnValue("SearchIndex"));

    var request = new SearchRequest(TableName, IndexName, searchQuery);
    // 配置返回列:ReturnAllFromIndex 返回多元索引中的所有列。
    // 返回指定列:Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
    // 返回数据表所有列:ReturnAll = true
    request.ColumnsToGet = new ColumnsToGet()
    {
        ReturnAllFromIndex = true
    };

    var response = otsClient.Search(request);
    // 可通过 NextToken 实现翻页查询。
}

常见问题

相关文档