如何在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(),'other stuff'})
---
- [1,'other stuff']
...

我试图这样搜索

Box.space.user:select({'other stuff'})

出现错误

- error: 'Supplied key type of part 0 does not match index part type: expected unsigned'

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

解决方法

文档说:

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

显式使用该二级索引:

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

详细了解in the documentation.