{
  "id": "data-modeling",
  "title": "Data modeling for search",
  "url": "https://redis.io/docs/latest/develop/get-started/search-tutorial/data-modeling/",
  "summary": "Learn how to store records in Redis so they can be searched, and when to choose hashes versus JSON documents.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-08-03T12:11:45-07:00",
  "page_type": "content",
  "content_hash": "e5c2048c1841811fa9ae859118a582cf955bb0ab0553ba3e0fab1cd62e497c8e",
  "sections": [
    {
      "id": "a-record-as-a-hash",
      "title": "A record as a hash",
      "role": "content",
      "text": "A [hash](https://redis.io/docs/latest/develop/data-types/hashes) stores a flat set of field-value pairs under a single key. It is the simplest way to represent a record and maps neatly onto a row of fields:\n\nFoundational: Store a record as a hash with HSET when your data is a flat set of fields\n\n**Difficulty:** Beginner\n\n**Commands:** HSET\n\n**Complexity:**\n- HSET: O(1)\n\n**Available in:** Redis CLI, Python\n\n##### Redis CLI\n\n[code example]\n\n##### Python\n\n[code example]\n\n\n\nHashes are compact and fast, but they are **flat**: every value is a string or number. There is no natural place to put a nested object (like a `specs` sub-record) or a list of values (like multiple `features`) without flattening or encoding it yourself."
    },
    {
      "id": "a-record-as-a-json-document",
      "title": "A record as a JSON document",
      "role": "content",
      "text": "The [JSON](https://redis.io/docs/latest/develop/data-types/json) data type stores a full JSON document under a key. It can represent nested objects and arrays directly, which matches how application data usually looks:\n\nFoundational: Store a record as a JSON document with JSON.SET when your data has nested objects or arrays\n\n**Difficulty:** Beginner\n\n**Commands:** JSON.SET\n\n**Complexity:**\n- JSON.SET: O(M+N)\n\n**Available in:** Redis CLI, Python\n\n##### Redis CLI\n\n[code example]\n\n##### Python\n\n[code example]\n\n\n\nNotice that `features` is a real array and `specs` is a real nested object. You did not have to flatten them."
    },
    {
      "id": "which-should-you-use",
      "title": "Which should you use?",
      "role": "content",
      "text": "Both hashes and JSON documents can be indexed and searched by Redis Search, so you can search either one. Use this as a guide:\n\n| Choose **hashes** when... | Choose **JSON** when... |\n| --- | --- |\n| Your records are flat (no nesting). | Your records have nested objects or arrays. |\n| You want the smallest possible memory footprint. | You want your stored shape to match your application objects. |\n| You frequently update individual fields. | You want to read, update, or index nested paths directly. |\n\nFor this tutorial, the catalog records have arrays (`features`) and a nested object (`specs`), so **we will use JSON documents** for the rest of the tutorial. If you are coming from a background where every record is a flat row, JSON is also a gentle way to keep your existing object shapes.\n\n\nThis is a modeling choice, not a limitation. The indexing and query commands you will learn (`FT.CREATE`, `FT.SEARCH`, `FT.AGGREGATE`) work with both hashes and JSON. The main practical difference shows up when indexing arrays, which you will see on the [next page](https://redis.io/docs/latest/develop/get-started/search-tutorial/indexing)."
    },
    {
      "id": "load-the-dataset",
      "title": "Load the dataset",
      "role": "content",
      "text": "Now load the full catalog of 12 products. Each product is stored as a JSON document under a key with the prefix `product:`. The key prefix matters: in the next step you will tell Redis to index every key that starts with `product:`.\n\nFoundational: Load the tutorial dataset as JSON documents under a shared key prefix using JSON.SET\n\n**Difficulty:** Beginner\n\n**Commands:** JSON.SET\n\n**Complexity:**\n- JSON.SET: O(M+N)\n\n**Available in:** Redis CLI, Python\n\n##### Redis CLI\n\n[code example]\n\n##### Python\n\n[code example]\n\n\n\nYou can read any single document back by its key with [JSON.GET](https://redis.io/docs/latest/commands/json.get):\n\nFoundational: Read one JSON document back by its key with JSON.GET\n\n**Difficulty:** Beginner\n\n**Commands:** JSON.GET\n\n**Complexity:**\n- JSON.GET: O(N)\n\n**Available in:** Redis CLI, Python\n\n##### Redis CLI\n\n[code example]\n\n##### Python\n\n[code example]\n\n\n\nAt this point the data is in Redis, but you can only fetch it one key at a time. To *search* across all products, you need an index."
    },
    {
      "id": "next-steps",
      "title": "Next steps",
      "role": "content",
      "text": "Continue to [creating an index](https://redis.io/docs/latest/develop/get-started/search-tutorial/indexing) to make this data searchable."
    }
  ],
  "examples": [
    {
      "id": "a-record-as-a-hash-ex0",
      "language": "plaintext",
      "code": "> HSET product:1 name \"Aurora AcousticPro Headphones\" brand \"Aurora\" category \"Audio\" price 199.99 rating 4.6\n(integer) 5",
      "section_id": "a-record-as-a-hash"
    },
    {
      "id": "a-record-as-a-hash-ex1",
      "language": "python",
      "code": "r.hset(\n    \"product:1\",\n    mapping={\n        \"name\": \"Aurora AcousticPro Headphones\",\n        \"brand\": \"Aurora\",\n        \"category\": \"Audio\",\n        \"price\": 199.99,\n        \"rating\": 4.6,\n    },\n)\n# >>> 5",
      "section_id": "a-record-as-a-hash"
    },
    {
      "id": "a-record-as-a-json-document-ex0",
      "language": "plaintext",
      "code": "> JSON.SET product:1 $ '{\"name\":\"Aurora AcousticPro Headphones\",\"brand\":\"Aurora\",\"category\":\"Audio\",\"price\":199.99,\"rating\":4.6,\"features\":[\"wireless\",\"noise-cancelling\",\"bluetooth\"],\"specs\":{\"color\":\"midnight black\",\"weight_grams\":268}}'\nOK",
      "section_id": "a-record-as-a-json-document"
    },
    {
      "id": "a-record-as-a-json-document-ex1",
      "language": "python",
      "code": "r.json().set(\n    \"product:1\",\n    Path.root_path(),\n    {\n        \"name\": \"Aurora AcousticPro Headphones\",\n        \"brand\": \"Aurora\",\n        \"category\": \"Audio\",\n        \"price\": 199.99,\n        \"rating\": 4.6,\n        \"features\": [\"wireless\", \"noise-cancelling\", \"bluetooth\"],\n        \"specs\": {\"color\": \"midnight black\", \"weight_grams\": 268},\n    },\n)\n# >>> True",
      "section_id": "a-record-as-a-json-document"
    },
    {
      "id": "load-the-dataset-ex0",
      "language": "plaintext",
      "code": "> JSON.SET product:1 $ '{\"name\":\"Aurora AcousticPro Headphones\",\"brand\":\"Aurora\",\"category\":\"Audio\",\"description\":\"Over-ear wireless headphones with active noise cancelling and a 40-hour battery. Plush memory-foam earcups and a lightweight frame make them comfortable for all-day listening, whether you are commuting, working, or relaxing at home.\",\"price\":199.99,\"rating\":4.6,\"review_count\":1284,\"stock\":42,\"release_year\":2024,\"features\":[\"wireless\",\"noise-cancelling\",\"bluetooth\",\"over-ear\"],\"specs\":{\"color\":\"midnight black\",\"weight_grams\":268,\"warranty_years\":2}}'\n... output truncated for AI-facing Markdown ...\n> JSON.SET product:12 $ '{\"name\":\"Vista Action Cam 4K\",\"brand\":\"Vista\",\"category\":\"Cameras\",\"description\":\"A pocket-sized action camera that shoots stabilized 4K video and is waterproof without a case. Mount it on a helmet or bike and capture your adventures in sharp detail.\",\"price\":299.0,\"rating\":4.3,\"review_count\":455,\"stock\":33,\"release_year\":2023,\"features\":[\"camera\",\"4k\",\"waterproof\",\"wifi\"],\"specs\":{\"color\":\"black\",\"weight_grams\":128,\"warranty_years\":1}}'\nOK",
      "section_id": "load-the-dataset"
    },
    {
      "id": "load-the-dataset-ex1",
      "language": "python",
      "code": "catalog = [\n    {\n        \"name\": \"Aurora AcousticPro Headphones\",\n        \"brand\": \"Aurora\",\n        \"category\": \"Audio\",\n        \"description\": (\n            \"Over-ear wireless headphones with active noise cancelling and a \"\n            \"40-hour battery. Plush memory-foam earcups and a lightweight frame \"\n            \"make them comfortable for all-day listening, whether you are \"\n            \"commuting, working, or relaxing at home.\"\n        ),\n        \"price\": 199.99,\n        \"rating\": 4.6,\n        \"review_count\": 1284,\n        \"stock\": 42,\n        \"release_year\": 2024,\n        \"features\": [\"wireless\", \"noise-cancelling\", \"bluetooth\", \"over-ear\"],\n        \"specs\": {\"color\": \"midnight black\", \"weight_grams\": 268, \"warranty_years\": 2},\n    },\n    {\n        \"name\": \"Aurora BudsMini Earbuds\",\n        \"brand\": \"Aurora\",\n        \"category\": \"Audio\",\n        \"description\": (\n            \"Tiny true-wireless earbuds with a secure in-ear fit and sweat \"\n            \"resistance for workouts. The compact charging case slips into a \"\n            \"pocket and delivers three full recharges on the go.\"\n        ),\n        \"price\": 89.99,\n        \"rating\": 4.3,\n        \"review_count\": 942,\n        \"stock\": 130,\n        \"release_year\": 2023,\n        \"features\": [\"wireless\", \"bluetooth\", \"in-ear\", \"water-resistant\"],\n        \"specs\": {\"color\": \"pearl white\", \"weight_grams\": 5, \"warranty_years\": 1},\n    },\n    {\n        \"name\": \"Sonus Boom Portable Speaker\",\n        \"brand\": \"Sonus\",\n        \"category\": \"Audio\",\n        \"description\": (\n            \"A rugged portable Bluetooth speaker with deep bass and a waterproof \"\n            \"shell. Toss it in a bag for the beach or a campsite and enjoy \"\n            \"room-filling sound for up to 20 hours per charge.\"\n        ),\n        \"price\": 129.5,\n        \"rating\": 4.5,\n        \"review_count\": 512,\n        \"stock\": 64,\n        \"release_year\": 2024,\n        \"features\": [\"wireless\", \"bluetooth\", \"portable\", \"waterproof\"],\n        \"specs\": {\"color\": \"slate gray\", \"weight_grams\": 540, \"warranty_years\": 1},\n    },\n    {\n        \"name\": \"Pixma Vortex 15 Laptop\",\n        \"brand\": \"Pixma\",\n        \"category\": \"Computers\",\n        \"description\": (\n            \"A thin-and-light 15-inch laptop with a fast multi-core processor, \"\n            \"16 GB of memory, and a speedy solid-state drive. The backlit keyboard \"\n            \"and bright display make it a capable companion for work and study.\"\n        ),\n        \"price\": 1399.0,\n        \"rating\": 4.7,\n        \"review_count\": 318,\n        \"stock\": 18,\n        \"release_year\": 2024,\n        \"features\": [\"laptop\", \"ssd\", \"backlit-keyboard\", \"lightweight\"],\n        \"specs\": {\"color\": \"space silver\", \"weight_grams\": 1600, \"warranty_years\": 2},\n    },\n    {\n        \"name\": \"Pixma UltraView 27 Monitor\",\n        \"brand\": \"Pixma\",\n        \"category\": \"Computers\",\n        \"description\": (\n            \"A 27-inch 4K monitor with an IPS panel for accurate colors and wide \"\n            \"viewing angles. A single USB-C cable carries video and power, keeping \"\n            \"your desk tidy.\"\n        ),\n        \"price\": 329.99,\n        \"rating\": 4.4,\n        \"review_count\": 221,\n        \"stock\": 27,\n        \"release_year\": 2023,\n        \"features\": [\"monitor\", \"4k\", \"ips\", \"usb-c\"],\n        \"specs\": {\"color\": \"black\", \"weight_grams\": 5200, \"warranty_years\": 3},\n    },\n    {\n        \"name\": \"Clackr Mechanical Keyboard\",\n        \"brand\": \"Clackr\",\n        \"category\": \"Accessories\",\n        \"description\": (\n            \"A compact mechanical keyboard with tactile switches, per-key RGB \"\n            \"lighting, and wireless connectivity. Hot-swappable switches let you \"\n            \"tune the typing feel without soldering.\"\n        ),\n        \"price\": 119.0,\n        \"rating\": 4.8,\n        \"review_count\": 1502,\n        \"stock\": 88,\n        \"release_year\": 2024,\n        \"features\": [\"keyboard\", \"mechanical\", \"rgb\", \"wireless\"],\n        \"specs\": {\"color\": \"graphite\", \"weight_grams\": 720, \"warranty_years\": 2},\n    },\n    {\n        \"name\": \"Glide Pro Wireless Mouse\",\n        \"brand\": \"Glide\",\n        \"category\": \"Accessories\",\n        \"description\": (\n            \"An ergonomic wireless mouse with a high-precision sensor and a \"\n            \"contoured shape that reduces wrist strain. A single charge lasts for \"\n            \"weeks of everyday use.\"\n        ),\n        \"price\": 59.99,\n        \"rating\": 4.2,\n        \"review_count\": 869,\n        \"stock\": 150,\n        \"release_year\": 2022,\n        \"features\": [\"mouse\", \"wireless\", \"ergonomic\"],\n        \"specs\": {\"color\": \"charcoal\", \"weight_grams\": 98, \"warranty_years\": 1},\n    },\n    {\n        \"name\": \"Pulse Series 6 Smartwatch\",\n        \"brand\": \"Pulse\",\n        \"category\": \"Wearables\",\n        \"description\": (\n            \"A sleek smartwatch with built-in GPS, continuous heart-rate \"\n            \"monitoring, and water resistance for swimming. Track workouts, sleep, \"\n            \"and notifications from your wrist.\"\n        ),\n        \"price\": 249.0,\n        \"rating\": 4.5,\n        \"review_count\": 1733,\n        \"stock\": 51,\n        \"release_year\": 2024,\n        \"features\": [\"smartwatch\", \"gps\", \"heart-rate\", \"water-resistant\"],\n        \"specs\": {\"color\": \"rose gold\", \"weight_grams\": 38, \"warranty_years\": 1},\n    },\n    {\n        \"name\": \"Pulse Band Fitness Tracker\",\n        \"brand\": \"Pulse\",\n        \"category\": \"Wearables\",\n        \"description\": (\n            \"A lightweight fitness band that tracks steps, heart rate, and sleep \"\n            \"stages. The slim screen shows daily progress and the battery lasts a \"\n            \"full week between charges.\"\n        ),\n        \"price\": 79.99,\n        \"rating\": 4.1,\n        \"review_count\": 2210,\n        \"stock\": 200,\n        \"release_year\": 2023,\n        \"features\": [\"fitness-tracker\", \"heart-rate\", \"sleep-tracking\"],\n        \"specs\": {\"color\": \"ocean blue\", \"weight_grams\": 24, \"warranty_years\": 1},\n    },\n    {\n        \"name\": \"Lumi Glow Smart Bulb\",\n        \"brand\": \"Lumi\",\n        \"category\": \"Home\",\n        \"description\": (\n            \"A color-changing smart bulb that connects over Wi-Fi and works with \"\n            \"voice assistants. Dim it for movie night or set a warm white for \"\n            \"reading, all from your phone.\"\n        ),\n        \"price\": 24.99,\n        \"rating\": 4.0,\n        \"review_count\": 640,\n        \"stock\": 320,\n        \"release_year\": 2022,\n        \"features\": [\"smart-home\", \"wifi\", \"dimmable\", \"color\"],\n        \"specs\": {\"color\": \"white\", \"weight_grams\": 70, \"warranty_years\": 2},\n    },\n    {\n        \"name\": \"Lumi Climate Smart Thermostat\",\n        \"brand\": \"Lumi\",\n        \"category\": \"Home\",\n        \"description\": (\n            \"A learning smart thermostat that adjusts heating and cooling to your \"\n            \"routine and helps lower energy bills. The crisp display and Wi-Fi app \"\n            \"make scheduling effortless.\"\n        ),\n        \"price\": 149.0,\n        \"rating\": 4.6,\n        \"review_count\": 388,\n        \"stock\": 75,\n        \"release_year\": 2024,\n        \"features\": [\"smart-home\", \"wifi\", \"energy-saving\"],\n        \"specs\": {\"color\": \"white\", \"weight_grams\": 210, \"warranty_years\": 3},\n    },\n    {\n        \"name\": \"Vista Action Cam 4K\",\n        \"brand\": \"Vista\",\n        \"category\": \"Cameras\",\n        \"description\": (\n            \"A pocket-sized action camera that shoots stabilized 4K video and is \"\n            \"waterproof without a case. Mount it on a helmet or bike and capture \"\n            \"your adventures in sharp detail.\"\n        ),\n        \"price\": 299.0,\n        \"rating\": 4.3,\n        \"review_count\": 455,\n        \"stock\": 33,\n        \"release_year\": 2023,\n        \"features\": [\"camera\", \"4k\", \"waterproof\", \"wifi\"],\n        \"specs\": {\"color\": \"black\", \"weight_grams\": 128, \"warranty_years\": 1},\n    },\n]\n\nfor product_id, product in enumerate(catalog, start=1):\n    r.json().set(f\"product:{product_id}\", Path.root_path(), product)",
      "section_id": "load-the-dataset"
    },
    {
      "id": "load-the-dataset-ex2",
      "language": "plaintext",
      "code": "> JSON.GET product:1 $.name\n\"[\\\"Aurora AcousticPro Headphones\\\"]\"",
      "section_id": "load-the-dataset"
    },
    {
      "id": "load-the-dataset-ex3",
      "language": "python",
      "code": "res = r.json().get(\"product:1\", \"$.name\")\nprint(res)  # >>> ['Aurora AcousticPro Headphones']",
      "section_id": "load-the-dataset"
    }
  ]
}
