When you perform a wildcard query, you can use the asterisk (*) and question mark (?) wildcard characters in the query to search for data. The asterisk (*) matches a string of any length at, before, or after a search term. The question mark (?) matches a single character in a specific position. The string can start with an asterisk (*) or a question mark (?). For example, if you search for the "table*e" string, "tablestore" can be matched.
The *word*
string is equivalent to the WHERE field_a LIKE '%word%'
clause in SQL. If you want to search for the *word* string, you can perform a fuzzy query that provides higher performance than a wildcard query. For more information about how to perform a fuzzy query, see Fuzzy query. If you perform a fuzzy query, the query performance is not compromised when the data volume increases.
Prerequisites
- The TableStoreClient is initialized. For more information, see Initialization.
- 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.
Parameters
Parameter | Description |
---|---|
TableName | The name of the data table. |
IndexName | The name of the search index. |
query | The type of the query. Set the query type to WildcardQuery. |
FieldName | The name of the column. |
Value | The string that contains wildcard characters. The string cannot exceed 32 characters in length. |
ColumnsToGet | Specifies whether to return all columns of each matched row. By default, the value of ReturnAll is false, which specifies that not all columns are returned. If ReturnAll is set to false, you can use ColumnsToGet to specify the columns that you want to return. If you do not specify the columns that you want to return, only the primary key columns are returned. If you set the ReturnAll parameter to true, all columns are returned. |
Examples
/**
* Search the table for rows in which the value of the Col_Keyword column matches "hang*u".
*/
func WildcardQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(tableName)
searchRequest.SetIndexName(indexName)
query := &search.WildcardQuery{} // Set the query type to WildcardQuery.
query.FieldName = "Col_Keyword"
query.Value = "hang*u"
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(query)
searchRequest.SetSearchQuery(searchQuery)
// Specify that all columns are returned.
searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
ReturnAll:true,
})
searchResponse, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("%#v", err)
return
}
fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all matched rows are returned.
fmt.Println("TotalCount: ", searchResponse.TotalCount) // Specify that the total number of matched rows instead of the number of returned rows is displayed.
fmt.Println("RowCount: ", len(searchResponse.Rows))
for _, row := range searchResponse.Rows {
jsonBody, err := json.Marshal(row)
if err != nil {
panic(err)
}
fmt.Println("Row: ", string(jsonBody))
}
}