如何在 Tarantool 中通过二级索引创建 select?
How to make a select via secondary indexes in Tarantool?
我创建了一个 space,其中包含两个索引 — 主索引和辅助索引:
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 is optional. If it is omitted, then the assumed index is the first (primary-key) index. Therefore, for the example above, box.space.tester:select({1}, {iterator = 'GT'}) would have returned the same two rows, via the ‘primary’ index.
明确使用二级索引:
tarantool> box.space.user.index.bio:select({'other stuff'})
---
- - [1, 'other stuff']
...
了解更多信息 in the documentation.
我创建了一个 space,其中包含两个索引 — 主索引和辅助索引:
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 is optional. If it is omitted, then the assumed index is the first (primary-key) index. Therefore, for the example above, box.space.tester:select({1}, {iterator = 'GT'}) would have returned the same two rows, via the ‘primary’ index.
明确使用二级索引:
tarantool> box.space.user.index.bio:select({'other stuff'})
---
- - [1, 'other stuff']
...
了解更多信息 in the documentation.