简体   繁体   English

如何搜索不区分大小写的cassandra?

[英]How to search in cassandra case-insensitive?

I am learning cassandra with Python and specially with Django 2 using cqlengine . 我正在使用Python学习cassandra ,特别是使用cqlengineDjango 2 cqlengine I am trying to search in database where I search for string that starts with the search parameter but I want to make it case insensitive. 我正在尝试在数据库中搜索以search参数开头的字符串,但我想使其不区分大小写。 So if I have following data 所以如果我有以下数据

-------------------------------
|    PKID    |     String     |
-------------------------------
|    1234    |     FOObar     |
|    4321    |     FoOBar     |
|    5665    |     IreALLy    |
|    5995    |     DontknoW   |
|    8765    |     WHatTOdo   |
|    4327    |     foobaR     |
-------------------------------

So if I want to search for string that starts with foo , I should get all three records. 因此,如果我要搜索以foo开头的字符串,则应该获取所有三个记录。 I searched for the solution and I found one comment on stackoverflow that everything is byte in cassandra and so it is not possible but I also found something that says I need to write custom function to do it. 我搜索了解决方案,并发现了关于stackoverflow的一条评论,即在cassandra中所有内容都是字节,所以这是不可能的,但我也发现有些东西说我需要编写自定义函数来做到这一点。

For Django I am using django-cassandra-engine to create model. 对于Django,我使用django-cassandra-engine创建模型。 It is an implementation of cqlengine for django. 它是django的cqlengine的实现。 So when I create my model, is there anything that I need to add in it? 因此,当我创建模型时,是否需要添加任何内容? My test model is 我的测试模型是

class TestModel(DjangoCassandraModel):
    key_id = columns.UUID(primary_key=True, default=uuid.uuid4)
    string = columns.Text()

I looked for it in cqlengine docs but couldn't find anything helpful. 我在cqlengine文档中寻找了它,但找不到任何有用的东西。 So I am seeking for help here on stackoverflow. 所以我在这里寻求有关stackoverflow的帮助。

Thanks. 谢谢。

You can use so-called SASI-index (SSTable Attached Secondary Index) to do it in Cassandra itself (although it's marked as experimental feature). 您可以使用所谓的SASI-index(SSTable附加二级索引)在Cassandra本身中执行此操作(尽管已将其标记为实验功能)。 You can define indexes for doing prefix, range, or substring search, and when configuring index, you can specify that you want case-insensitive strings: 您可以定义用于进行前缀,范围或子字符串搜索的索引,并且在配置索引时,可以指定您要区分大小写的字符串:

CREATE CUSTOM INDEX index_name ON table (field) 
  USING 'org.apache.cassandra.index.sasi.SASIIndex'
  WITH OPTIONS = { 
    'mode': 'PREFIX', // if you want to search only for 'starting with'
    'case_sensitive': 'false'
};

Although I'm not sure, will cqlengine pickup this functionality out of the box, or not. 尽管我不确定,cqlengine是否会立即使用此功能。

Here is very detailed blog post about SASI-indexes. 这是有关SASI-indexes的非常详细的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM