{
  "id": "nodejs",
  "title": "Redis recommendation engine with node-redis",
  "url": "https://redis.io/docs/latest/develop/use-cases/recommendation-engine/nodejs/",
  "summary": "Build a Redis-backed recommendation engine in Node.js with node-redis and @xenova/transformers",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-05-26T09:29:27-05:00",
  "children": [],
  "page_type": "content",
  "content_hash": "7999b20550fbd2e8b92945dbe915d2c35ea71d7ba4e82e283f1d156cac56a67c",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to build a small Redis-backed product recommendation service in Node.js with [`node-redis`](https://redis.io/docs/latest/develop/clients/nodejs) and the [`@xenova/transformers`](https://www.npmjs.com/package/@xenova/transformers) library. It includes a local web server built on the Node.js `http` module so you can embed a natural-language query, run a KNN retrieval with structured pre-filters in one round trip, feed clicks back as a session signal, and watch the next recommendation incorporate them immediately."
    },
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Each product is stored as a single Redis [Hash](https://redis.io/docs/latest/develop/data-types/hashes) at `product:<id>`. The hash holds the structured metadata (name, description, category, brand, price, rating, in-stock flag) alongside the raw `float32` bytes of a 384-dimensional embedding. A single [Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query) index covers every field, so one [`FT.SEARCH`](https://redis.io/docs/latest/commands/ft.search) call with a `KNN` clause does the vector similarity *and* the TAG / NUMERIC / TEXT pre-filtering in the same pass — no cross-store joins.\n\nPer-user state lives in `user:<id>:features`: a session vector written as an exponentially weighted average of recently-clicked item embeddings, plus per-category affinity counters incremented atomically with [`HINCRBYFLOAT`](https://redis.io/docs/latest/commands/hincrbyfloat). [`FT.SEARCH`](https://redis.io/docs/latest/commands/ft.search) does *not* read that hash directly; instead, the application reads it on the next request and passes the session vector to `FT.SEARCH` as the query parameter. The two-step is what lets a click feed the very next recommendation without a batch cycle or cache invalidation.\n\nThat gives you:\n\n* A single round trip for retrieval — vector KNN + structured filters in one [`FT.SEARCH`](https://redis.io/docs/latest/commands/ft.search).\n* Sub-millisecond hot path once the query is embedded; embedding the query is the bottleneck, and that's a model-side cost, not a Redis one.\n* Real-time session signals — a click writes a new session vector and bumps an affinity counter; the next query reads them and folds them in.\n* No-downtime embedding refresh — [`HSET`](https://redis.io/docs/latest/commands/hset) on the vector field, and the HNSW index reflects the change on the next query."
    },
    {
      "id": "how-it-works",
      "title": "How it works",
      "role": "content",
      "text": "There are two distinct paths: a **query path** runs every time the application wants a recommendation, and a **click path** runs every time the user interacts with a product."
    },
    {
      "id": "query-path-per-recommendation-request",
      "title": "Query path (per recommendation request)",
      "role": "content",
      "text": "1. The application calls `embedder.encodeOne(queryText)` to turn a natural-language query into a 384-dimensional `Float32Array`.\n2. The application reads the user's session vector and affinities from the user features hash. If a session vector exists, it gets blended into the query vector with a tunable weight, so the user's recent clicks pull retrieval toward what they've been engaging with.\n3. `recommender.candidateRetrieve(queryVec, ...)` runs [`FT.SEARCH`](https://redis.io/docs/latest/commands/ft.search) with a pre-filter clause built from the request's TAG / NUMERIC / TEXT inputs, followed by a `KNN k @embedding $vec` clause. Redis returns up to `k` candidates with the cosine distance to the query (lower is closer).\n4. `recommender.rerank(candidates, userFeatures)` subtracts a log-scaled per-category affinity bonus from each candidate's distance and re-sorts the list closest-first. The log scaling keeps repeated clicks from running away with the ranking."
    },
    {
      "id": "click-path-per-user-interaction",
      "title": "Click path (per user interaction)",
      "role": "content",
      "text": "When the user clicks a product, `recommender.recordClick(userId, productId)` does the following:\n\n1. Reads the clicked item's embedding from its hash.\n2. Reads the user's previous session vector from the user features hash, blends the new click in via an exponentially weighted moving average, and writes the new session vector back with [`HSET`](https://redis.io/docs/latest/commands/hset). This is a read-modify-write — atomic against any single write but not against a concurrent click for the same user; in practice, per-user click streams don't generate the contention to make this matter, and if a deployment does, the read and write can be wrapped in [`WATCH/MULTI/EXEC`](https://redis.io/docs/latest/commands/multi) or a small Lua script.\n3. Bumps the per-category affinity counter with [`HINCRBYFLOAT`](https://redis.io/docs/latest/commands/hincrbyfloat) — atomic, no read needed — and the click count with [`HINCRBY`](https://redis.io/docs/latest/commands/hincrby).\n\nThe next query path picks both changes up the next time it reads the user features hash.\n\nRefreshing an item's embedding follows a similar shape: encode the new text, write the vector bytes back with [`HSET`](https://redis.io/docs/latest/commands/hset), and the HNSW index reflects the change on the next query without a rebuild."
    },
    {
      "id": "the-recommender-helper",
      "title": "The recommender helper",
      "role": "content",
      "text": "The `RedisRecommender` class wraps the Redis Search index and the retrieval flow\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/recommendation-engine/nodejs/recommender.js)):\n\n[code example]"
    },
    {
      "id": "data-model",
      "title": "Data model",
      "role": "content",
      "text": "Each product is one Redis Hash. The vector field is raw little-endian `float32` bytes — no JSON wrapping — because the Redis Search vector encoding expects exactly that. In Node.js, that's a `Buffer` view over a `Float32Array`.\n\n[code example]\n\nThe Redis Search index schema treats every field as queryable in its natural type:\n\n[code example]\n\nIn `node-redis` 5.x that schema is expressed with the `SCHEMA_FIELD_TYPE` and `SCHEMA_VECTOR_FIELD_ALGORITHM` constants — see [`recommender.js`](https://github.com/redis/docs/blob/main/content/develop/use-cases/recommendation-engine/nodejs/recommender.js).\n\nPer-user state is a separate hash. The session vector is stored as raw `float32` bytes the same way; affinity counters are stored as plain numeric strings, one field per category, prefixed with `aff:` so they don't collide with anything else.\n\n[code example]"
    },
    {
      "id": "the-query",
      "title": "The query",
      "role": "content",
      "text": "The KNN clause is a hybrid query: a pre-filter expression in parentheses, then `=>[KNN k @embedding $vec]`. With `DIALECT 2`, Redis applies the filter first and then KNN-ranks only the matching documents.\n\n[code example]\n\nWhen there's no filter, the pre-filter clause is just `(*)`. `vector_score` returned by Redis is the cosine distance (0 = identical, 2 = opposite), where 0 is a perfect match — so the result is sorted ascending."
    },
    {
      "id": "binary-fields-with-node-redis-5-x",
      "title": "Binary fields with `node-redis` 5.x",
      "role": "content",
      "text": "`node-redis` returns text by default. The embedding field is binary, so the helper derives a *type-mapped view* of the same connection that returns blob strings as `Buffer` instances:\n\n[code example]\n\nThe `RedisRecommender` keeps both views and routes reads accordingly: the binary view for the `embedding` and `session_vec` fields, the default view for `category`, `clicks`, and the rest."
    },
    {
      "id": "the-catalog-builder",
      "title": "The catalog builder",
      "role": "content",
      "text": "Item vectors are pre-computed once and stored in `catalog.json` so the demo server can boot quickly. `buildCatalog.js` is the reference for how to do that — and is the script you'd adapt for a real catalog ingestion pipeline\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/recommendation-engine/nodejs/buildCatalog.js)):\n\n[code example]\n\nIn production the equivalent of this script lives in an offline pipeline: embed once on catalog updates and ship the vectors into Redis with [`HSET`](https://redis.io/docs/latest/commands/hset). The serving tier still embeds the *query* on each request, but that's usually fronted by a dedicated model server or batched at the API gateway rather than co-located with the data tier as it is in this demo."
    },
    {
      "id": "the-interactive-demo",
      "title": "The interactive demo",
      "role": "content",
      "text": "`demoServer.js` runs an `http` server with one demo user (`demo`). The HTML page lets you:\n\n* Type a natural-language query and toggle filters: TAG (category, brand, in-stock), NUMERIC (price range, rating), and TEXT (the **Description contains** field, a phrase pre-filter against the `description` text index).\n* Toggle session blending and category-affinity re-ranking independently to see what each layer contributes.\n* Click any product card to record a click into the session. The page re-renders the user features panel immediately — the click wrote to the user features hash, and the next search reads that hash to fold the update in.\n* Refresh a single product's embedding with new text and watch the ranking change on the next query.\n\nThe server holds one `LocalEmbedder` and one `RedisRecommender` for the lifetime of the process. Endpoints:\n\n| Endpoint                | What it does                                                                |\n|-------------------------|-----------------------------------------------------------------------------|\n| `GET  /state`           | Index info, user features, full catalog listing.                            |\n| `POST /search`          | Embed the query, run `FT.SEARCH` with filters + KNN, optionally re-rank.    |\n| `POST /click`           | Record a click for the demo user: update session vector and affinity.       |\n| `POST /reset-user`      | Drop the user features hash.                                                |\n| `POST /reset-index`     | Drop the index and documents and re-seed from `catalog.json`.               |\n| `POST /refresh-embedding` | Embed new text and overwrite one product's vector with `HSET`.            |"
    },
    {
      "id": "run-the-demo-locally",
      "title": "Run the demo locally",
      "role": "content",
      "text": "1.  Clone the [`redis/docs`](https://github.com/redis/docs) repository and change into the example\n    directory:\n\n    [code example]\n\n2.  Install the dependencies:\n\n    [code example]\n\n3.  Make sure a Redis instance with the Redis Search module is running locally on\n    port 6379. [Redis Stack](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack) or\n    [Redis 8 with Search](https://redis.io/docs/latest/develop/ai/search-and-query) both work.\n\n4.  Generate the catalog with pre-computed embeddings. The first run downloads the\n    `all-MiniLM-L6-v2` model (~80 MB) into the local Transformers.js cache:\n\n    [code example]\n\n5.  Start the demo server:\n\n    [code example]\n\n6.  Open <http://localhost:8085> and try some queries:\n\n    * **\"insulated down jacket for cold weather\"** — filtered to `outerwear`, in-stock only.\n    * **\"comfortable shoes for trail running\"** — filtered to `footwear`.\n    * Add **Description contains: waterproof** to either query above to see a TEXT pre-filter\n      combine with the KNN.\n    * Click a couple of products to seed a session, then re-run the same query\n      with **Blend session vector into query** on and watch the ranking shift.\n    * Use **Refresh embedding** to change a product's vector — for example,\n      change the Alpine down parka's text to \"heavy duty arctic expedition parka\n      with hood\" and re-run the jacket query to see the result move.\n\nThe server is read/write against your local Redis. The default index name is `recommend:idx` and product keys live under `product:`. Pass `--no-reset` to keep an existing index across restarts, or `--redis-url redis://host:port` to point at a different Redis."
    }
  ],
  "examples": [
    {
      "id": "the-recommender-helper-ex0",
      "language": "javascript",
      "code": "import { createClient } from \"redis\";\nimport { LocalEmbedder } from \"./embeddings.js\";\nimport { RedisRecommender } from \"./recommender.js\";\n\nconst client = createClient();\nawait client.connect();\nconst recommender = new RedisRecommender({\n  redisClient: client,\n  indexName: \"recommend:idx\",\n});\nconst embedder = new LocalEmbedder();  // Xenova/all-MiniLM-L6-v2\n\n// One-time index setup (idempotent).\nawait recommender.createIndex();\n\n// Embed the natural-language query.\nconst queryVec = await embedder.encodeOne(\"warm waterproof jacket for hiking\");\n\n// Retrieval: KNN with structured pre-filter in one round trip.\n// Filters combine TAG (category, brand, inStockOnly), NUMERIC\n// (price range, rating), and TEXT (textMatch against textField) —\n// Redis applies them all in front of the KNN.\nconst candidates = await recommender.candidateRetrieve(queryVec, {\n  category: \"outerwear\",\n  inStockOnly: true,\n  minPrice: 50,\n  maxPrice: 200,\n  textMatch: \"waterproof\",        // TEXT pre-filter on @description\n  k: 10,\n});\n\n// Record a click — updates the user's session vector and category\n// affinity atomically; the next call to candidateRetrieve sees it.\nawait recommender.recordClick(\"alice\", \"p001\");\n\n// Pull user features so the next retrieval can blend the session\n// vector into the query and apply the category-affinity re-rank.\nconst features = await recommender.getUserFeatures(\"alice\");\nconst reranked = recommender.rerank(\n  await recommender.candidateRetrieve(queryVec, {\n    category: \"outerwear\",\n    inStockOnly: true,\n    k: 10,\n    sessionVec: features.session_vec,\n    sessionWeight: 0.3,\n  }),\n  features,\n  0.15,\n);\n\n// Hot embedding refresh — overwrite the vector for one product; the\n// HNSW index reflects the change on the next FT.SEARCH.\nconst newVector = await embedder.encodeOne(\"heavy-duty arctic expedition parka\");\nawait recommender.refreshEmbedding(\"p001\", newVector);",
      "section_id": "the-recommender-helper"
    },
    {
      "id": "data-model-ex0",
      "language": "text",
      "code": "product:p001\n  name=Alpine down parka\n  description=Heavyweight 800-fill goose down parka...\n  category=outerwear\n  brand=northpeak\n  price=289\n  rating=4.7\n  in_stock=true\n  embedding=<384 × float32 little-endian bytes>",
      "section_id": "data-model"
    },
    {
      "id": "data-model-ex1",
      "language": "text",
      "code": "FT.CREATE recommend:idx\n  ON HASH PREFIX 1 product:\n  SCHEMA\n    name        TEXT\n    description TEXT\n    category    TAG\n    brand       TAG\n    in_stock    TAG\n    price       NUMERIC SORTABLE\n    rating      NUMERIC SORTABLE\n    embedding   VECTOR HNSW 6 TYPE FLOAT32 DIM 384 DISTANCE_METRIC COSINE",
      "section_id": "data-model"
    },
    {
      "id": "data-model-ex2",
      "language": "text",
      "code": "user:alice:features\n  session_vec=<384 × float32 little-endian bytes>\n  aff:outerwear=2\n  aff:footwear=1\n  last_clicked_id=p015\n  last_clicked_category=footwear\n  clicks=3",
      "section_id": "data-model"
    },
    {
      "id": "the-query-ex0",
      "language": "text",
      "code": "FT.SEARCH recommend:idx\n  \"(@category:{outerwear} @in_stock:{true} @price:[50 200])\n     =>[KNN 10 @embedding $vec AS vector_score]\"\n  PARAMS 2 vec <384-float32-bytes>\n  SORTBY vector_score\n  RETURN 8 name description category brand price rating in_stock vector_score\n  DIALECT 2",
      "section_id": "the-query"
    },
    {
      "id": "binary-fields-with-node-redis-5-x-ex0",
      "language": "javascript",
      "code": "import { RESP_TYPES } from \"redis\";\n\n// One connection, two views: ``client`` returns strings; ``redisBuf``\n// returns Buffer for every bulk-string reply (HGET, HMGET, HGETALL).\n// Both share the underlying socket.\nconst redisBuf = client.withTypeMapping({\n  [RESP_TYPES.BLOB_STRING]: Buffer,\n});\n\nconst embedding = await redisBuf.hGet(\"product:p001\", \"embedding\");\n// embedding is a Buffer of 384*4 = 1536 bytes",
      "section_id": "binary-fields-with-node-redis-5-x"
    },
    {
      "id": "the-catalog-builder-ex0",
      "language": "javascript",
      "code": "import { LocalEmbedder } from \"./embeddings.js\";\n\nconst CATALOG = [\n  { id: \"p001\", name: \"Alpine down parka\",\n    description: \"Heavyweight 800-fill goose down parka...\",\n    category: \"outerwear\", brand: \"northpeak\",\n    price: 289.00, in_stock: true, rating: 4.7 },\n  // ... rest of the catalog ...\n];\n\nconst embedder = new LocalEmbedder();\nconst vectors = await embedder.encodeMany(\n  CATALOG.map(p => `${p.name}. ${p.description}`),\n);\n// Each vector is 384 float32s, packed and written into catalog.json\n// alongside the structured fields. The demo server reads that file at\n// startup and HSETs every product into Redis.",
      "section_id": "the-catalog-builder"
    },
    {
      "id": "run-the-demo-locally-ex0",
      "language": "bash",
      "code": "git clone https://github.com/redis/docs.git\n    cd docs/content/develop/use-cases/recommendation-engine/nodejs",
      "section_id": "run-the-demo-locally"
    },
    {
      "id": "run-the-demo-locally-ex1",
      "language": "bash",
      "code": "npm install",
      "section_id": "run-the-demo-locally"
    },
    {
      "id": "run-the-demo-locally-ex2",
      "language": "bash",
      "code": "node buildCatalog.js",
      "section_id": "run-the-demo-locally"
    },
    {
      "id": "run-the-demo-locally-ex3",
      "language": "bash",
      "code": "node demoServer.js",
      "section_id": "run-the-demo-locally"
    }
  ]
}
