【问题标题】:How to make a select via secondary indexes in Tarantool?如何通过 Tarantool 中的二级索引进行选择?
【发布时间】:2020-09-25 15:43:42
【问题描述】:

我创建了一个包含两个索引的空间——主要和次要:

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'

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

【问题讨论】:

    标签: lua tarantool


    【解决方案1】:

    文档说:

    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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 2021-11-08
      • 2019-10-26
      • 2018-06-03
      • 2020-04-06
      • 2020-01-06
      • 2013-11-30
      • 2016-04-13
      相关资源
      最近更新 更多