Use a wildcard query in the Tablestore SDK for Go to match values or tokens by using asterisks (*) and question marks (?).
Prerequisites
Install the Tablestore Go SDK and initialize a client.
Description
A wildcard query uses an asterisk (*) to match zero or more characters and a question mark (?) to match one character. For a Text field, the query applies to tokenized terms. Matching is case-sensitive, and the query string can contain up to 32 characters.
func (client *tablestore.TableStoreClient) Search(request *tablestore.SearchRequest) (*tablestore.SearchResponse, error)
The following example queries data and returns up to 10 rows and the total number of matched rows.
tableName := "example_table"
indexName := "example_index"
query := &search.WildcardQuery{FieldName: "category", Value: "book-*"}
searchQuery := search.NewSearchQuery().
SetQuery(query).
SetLimit(10).
SetGetTotalCount(true)
response, err := client.Search(&tablestore.SearchRequest{
TableName: tableName,
IndexName: indexName,
SearchQuery: searchQuery,
ColumnsToGet: &tablestore.ColumnsToGet{
ReturnAllFromIndex: true,
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.TotalCount)
fmt.Println(response.Rows)
Parameters
Query request
request is of the tablestore.SearchRequest type and contains the following parameters.
|
Name |
Type |
Description |
|
TableName (required) |
string |
The name of the data table. |
|
IndexName (required) |
string |
The name of the search index. |
|
SearchQuery (required) |
search.SearchQuery |
The query condition and common query configurations. |
|
ColumnsToGet (optional) |
*tablestore.ColumnsToGet |
The return-column configuration. If this parameter is omitted, only primary key columns are returned. |
|
RoutingValues (optional) |
[]*tablestore.PrimaryKey |
Primary key values for custom routing fields. Omit this parameter if custom routing is not configured. |
|
TimeoutMs (optional) |
*int32 |
The request timeout period in milliseconds. |
Query configuration
Create a query configuration by calling search.NewSearchQuery() and configure it by using the following methods.
|
Name |
Type |
Description |
|
SetQuery (required) |
search.Query |
Specifies the query condition. |
|
SetOffset (optional) |
int32 |
Specifies the start position. Default value: 0. For offset-based pagination, Offset + Limit cannot exceed 100,000. |
|
SetLimit (optional) |
int32 |
Specifies the maximum number of rows to return. Default value: 10. Maximum value: 100. A value of 0 returns no rows. |
|
SetHighlight (optional) |
*search.Highlight |
Configures summaries and highlighting for Text fields. For more information, see Summary and highlighting. |
|
SetCollapse (optional) |
*search.Collapse |
Collapses query results. For more information, see Collapse query results. |
|
SetSort (optional) |
*search.Sort |
Specifies the result sort order. For more information, see Sort and paginate results. |
|
SetGetTotalCount (optional) |
bool |
Specifies whether to count all matched rows. Default value: false. |
|
SetToken (optional) |
[]byte |
Specifies the NextToken value returned by the previous response. This method clears Sort because the token contains the previous-page sort conditions. Do not specify Offset when you use token-based pagination. |
|
SetSearchFilter (optional) |
*search.SearchFilter |
Applies a post-query filter. For more information, see Use post-query filters. |
|
Aggregation (optional) |
...search.Aggregation |
Configures aggregations. For more information, see Aggregation. |
|
GroupBy (optional) |
...search.GroupBy |
Configures grouping. For more information, see Aggregation. |
Query condition
The query is of the search.WildcardQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
FieldName (required) |
string |
The name of the index field to query. |
|
Value (required) |
string |
The wildcard query string, which can contain up to 32 characters. |
Columns to return
request.ColumnsToGet is of the tablestore.ColumnsToGet type and contains the following parameters.
|
Name |
Type |
Description |
|
Columns (optional) |
[]string |
The attribute columns to return. This parameter takes effect only when ReturnAll and ReturnAllFromIndex are both false. |
|
ReturnAll (optional) |
bool |
Specifies whether to return all attribute columns in the data table. Default value: false. |
|
ReturnAllFromIndex (optional) |
bool |
Specifies whether to return all indexed attribute columns. Default value: false. Do not set this parameter and ReturnAll to true at the same time. |
Response
The Search method returns a tablestore.SearchResponse value. The following table describes the core business fields.
|
Name |
Type |
Description |
|
TotalCount |
int64 |
The total number of matched rows. The value depends on SetGetTotalCount. |
|
Rows |
[]*tablestore.Row |
The rows returned by the current query. The number does not exceed the value specified by SetLimit. |
|
SearchHits |
[]*tablestore.SearchHit |
The search hits. Read this field when you use highlighting, nested inner hits, or relevance scores. |
|
NextToken |
[]byte |
The token for the next page. If the value is not empty, pass it to the next query. |
|
IsAllSuccess |
bool |
Indicates whether all index partitions were queried. If the value is false, partial results are returned and TotalCount may be less than the actual number of matched rows. |
|
AggregationResults |
search.AggregationResults |
The aggregation results. |
|
GroupByResults |
search.GroupByResults |
The grouping results. |
Examples
Improve substring query performance
For a query pattern such as *word*, configure the Text field with the fuzzy analyzer when you create the search index, and use a match phrase query to query the field. This approach typically performs better than a wildcard query for matching a substring. For matching rules and limits, see Token-based wildcard queries.
The following example creates a search index that contains the file_name field and configures the fuzzy analyzer for the field.
analyzer := tablestore.Analyzer_Fuzzy
fieldSchema := &tablestore.FieldSchema{
FieldName: proto.String("file_name"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
Analyzer: &analyzer,
AnalyzerParameter: tablestore.FuzzyAnalyzerParameter{},
}
request := &tablestore.CreateSearchIndexRequest{
TableName: "example_table",
IndexName: "example_index",
IndexSchema: &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{fieldSchema},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
log.Fatal(err)
}
After the index data is synchronized, use a MatchPhraseQuery to query rows in which the file_name field contains word at any position.
query := &search.MatchPhraseQuery{
FieldName: "file_name",
Text: "word",
}
searchQuery := search.NewSearchQuery().
SetQuery(query).
SetLimit(10)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
ColumnsToGet: &tablestore.ColumnsToGet{
ReturnAllFromIndex: true,
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Rows)
Exclude rows that match a wildcard pattern
To implement the equivalent of SQL NOT LIKE, add a WildcardQuery to BoolQuery.MustNotQueries.
wildcardQuery := &search.WildcardQuery{
FieldName: "category",
Value: "book-*",
}
query := &search.BoolQuery{
MustNotQueries: []search.Query{wildcardQuery},
}
searchQuery := search.NewSearchQuery().
SetQuery(query).
SetLimit(10)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
ColumnsToGet: &tablestore.ColumnsToGet{
ReturnAllFromIndex: true,
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Rows)