8
n8n 中文网amn8n.com

基于Elasticsearch的动态搜索界面与自动化报告生成

中级

这是一个Document Extraction, Multimodal AI领域的自动化工作流,包含 11 个节点。主要使用 Code, FormTrigger, HttpRequest, ReadWriteFile 等节点。 使用Elasticsearch的动态搜索界面和自动化报告生成

前置要求
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "3eqTGbhtSxDjuMZC",
  "meta": {
    "instanceId": "5cee0adb1ef2b84ac8a86937fac5115d710898b6c70f9f7c3f3ca3ef70a11bf7",
    "templateCredsSetupCompleted": true
  },
  "name": "基于 Elasticsearch 的动态搜索界面与自动化报告生成",
  "tags": [],
  "nodes": [
    {
      "id": "e3eb30ef-51d9-4cd5-ab1f-807b87e06795",
      "name": "搜索表单",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        480,
        -220
      ],
      "webhookId": "169f8e1c-e4a8-4229-9b2a-9db33003f221",
      "parameters": {
        "path": "169f8e1c-e4a8-4229-9b2a-9db33003f221",
        "options": {},
        "formTitle": "🔍 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": "构建搜索查询",
      "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": "搜索 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": "格式化报告",
      "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": "从磁盘读取/写入文件",
      "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": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        -260
      ],
      "parameters": {
        "width": 360,
        "height": 220,
        "content": "## 🎯 入口点"
      },
      "typeVersion": 1
    },
    {
      "id": "b36fea54-e177-4edc-a091-110b25475ad4",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        0
      ],
      "parameters": {
        "color": 2,
        "width": 360,
        "height": 300,
        "content": "## 🔧 查询构建器"
      },
      "typeVersion": 1
    },
    {
      "id": "8d3f38d7-b483-43cb-bcb5-9524a7a17e86",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        0
      ],
      "parameters": {
        "color": 3,
        "width": 320,
        "height": 260,
        "content": "## 🎯 数据猎手"
      },
      "typeVersion": 1
    },
    {
      "id": "11039177-def0-4de1-ad25-f91e1294abee",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 340,
        "height": 340,
        "content": "## 📝 报告生成器"
      },
      "typeVersion": 1
    },
    {
      "id": "7c8c7226-a9e3-4edb-9497-e1980f3e84ea",
      "name": "便签 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1180,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 400,
        "height": 260,
        "content": "## 📁 文件保存器"
      },
      "typeVersion": 1
    },
    {
      "id": "79d96536-a117-474b-a524-ec0e54f7ae2b",
      "name": "便签 5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        -520
      ],
      "parameters": {
        "color": 6,
        "width": 720,
        "height": 200,
        "content": "## 🎯 动态搜索流水线"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "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
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 文档提取, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量11
分类2
节点类型5
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
DataMinex

DataMinex

@dataminex

Smart Connection Analysis from Open Data, Globally at Scale

外部链接
在 n8n.io 查看

分享此工作流