{
  "id": "3eqTGbhtSxDjuMZC",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Dynamic Search Interface with Elasticsearch and Automated Report Generation",
  "tags": [],
  "nodes": [
    {
      "id": "e3eb30ef-51d9-4cd5-ab1f-807b87e06795",
      "name": "Search Form",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        480,
        -220
      ],
      "parameters": {
        "path": "169f8e1c-e4a8-4229-9b2a-9db33003f221",
        "options": {},
        "formTitle": "\ud83d\udd0d Dynamic Search",
        "formFields": {
          "values": [
            {
              "fieldType": "number",
              "fieldLabel": "Minimum Amount ($)"
            },
            {
              "fieldType": "dropdown",
              "fieldLabel": "Time Range",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Last 1 Hour"
                  },
                  {
                    "option": "Last 6 Hours"
                  },
                  {
                    "option": "Last 24 Hours"
                  },
                  {
                    "option": "Last 3 Days"
                  }
                ]
              },
              "requiredField": true
            },
            {
              "fieldLabel": "Customer ID (Optional)"
            },
            {
              "fieldType": "dropdown",
              "fieldLabel": "Report Format",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Text Report"
                  },
                  {
                    "option": "CSV Export"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        },
        "responseMode": "responseNode",
        "formDescription": "Search for suspicious transactions in your banking database"
      },
      "typeVersion": 2.1
    },
    {
      "id": "79e2a04d-9427-4d8d-ae8a-41005a2ab80d",
      "name": "Build Search Query",
      "type": "n8n-nodes-base.code",
      "position": [
        700,
        -220
      ],
      "parameters": {
        "jsCode": "// Process form input and build Elasticsearch query\nconst formData = $input.first().json;\n\n// Extract form values\nconst minAmount = formData['Minimum Amount ($)'] || 1000;\nconst timeRange = formData['Time Range'] || 'Last 24 Hours';\nconst customerId = formData['Customer ID (Optional)']?.trim() || null;\nconst reportFormat = formData['Report Format'] || 'Text Report';\n\n// Convert time range to Elasticsearch format\nlet timeRangeES;\nswitch(timeRange) {\n  case 'Last 1 Hour':\n    timeRangeES = 'now-1h';\n    break;\n  case 'Last 6 Hours':\n    timeRangeES = 'now-6h';\n    break;\n  case 'Last 24 Hours':\n    timeRangeES = 'now-24h';\n    break;\n  case 'Last 3 Days':\n    timeRangeES = 'now-3d';\n    break;\n  default:\n    timeRangeES = 'now-24h';\n}\n\n// Build Elasticsearch query\nconst mustConditions = [\n  {\n    \"range\": {\n      \"timestamp\": {\n        \"gte\": timeRangeES\n      }\n    }\n  },\n  {\n    \"range\": {\n      \"amount\": {\n        \"gte\": minAmount\n      }\n    }\n  }\n];\n\n// Add customer filter if provided\nif (customerId) {\n  mustConditions.push({\n    \"term\": {\n      \"customer_id.keyword\": customerId\n    }\n  });\n}\n\nconst esQuery = {\n  \"query\": {\n    \"bool\": {\n      \"must\": mustConditions\n    }\n  },\n  \"sort\": [{ \"timestamp\": { \"order\": \"desc\" } }],\n  \"size\": 100\n};\n\nreturn {\n  elasticsearchQuery: esQuery,\n  searchParams: {\n    minAmount,\n    timeRange,\n    customerId,\n    reportFormat\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "62e186d7-c90f-4576-8c56-d850e784e9e0",
      "name": "Search Elasticsearch",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        940,
        -220
      ],
      "parameters": {
        "url": "https://localhost:9220/bank_transactions/_search",
        "options": {
          "allowUnauthorizedCerts": true
        },
        "jsonBody": "={{ $json.elasticsearchQuery }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth"
      },
      "typeVersion": 4.1
    },
    {
      "id": "4869c996-8a2e-40a1-aea3-384e0e379c14",
      "name": "Format Report",
      "type": "n8n-nodes-base.code",
      "position": [
        1140,
        -220
      ],
      "parameters": {
        "jsCode": "// Get data from previous nodes\nconst esResponse = $input.first().json;\nconst searchParams = $('Build Search Query').first().json.searchParams;\n\n// Extract results\nconst hits = esResponse.hits?.hits || [];\nconst totalFound = esResponse.hits?.total?.value || 0;\n\n// Generate filename\nconst timestamp = new Date().toISOString().split('T')[0];\nconst isCSV = searchParams.reportFormat === 'CSV Export';\nconst filename = `report_${timestamp}.${isCSV ? 'csv' : 'txt'}`;\nconst mimeType = isCSV ? 'text/csv' : 'text/plain';\n\n// Generate report content\nlet reportContent = '';\n\nif (isCSV) {\n  // CSV format\n  reportContent = 'Transaction_ID,Customer_ID,Amount,Merchant_Category,Timestamp\\n';\n  hits.forEach(hit => {\n    const t = hit._source || {};\n    reportContent += `\"${t.transaction_id || ''}\",\"${t.customer_id || ''}\",${t.amount || 0},\"${t.merchant_category || ''}\",\"${t.timestamp || ''}\"\\n`;\n  });\n} else {\n  // Text format\n  reportContent = `DYNAMIC SEARCH REPORT\\n`;\n  reportContent += `======================\\n\\n`;\n  reportContent += `Search Criteria:\\n`;\n  reportContent += `- Minimum Amount: $${searchParams.minAmount}\\n`;\n  reportContent += `- Time Range: ${searchParams.timeRange}\\n`;\n  reportContent += `- Customer: ${searchParams.customerId || 'All'}\\n\\n`;\n  reportContent += `Results: ${totalFound} transactions found\\n\\n`;\n  \n  if (hits.length > 0) {\n    reportContent += `TRANSACTIONS:\\n`;\n    reportContent += `=============\\n\\n`;\n    hits.forEach((hit, index) => {\n      const t = hit._source || {};\n      reportContent += `${index + 1}. Transaction ID: ${t.transaction_id}\\n`;\n      reportContent += `   Customer: ${t.customer_id}\\n`;\n      reportContent += `   Amount: $${t.amount}\\n`;\n      reportContent += `   Merchant: ${t.merchant_category}\\n`;\n      reportContent += `   Time: ${t.timestamp}\\n\\n`;\n    });\n  } else {\n    reportContent += `No suspicious transactions found matching your criteria.\\n`;\n  }\n}\n\n// Convert content to binary data\nconst binaryData = Buffer.from(reportContent, 'utf8');\n\nreturn {\n  json: {\n    filename: filename,\n    mimeType: mimeType,\n    content: reportContent\n  },\n  binary: {\n    data: binaryData\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "764ac1c5-0ae2-4048-884b-dceb4ea6d719",
      "name": "Read/Write Files from Disk",
      "type": "n8n-nodes-base.readWriteFile",
      "position": [
        1320,
        -220
      ],
      "parameters": {
        "options": {},
        "fileName": "=/tmp/{{ $json.filename }}",
        "operation": "write"
      },
      "typeVersion": 1
    },
    {
      "id": "b4c9ae5c-cd17-492e-a543-205329a872b9",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        -260
      ],
      "parameters": {
        "width": 360,
        "height": 220,
        "content": "## \ud83c\udfaf ENTRY POINT\nUser fills out dynamic search form with:\n- Minimum transaction amount\n- Time range (1hr to 3 days)\n- Optional customer ID filter\n- Output format (Text/CSV)\n\n\ud83d\udca1 TIP: Form auto-validates required fields!"
      },
      "typeVersion": 1
    },
    {
      "id": "b36fea54-e177-4edc-a091-110b25475ad4",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        0
      ],
      "parameters": {
        "color": 2,
        "width": 360,
        "height": 300,
        "content": "## \ud83d\udd27 QUERY BUILDER\nTransforms user input into Elasticsearch query:\n\n\u2705 Converts time ranges:\n   \"Last 24 Hours\" \u2192 \"now-24h\"\n   \"Last 3 Days\" \u2192 \"now-3d\"\n\n\u2705 Builds filters:\n   \u2022 Amount >= minimum\n   \u2022 Timestamp within range\n   \u2022 Customer ID (if provided)\n\n\ud83d\udcca Output: Ready-to-use ES query + metadata"
      },
      "typeVersion": 1
    },
    {
      "id": "8d3f38d7-b483-43cb-bcb5-9524a7a17e86",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        0
      ],
      "parameters": {
        "color": 3,
        "width": 320,
        "height": 260,
        "content": "## \ud83c\udfaf DATA HUNTER\nSearches bank_transactions index for suspicious activity\n\n\ud83d\udd27 Configuration:\n   \u2022 POST to localhost:9220\n   \u2022 Uses HTTP Basic Auth\n   \u2022 Sends JSON query from previous node\n   \u2022 Returns max 100 results, newest first\n\n\u26a0\ufe0f Note: Requires valid ES credentials!"
      },
      "typeVersion": 1
    },
    {
      "id": "11039177-def0-4de1-ad25-f91e1294abee",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 340,
        "height": 340,
        "content": "## \ud83d\udcdd REPORT GENERATOR\nTransforms raw ES data into user-friendly reports\n\n\ud83c\udfa8 Creates two formats:\n   \ud83d\udcc4 TEXT: Human-readable summary\n   \ud83d\udcca CSV: Spreadsheet-ready data export\n\n\u2728 Features:\n   \u2022 Auto-generates timestamped filename\n   \u2022 Converts text to binary for file writing\n   \u2022 Includes search criteria summary\n   \u2022 Lists all suspicious transactions\n\n\ud83d\udd04 Output: Content + Binary data ready for file save"
      },
      "typeVersion": 1
    },
    {
      "id": "7c8c7226-a9e3-4edb-9497-e1980f3e84ea",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1180,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 400,
        "height": 260,
        "content": "## \ud83d\udcc1 FILE SAVER\nWrites the report to server disk\n\n\ud83d\udccd Location: /tmp/report_YYYY-MM-DD.{txt|csv}\n\ud83d\udd27 Uses binary data from Format Report node\n\u2705 File ready for download or further processing\n\n\ud83c\udfaf Perfect for:\n   \u2022 Audit trails\n   \u2022 Batch processing\n   \u2022 Compliance reporting"
      },
      "typeVersion": 1
    },
    {
      "id": "79d96536-a117-474b-a524-ec0e54f7ae2b",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        -520
      ],
      "parameters": {
        "color": 6,
        "width": 720,
        "height": 200,
        "content": "## \ud83c\udfaf DYNAMIC SEARCH PIPELINE\n\nUser Form \u2192 Query Builder \u2192 ES Search \u2192 Report Format \u2192 File Save\n\n\u23f1\ufe0f Typical execution: 2-5 seconds\n\ud83d\udcca Handles up to 100 suspicious transactions\n\ud83d\udd12 Secure: Uses authenticated ES connection\n\ud83d\udcc1 Output: Timestamped report files in /tmp/"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "42705d97-c55a-4ebe-8107-536a8c7faea3",
  "connections": {
    "Search Form": {
      "main": [
        [
          {
            "node": "Build Search Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Report": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Search Query": {
      "main": [
        [
          {
            "node": "Search Elasticsearch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Elasticsearch": {
      "main": [
        [
          {
            "node": "Format Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}