如何在Redis中搜索? [英] How to search in Redis?
问题描述
在 MySQL 中,我有一个表:
In MySQL I have a table called:
cities
包括 2 列:name
和 population
cities
which includes 2 columns: name
and population
然后我得到这样的搜索结果:
and then I get the search results with this:
SELECT * FROM city WHERE name LIKE '%Bu%' ORDER by population DESC LIMIT 2
然后我得到结果Bucharest
,Budapest
.
如何在 Redis 中执行此操作?我的意思是如何创建类似的结构,然后如何搜索值?
How can I do this in Redis? I mean how can I create a similar structure and then how can I search for values?
推荐答案
我认为您需要对键值存储的工作原理进行更多研究,但要将您的帖子用作过于简化的示例,one 这样做的方法是为您的所有值创建键,然后创建与这些键匹配的索引,例如:
I think you need to do some more research on how key-value storage works, but to use your post as an over-simplified example, one way to do this would be for you to create keys for all your values and then create indexes matched to those keys like:
SET c1 "Bucharest"
SET c3 "Budapest"
SADD city:bu c1
SADD city:bu c3
SMEMBERS city:bu
=> ["c1","c3"]
=> ["c1","c3"]
您可以使用:http://try.redis-db.com/ 来尝试一下.
you can use: http://try.redis-db.com/ to try it out.
将其扩展为 3 个字符搜索:
to extend that for 3 character search:
SADD city:buc c1
SMEMBERS city:buc
=>["c1"]
有很多方法可以解决这个问题,包括您可以为索引分配排名的方法等.另请注意,此示例与引用的方法不同 example,但更容易理解——它类似于使用的这里.
There are many ways to approach this, including methods where you can assign rank to your index, etc. Also note that this example is a different approach from that referenced example, but a bit easier to understand -- it's similar to one used here.
这篇关于如何在Redis中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!