siddharth21ss
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # redis OM > **Content description**: How to create a cluster of redis to perform fast and scalalble search in python. Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. — Redis Website (But i digress as it can be ***the only DB you need*** with super capabilites of hashes, Object mapping, Pub/Sub, Search, Indexing and json, Graph and timeseries, and most importantly speed and reliability) Understanding redis Every Redis Cluster node requires two TCP connections. The normal Redis TCP port used to serve clients, i.e. 6379, plus the port obtained by adding 10000 to the data port, so 16379. Make sure you open both ports in your firewall, otherwise Redis cluster nodes will be not able to communicate. Redis Cluster does not use consistent hashing, but a different form of sharding where every key is conceptually part of what we call an hash slot. There are 16384 hash slots in Redis Cluster. Every node in a Redis Cluster is responsible for a subset of the hash slots, so for example you may have a cluster with 3 nodes, where: * Node A contains hash slots from 0 to 5500. * Node B contains hash slots from 5501 to 11000. * Node C contains hash slots from 11001 to 16383. You can use docker & docker-compose to set up a local cluster vai using multiple templates on the web eg.: https://pierreabreu.medium.com/building-redis-cluster-with-docker-compose-9569ddb6414a I wish to explore on the search features of Redis combined with the Json module for redis. In the tutorial we'll also create a small microservice for extending and interacting with redis. # env setup for redis om ``` $ export export REDIS_OM_URL=redis://<username>:<password>@<host>:<port> ``` # Using Pokemon data: <summary>pokemon csv:</summary> ``` Name,Type_1,Type_2,Max_CP,Max_HP Bulbasaur,Grass,Poison,1079,83 Ivysaur,Grass,Poison,1643,107 Venusaur,Grass,Poison,2598,138 Charmander,Fire,,962,73 Charmeleon,Fire,,1568,103 Charizard,Fire,Flying,2620,135 Squirtle,Water,,1015,81 Wartortle,Water,,1594,105 Blastoise,Water,,2560,137 Caterpie,Bug,,446,83 Metapod,Bug,,481,91 Butterfree,Bug,Flying,1465,107 Weedle,Bug,Poison,452,75 Kakuna,Bug,Poison,488,83 Beedrill,Bug,Poison,1450,115 Pidgey,Normal,Flying,684,75 Pidgeotto,Normal,Flying,1232,111 Pidgeot,Normal,Flying,2106,143 Rattata,Normal,,585,59 Raticate,Normal,,1454,99 Spearow,Normal,Flying,691,75 Fearow,Normal,Flying,1758,115 Ekans,Poison,,830,67 Arbok,Poison,,1779,107 Pikachu,Electric,,894,67 Raichu,Electric,,2042,107 Sandshrew,Ground,,804,91 Sandslash,Ground,,1823,130 Nidoran♀,Poison,,882,99 Nidorina,Poison,,1414,122 Nidoqueen,Poison,Ground,2502,154 Nidoran♂,Poison,,849,84 Nidorino,Poison,,1382,108 Nidoking,Poison,Ground,2492,140 Clefairy,Fairy,,1209,122 Clefable,Fairy,,2414,162 Vulpix,Fire,,837,72 Ninetales,Fire,,2203,127 Jigglypuff,Normal,Fairy,924,194 Wigglytuff,Normal,Fairy,2192,233 Zubat,Poison,Flying,647,75 Golbat,Poison,Flying,1935,130 Oddish,Grass,Poison,1156,83 Gloom,Grass,Poison,1701,107 Vileplume,Grass,Poison,2510,130 Paras,Bug,Grass,923,67 Parasect,Bug,Grass,1759,107 Venonat,Bug,Poison,1036,107 Venomoth,Bug,Poison,1903,122 Diglett,Ground,,460,27 Dugtrio,Ground,,1176,67 Meowth,Normal,,761,75 Persian,Normal,,1643,115 Psyduck,Water,,1117,91 Golduck,Water,,2403,138 Mankey,Fighting,,884,75 Primeape,Fighting,,1877,115 Growlithe,Fire,,1344,99 Arcanine,Fire,,3005,154 Poliwag,Water,,801,75 Poliwhirl,Water,,1350,115 Poliwrath,Water,Fighting,2523,154 Abra,Psychic,,604,51 Kadabra,Psychic,,1140,75 Alakazam,Psychic,,1826,99 Machop,Fighting,,1097,122 Machoke,Fighting,,1773,138 Machamp,Fighting,,2612,154 Bellsprout,Grass,Poison,1125,91 Weepinbell,Grass,Poison,1736,115 Victreebel,Grass,Poison,2548,138 Tentacool,Water,Poison,911,75 Tentacruel,Water,Poison,2236,138 Geodude,Rock,Ground,855,75 Graveler,Rock,Ground,1443,99 Golem,Rock,Ground,2319,138 Ponyta,Fire,,1526,91 Rapidash,Fire,,2215,115 Slowpoke,Water,Psychic,1227,154 Slowbro,Water,Psychic,2615,162 Magnemite,Electric,Steel,897,51 Magneton,Electric,Steel,1893,91 Farfetch'd,Normal,Flying,1272,94 Doduo,Normal,Flying,861,67 Dodrio,Normal,Flying,1849,107 Seel,Water,,1114,115 Dewgong,Water,Ice,2161,154 Grimer,Poison,,1293,138 Muk,Poison,,2621,178 Shellder,Water,,828,59 Cloyster,Water,Ice,2067,91 Gastly,Ghost,Poison,810,59 Haunter,Ghost,Poison,1390,83 Gengar,Ghost,Poison,2093,107 Onix,Rock,Ground,863,67 Drowzee,Psychic,,1082,107 Hypno,Psychic,,2199,146 Krabby,Water,,797,59 Kingler,Water,,1836,99 Voltorb,Electric,,845,75 Electrode,Electric,,1657,107 Exeggcute,Grass,Psychic,1107,107 Exeggutor,Grass,Psychic,2976,162 Cubone,Ground,,1013,91 Marowak,Ground,,1668,107 Hitmonlee,Fighting,,1503,91 Hitmonchan,Fighting,,1527,91 Lickitung,Normal,,1638,154 Koffing,Poison,,1160,75 Weezing,Poison,,2266,115 Rhyhorn,Ground,Rock,1190,138 Rhydon,Ground,Rock,2259,178 Chansey,Normal,,679,408 Tangela,Grass,,1752,115 Kangaskhan,Normal,,2057,178 Horsea,Water,,800,59 Seadra,Water,,1725,99 Goldeen,Water,,972,83 Seaking,Water,,2058,138 Staryu,Water,,944,59 Starmie,Water,Psychic,2197,107 Mr. Mime,Psychic,Fairy,1505,75 Scyther,Bug,Flying,2088,122 Jynx,Ice,Psychic,1728,115 Electabuzz,Electric,,2134,115 Magmar,Fire,,2281,115 Pinsir,Bug,,2137,115 Tauros,Normal,,1857,130 Magikarp,Water,,264,43 Gyarados,Water,Flying,2708,162 Lapras,Water,Ice,3002,218 Ditto,Normal,,926,88 Eevee,Normal,,1084,99 Vaporeon,Water,,2836,218 Jolteon,Electric,,2155,115 Flareon,Fire,,2662,115 Porygon,Normal,,1703,115 Omanyte,Rock,Water,1127,67 Omastar,Rock,Water,2249,122 Kabuto,Rock,Water,1112,59 Kabutops,Rock,Water,2145,107 Aerodactyl,Rock,Flying,2180,138 Snorlax,Normal,,3135,265 Articuno,Ice,Flying,2999,154 Zapdos,Electric,Flying,3136,154 Moltres,Fire,Flying,3263,154 Dratini,Dragon,,990,76 Dragonair,Dragon,,1760,108 Dragonite,Dragon,Flying,3525,156 Mewtwo,Psychic,,4174,180 Mew,Psychic,,3322,170 ``` # model.py ``` from aredis_om import HashModel,JsonModel, NotFoundError , Field from pydantic import BaseModel class Definitions(JsonModel, BaseModel): Name: str = Field(index=True, full_text_search=True) Type_1: str = Field(index=True, full_text_search=True) Type_2: str = Field(index=True, full_text_search=True) Max_CP:int Max_HP:int ``` # data upload file ``` from csv import DictReader import aioredis import asyncio from aredis_om import HashModel, NotFoundError from aredis_om import get_redis_connection from aredis_om import Migrator from model import Definitions async def main(): with open("pokemonGO.csv", 'r') as f: dict_reader = DictReader(f) list_of_dict = list(dict_reader) for i in list_of_dict: print(i) d = Definitions(**i) await d.save() await Migrator().run() loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close() ``` # fastapi file ``` import asyncio from fastapi import FastAPI, HTTPException from starlette.requests import Request from starlette.responses import Response from aredis_om import HashModel, NotFoundError , Migrator from aredis_om import get_redis_connection from model import Definitions from redis import ResponseError from redisearch import Client, IndexDefinition, TextField, NumericField, TagField, GeoField from redisearch import reducers from redisearch.aggregation import AggregateRequest, Asc, Desc def initializeClient(): SCHEMA = ( TextField("Name"), TextField("Type_1"), TextField("Type_2"), NumericField("Max_CP"), NumericField("Max_HP") ) client = Client("pokedex", host=<host>, port=12830, conn=None, password=<password>, decode_responses=True) definition = IndexDefinition(prefix=[':model.Definitions:']) try: client.info() except ResponseError: # Index does not exist. We need to create it! client.create_index(SCHEMA, definition=definition) return client client = initializeClient() app = FastAPI() @app.post("/pokemon") async def save_pkemon(pokemon: Definitions): return await pokemon.save() @app.get("/search/{keyword}") async def search_pokemon(keyword:str): return client.search(f"@Name:{keyword}") @app.get("/search-type1/{keyword}") async def search_pokemon(keyword:str): return client.search(f"@Type_1:{keyword}*") @app.get("/search-type2/{keyword}") async def search_pokemon(keyword:str): return client.search(f"@Type_2:{keyword}*") @app.get("/fuzyy-search/{keyword}") async def fuzzy_pokemons(keyword:str): return await Definitions.find(Definitions.Name % keyword).all() @app.get("/pokemon/") async def list_pokemons(): return await Definitions.find().all() ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully