Post-query filter (Filter) applies an additional filtering pass to search index query results, allowing you to override the internal query optimizer and force specific conditions to execute in the final stage. When used properly, this feature can significantly improve query performance.
Before you use this feature, contact Tablestore technical support to enable it for your account.
Feature architecture
The post-query filter uses a multilayer query architecture with the following components:
-
SearchRequest: The top-level request container. Specifies the table name, index name, and query configuration.
-
SearchQuery: The core query configuration. Contains the primary query condition (Query) and an optional post-query filter (SearchFilter).
-
Query: The primary query condition. Supports all search index query types and data types for initial data retrieval.
-
SearchFilter: The post-query filter. Applies additional filter conditions for fine-grained refinement of the primary query results.
Limitations
Post-query filter must be used in combination with a search index query. Supported query types include TermQuery, TermsQuery, RangeQuery, ExistsQuery, and BoolQuery.
In a BoolQuery, only the
mustQueries,mustNotQueries, andshouldQueriesclauses are supported. ThefilterQueriesclause is not supported.Only Keyword, Long, and Double fields with the
enableSortAndAggproperty enabled support filtering.Weight settings are not supported in post-query filters.
Prerequisites
Before you begin, ensure that you have completed the following tasks:
-
Create a search index for the data table.
API operation
func (tableStoreClient *TableStoreClient) Search(request *SearchRequest) (*SearchResponse, error)
Example
The following example performs an exact match on the col_keyword field with the value "value" as the primary query, and applies a RangeQuery filter to select records where the col_long field value falls between 1 and 10.
func queryFilterExample(client *tablestore.TableStoreClient) {
// [Required] Replace with your table name.
tableName := "<TABLE_NAME>"
// [Required] Replace with your search index name.
indexName := "<SEARCH_INDEX_NAME>"
// Create the main query: a TermsQuery for an exact match.
termsQuery := &search.TermsQuery{
FieldName: "col_keyword",
Terms: []interface{}{"value"},
}
// Create the filter condition: a RangeQuery where the value of the col_long field is in the range (1, 10).
rangeQuery := &search.RangeQuery{
FieldName: "col_long",
From: int64(1), // Excludes 1.
To: int64(10), // Excludes 10.
// To include the borders, explicitly set IncludeLower/IncludeUpper. The default value is true in the Go SDK.
// The setFrom/setTo methods in the Java SDK exclude borders by default, and the Go SDK behavior is consistent.
}
// Assemble the filter.
searchFilter := &search.SearchFilter{
Query: rangeQuery,
}
// Combine them into a complete SearchQuery.
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(termsQuery)
searchQuery.SetSearchFilter(searchFilter)
// Create the request.
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(tableName)
searchRequest.SetIndexName(indexName)
searchRequest.SetSearchQuery(searchQuery)
// Set the columns to return: return all columns from the search index.
columnsToGet := &tablestore.ColumnsToGet{
ReturnAllFromIndex: true,
}
searchRequest.SetColumnsToGet(columnsToGet)
resp, err := client.Search(searchRequest)
if err != nil {
log.Printf("Search failed: %v", err)
return
}
for _, row := range resp.Rows {
fmt.Printf("Row: %+v\n", row.PrimaryKey)
}
}
-
Configure the query to return specific columns or all columns.
columnsToGet := &tablestore.ColumnsToGet{ // Specify columns. Columns: []string{"col_long", "col_keyword"}, // Or, return all columns. //ReturnAll: true, } searchRequest.SetColumnsToGet(columnsToGet) -
To retrieve the total number of matched rows, enable the
totalCountfeature and read the count from the response.// Enable totalCount in searchQuery. searchQuery.SetGetTotalCount(true) // Print the total number of matched rows from the response. fmt.Printf("Total Count (matched): %d\n", resp.TotalCount)