You can use the collapse (distinct) feature to collapse the result set based on a specific column when the results of a query contain large amounts of data of a specific type. Data of the specific type is displayed only once in the returned results to ensure the diversity of the result types.
Prerequisites
- An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
- A data table is created. Data is written to the table.
- A search index is created for the data table. For more information, see Create search indexes.
Usage notes
If you use the collapse feature, you can perform paging only by specifying the Offset and Limit parameters instead of tokens.
If you aggregate and collapse a result set at the same time, the result set is aggregated before it is collapsed.
If you collapse the query results, the total number of results to return is determined by the sum of the Offset and Limit values. A maximum of 50,000 results can be returned.
The total number of rows in the response indicates the number of rows that meet the query conditions before you use the collapse (distinct) feature. After the result set is collapsed, the total number of rows cannot be queried.
Parameters
Parameter | Description |
TableName | The name of the data table. |
IndexName | The name of the search index. |
Query | The type of the query. You can set this parameter to any query type. |
Collapse | The collapse parameter, including the FieldName parameter. FieldName: the name of the column based on which the result set is collapsed. Only columns whose values are of the INTEGER, FLOATING-POINT and KEYWORD data types are supported. |
Offset | The position from which the current query starts. |
Limit | The maximum number of rows that you want the current query to return. To query only the number of rows that meet the query conditions without specific data, set the Limit parameter to 0. |
Examples
The following sample code provides an example on how to query all data in a table and collapse the result set based on the pk0 column:
/// <summary>
/// Collapse the result set based on the pk0 column.
/// </summary>
/// <param name="otsClient"></param>
public static void UseCollapse(OTSClient otsClient)
{
MatchAllQuery matchAllQuery = new MatchAllQuery();
Collapse collapse = new Collapse();
collapse.FieldName = "pk0";
SearchQuery searchQuery = new SearchQuery();
searchQuery.Query = matchAllQuery;
searchQuery.Collapse = collapse;
SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);
SearchResponse searchResponse = otsClient.Search(searchRequest);
foreach (Row row in searchResponse.Rows)
{
Console.WriteLine(JsonConvert.SerializeObject(row));
}
}
FAQ
References
When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, and exists query. You can use different query methods to query data from multiple dimensions based on your business requirements.
You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.
You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).
If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.
If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.