skip to Main Content

I am looking to add a search engine to an app with as fast of a response as possible. The users are searching for tickers or company names so it’s a relatively compact data set, so I am wondering if storing all of the names and tickers in redis would be viable instead of querying the database directly.

We use MongoDB if that changes anything.

2

Answers


  1. You can use redis for search if you are storing data as separate Lists or Hashes.

    https://redis.io/topics/data-types

    In case, you need to perform actions like free text search, redis is not able to do that and you need to keep using MongoDB

    Login or Signup to reply.
  2. Redis can be extended by installing modules.

    RediSearch provides indexing, querying and full-text search on hashes and on JSON documents (when using RedisJSON).

    You can insert the Tickers or company names in hashes. As Redis is storing everything in memory (the index as well) the search request will be fast.

    Here is an example:

    Create an index with two fields. name and ticker using the TEXT type.
    (Note that for the ticker we disable the stemming.)

    127.0.0.1:6379> FT.CREATE idx SCHEMA name TEXT ticker TEXT NOSTEM
    OK
    

    Insert a company in a HASH:

    127.0.0.1:6379> HSET ticker:ufo ticker UFO name SpaceX
    (integer) 2
    

    I can search by using the ticker:

    127.0.0.1:6379> FT.SEARCH idx ufo
    1) (integer) 1
    2) "ticker:ufo"
    3) 1) "ticker"
       2) "UFO"
       3) "name"
       4) "SpaceX"
    

    Or search with the company name:

    127.0.0.1:6379> FT.SEARCH idx spacex
    1) (integer) 1
    2) "ticker:ufo"
    3) 1) "ticker"
       2) "UFO"
       3) "name"
       4) "SpaceX"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search