How do I search strings in redis?
Asked Answered
C

4

43

I want an autocomplete feature. I have short descriptive strings on a property of a data type. I have a list of ids in redis for the datatype ordered by created date and I use the ids to set and get properties for the datatype as explained in the redis type documentation. I don't use hash tables. What's the best way to get a set of strings matching what's been typed into an autocomplete input box given this setup? Going through all ids and checking the property I want to search - for each keystroke seems like the wrong way to do this.

EDIT: In addition to the answers below, I've been shown this:

http://antirez.com/post/autocomplete-with-redis.html

Canicula answered 19/6, 2011 at 7:41 Comment(0)
P
16

You need to set up an index using sets or sorted sets that you write to when you save anything.

There's a good writeup at http://web.archive.org/web/20121013063245/http://playnice.ly/blog/2010/05/05/a-fast-fuzzy-full-text-index-using-redis that is pretty close to what I use myself.

Phosphoprotein answered 19/6, 2011 at 11:26 Comment(3)
Sadly the link is down, so I found another good articleBedeck
for every lost site, there is always wayback machine web.archive.org/web/20121013063245/http://playnice.ly/blog/2010/…Pepsinogen
Can't you use lua and its pattern matching to search inside a string?Nuristan
G
6

In Redis, there is no way to search the value of a key. The only way you can "find" a string, is via the keys command. The only downside is that it searches the key names, not the value. The way you can get around this is by having your search string as the key and then have the value of said key your ID. I use an autocompleate function on my side, and I use another database that just contains search strings with have an ID as a value.

Gros answered 19/6, 2011 at 11:25 Comment(3)
No, they are both Redis. Database #3 and #4Gros
Ah, multiple databases aren't going to work if I want to use redis-cluster. I guess I could try another redis process.Canicula
As an update: the Redis commands SCAN, HSCAN, ZSCAN and SSCAN also support the MATCH pattern-search syntax of KEYS, but there's a pretty initimidating warning against using it in production at redis.io/commands/keysTriumphal
P
3

You can try RediSearch.
It's a Redis module that provides querying, secondary indexing, and full-text search for Redis.

It uses compressed, inverted indexes for fast indexing with a low memory footprint.
It's indexes enhance Redis by providing exact-phrase matching, fuzzy search, and numeric filtering, among many other features.

redisearch-stars

Perspire answered 20/6, 2021 at 15:44 Comment(0)
H
0

Redis itself does not support it. We have modified the source code of Redis so that it can support full-text search, string search, and some calculations of summation and average.

Project homepage: http://oncedb.com

Full text search

OnceDB supports direct search, supports objects such as String and Hash,

Search string: search pattern operator value

Use the SEARCH command

# Create data
set test1 'this is testing'
> OK
set test2 'this is article'
> OK

# Search data
search test* ~ article
1) test1
2) this is testing
3) test2
4) this is article

Operator supports:

= Exact match
~ Partial match
> >= < <= Compare match

Search hash: hsearch pattern field operator value ...

Use the hsearch command

There is no concept of "table" in redis. Generally, the key name using schema:id includes the table name and the primary key. For example, user:001 is a hash object with a user table and a primary key value of 001.

# Create hash data
hmset user:001 name kris email [email protected] gender male age 16
> OK
hmset user:002 name sunny age 24
> OK

# Search hash data
hsearch user:* age > 18 name = *
1) user:002
2) 24
3) sunny

Search ordered list(zset): zhsearch key from to schema field operator value ...

Use test:* or user:* pattern matching directly, because it will traverse all the keys, and the performance is low. zhsearch can search the corresponding primary key in an ordered list and specify the search scope.

In the above example, we store the primary key value of user into the *user ordered list, and the score is an integer about time

zadd *user 20020201 001 20020202 002

Search for the first matching data (from 0 to 0)

zhsearch *user 0 0 user: age > 10
1) -1
2) user:001
3) 16

When using full-text search, the first parameter returned is always -1.

OnceDB does not change the data storage structure of Redis. Redis database files can be directly operated in OnceDB and then returned to Redis for use.

Index query

The performance of full-text search is poor. You can improve the performance by creating indexes.

OnceDB can choose to automatically create auxiliary indexes when data is modified.

Detailed information can refer to this answer

Redis and Querying Values

Heavensent answered 1/2, 2020 at 10:20 Comment(1)
This answer is not applicable to redisIssykkul

© 2022 - 2024 — McMap. All rights reserved.