如何在Tarantool中通过二级索引进行选择?

我创建了一个具有两个索引的空间——主索引和次级索引:

box.schema.sequence.create('user_seq', { if_not_exists = true })
box.schema.space.create('user', {
    if_not_exists = true,
    format = {
        { name = 'id', type = 'unsigned'},
        { name = 'bio', type = 'string'}
    }
})
box.space.user:create_index('id', {
     sequence = 'user_seq',
     parts = {'id'}
})
box.space.user:create_index('bio', {
     parts = {'bio'},
     if_not_exists = true,
     unique = false
})

插入元组:

tarantool> box.space.user:insert({ box.sequence.user_seq:next(), '其他事情'})
---
- [1, '其他事情']
...

我尝试这样搜索:

box.space.user:select({'其他事情'})

并收到错误提示:

- error: '提供的第0部分的键类型与索引部分类型不匹配:期望的无符号'

我应该如何通过次级索引进行搜索?

点赞
用户10792439
用户10792439

文档中说:

index.index-name是可选的。如果省略它,则假定的索引是第一个(主键)索引。因此,对于上面的示例,box.space.tester:select({1},{iterator = 'GT'})将通过“主”索引返回相同的两行。

显式使用该次要索引:

tarantool> box.space.user.index.bio:select({'other stuff'})
---
- - [1,'other stuff']
...

了解更多信息请参阅文档。

2020-09-23 16:28:34