Tablestore filters are used to filter query results on the server side. The server returns only the rows or columns that match the filtering conditions. Filters can be used for GetRow, BatchGetRow, and GetRange operations.

Filter types

The following filters can be used to filter rows based on the reference column values.
  • SingleColumnValueFilter: determines whether to filter a row based only on the values of a reference column.
  • CompositeColumnValueFilter: determines whether to filter a row by combining the values of multiple reference columns.
Note You must include the reference columns in the query results. If the columns to be queried do not include any reference columns, the filters do not obtain any values. When a specified reference column does not exist, SingleColumnValueFilter uses the configured FilterIfMissing parameter to determine whether the filtering conditions are met and what operation to perform if a reference column does not exist.

Examples

  • Construct SingleColumnValueFilter
    func GetRowWithFilter(client *tablestore.TableStoreClient, tableName string) {
        fmt.Println("begin to get row")
        pk := new(tablestore.PrimaryKey)
        pk.AddPrimaryKeyColumn("pk1", "pk1value1")
        pk.AddPrimaryKeyColumn("pk2", int64(2))
        pk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
    
        // Configure the filter. Specify to return rows in which the value of c1 is Zhejiang. Set FilterIfMissing to true.
        condition := tablestore.NewSingleColumnCondition("c1", tablestore.ComparatorType(tablestore.CT_EQUAL), "Zhejiang")
        condition.FilterIfMissing = true
    
        criteria := &tablestore.SingleRowQueryCriteria{
            TableName:     tableName,
            PrimaryKey:    pk,
            MaxVersion:    1,
            Filter:        condition,
        }
    
        getResp, err := client.GetRow(&tablestore.GetRowRequest{SingleRowQueryCriteria: criteria})
        if err ! = nil {
            fmt.Println("getrow failed with error:", err)
        } else {
            colMap := getResp.GetColumnMap()
            fmt.Println("length is ", len(colMap.Columns))
            fmt.Println("get row col0 result is ", getResp.Columns[0].ColumnName, getResp.Columns[0].Value)
        }
    }
  • Construct CompositeColumnValueFilter
    func GetRowWithCompositeColumnValueFilter(client *tablestore.TableStoreClient, tableName string) {
        fmt.Println("begin to get row")
        pk := new(tablestore.PrimaryKey)
        pk.AddPrimaryKeyColumn("pk1", "pk1value1")
        pk.AddPrimaryKeyColumn("pk2", int64(2))
        pk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
    
        // Configure then filter. Specify to return rows in which the value of c1 is Zhejiang and the value of c2 is Hangzhou.
        filter := tablestore.NewCompositeColumnCondition(tablestore.LO_AND)
        filter1 := tablestore.NewSingleColumnCondition("c1", tablestore.CT_EQUAL, "Zhejiang")
        filter2 := tablestore.NewSingleColumnCondition("c2", tablestore.CT_EQUAL, "Hangzhou")
        filter.AddFilter(filter2)
        filter.AddFilter(filter1)
    
        criteria := &tablestore.SingleRowQueryCriteria{
            TableName:  tableName,
            PrimaryKey: pk,
            MaxVersion: 1,
            Filter:     filter,
        }
    
        getResp, err := client.GetRow(&tablestore.GetRowRequest{SingleRowQueryCriteria: criteria})
        if err ! = nil {
            fmt.Println("getrow failed with error:", err)
        } else {
            colMap := getResp.GetColumnMap()
            fmt.Println("length is ", len(colMap.Columns))
            fmt.Println("get row col0 result is ", getResp.Columns[0].ColumnName, getResp.Columns[0].Value)
        }
    }