3

In MySQL I have a table called:

cities which includes 2 columns: name and population

and then I get the search results with this:

SELECT * FROM cities WHERE name LIKE '%Bu%' ORDER by population DESC LIMIT 2

And I get the Results Bucharest, Budapest.

How can I do this in Redis? I mean how can I create a similar structure and then how can I search for values?

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
  • you don't. see: http://stackoverflow.com/questions/6401194/how-do-i-search-strings-in-redis – ldg Sep 29 '11 at 23:54
  • @ldg I see but for that question there are no answers which help me understand how is this working in redis. – Adam Halasz Sep 29 '11 at 23:58

1 Answers1

7

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"]

you can use: http://try.redis-db.com/ to try it out.

to extend that for 3 character search:

SADD city:buc c1
SMEMBERS city:buc

=>["c1"]

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.

ldg
  • 9,112
  • 2
  • 29
  • 44
  • Hi. I need to store multiple one two many values where each of the 'many' may have an additional string value attached to it. I need to be able to find the parent item by its child string value and provide efficient searching by contains, starts with etc. 1) Is Redis a good choice at all? 2) Any other ideas (preferably MIT/BSD/Apache licensed)? Thank you! – Shimmy Weitzhandler Nov 26 '17 at 05:08
  • Hi @Shimmy - this probably isn't the right forum as that's a pretty broad question with a lot of application-specific issues. You could use Redis by setting up your own indexes like [this](https://robots.thoughtbot.com/redis-partial-word-match-you-auto-complete-me) but from your brief description and it seems you aren't proficient with Redis, probably not the best choice. Look at a DB with native text searching which range from a document store (~MongoDB), to RDBMS, to a Multi-Model DB to Lucene/Solr -- or some mix of those, plus others! – ldg Dec 05 '17 at 06:41