MatchAllQuery

MatchAllQuery用于匹配所有行,常用于查询表中数据总行数,或者查看表中任意几条数据。

/// <summary>
/// 查询所有行,返回行数
/// </summary>
/// <param name="otsClient"></param>
public static void MatchAllQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    searchQuery.Query = new MatchAllQuery();
    searchQuery.GetTotalCount = true; // 需要设置GetTotalCount = true 才会返回满足条件的数据总行数
    /*
    * MatchAllQuery 结果中的Totalcount可以表示数据的总行数(数据量很大时为估算值)
    * 如果只是为了查询TotalHit,可以设置limit=0,即不返回任意一行数据。
    */
    searchQuery.Limit = 0;
    var request = new SearchRequest(TableName, IndexName, searchQuery);

    var response = otsClient.Search(request);
    // 判断返回的结果是否是完整的,当isAllSuccess为false时,代表可能有部分节点查询失败,返回的是部分数据
    Console.WriteLine("IsAllSuccess:" + response.IsAllSuccess);
    Console.WriteLine("Total Count:" + response.TotalCount);
}

MatchQuery

MatchQuery采用近似匹配的方式查询表中的数据。比如查询的值为"this is", 可以匹配到“...,this is tablestore”、“is this tablestore”、“tablestore is cool”、“this"、“is”等。


/// <summary>
/// 模糊匹配和短语或邻近查询,返回指定列
/// </summary>
/// <param name="otsClient"></param>
public static void MatchQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    //设置查询类型为MatchQuery,要匹配的字段为Text_type_col,要匹配的值为"SearchIndex"
    searchQuery.Query = new MatchQuery(Text_type_col, "SearchIndex");
    searchQuery.GetTotalCount = true;
    var request = new SearchRequest(TableName, IndexName, searchQuery);
    request.ColumnsToGet = new ColumnsToGet()
    {
        Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
    };

    var response = otsClient.Search(request);

    Console.WriteLine("Total Count:" + response.TotalCount);
}

MatchPhraseQuery

使用MatchPhraseQuery进行短语匹配查询。短语匹配查询与匹配查询类似,但是要求查询的短语必须完整的按照顺序匹配。比如查询的值为“this is”,可以匹配到“...,this is tablestore”、"this is a table”,但是无法匹配到"this table is ..."以及"is this a table"。

/// <summary>
/// 类似MatchQuery(MatchQuery 仅匹配某个词即可),但是 MatchPhraseQuery会匹配所有的短语。
/// </summary>
/// <param name="otsClient"></param>
public static void MatchPhraseQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    //设置查询类型为
    searchQuery.Query = new MatchPhraseQuery(Text_type_col, "TableStore SearchIndex");
    searchQuery.GetTotalCount = true;
    var request = new SearchRequest(TableName, IndexName, searchQuery);
    request.ColumnsToGet = new ColumnsToGet()
    {
        Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
    };
    
    var response = otsClient.Search(request);

    Console.WriteLine("Total Count:" + response.TotalCount);
}