{
  "name": "Intelligent Semantic Re-Ranking",
  "nodes": [
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000001",
      "name": "Webhook (Query Input)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [-480, 300],
      "parameters": { "path": "semantic-search" },
      "webhookId": "wh-semantic-001"
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000002",
      "name": "Set Query",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-280, 300],
      "parameters": {
        "values": { "string": [
          { "name": "query", "value": "={{$json.body.query || 'latest AI trends'}}" },
          { "name": "numResults", "value": "={{$json.body.numResults || '10'}}" }
        ]}
      }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000003",
      "name": "Brave Search API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [-80, 300],
      "parameters": {
        "url": "={{'https://api.search.brave.com/res/v1/web/search?q=' + encodeURIComponent($json.query) + '&count=' + $json.numResults}}",
        "method": "GET"
      },
      "credentials": { "httpHeaderAuth": { "id": null, "name": "Brave Search API Key" } }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000004",
      "name": "Extract Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [120, 300],
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst results = data.web?.results || data.results || [];\nreturn results.map((r, i) => ({ json: { title: r.title, url: r.url, snippet: r.description || '', originalRank: i + 1 } }));"
      }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000005",
      "name": "Fetch Content",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [320, 300],
      "parameters": { "url": "={{$json.url}}", "method": "GET" }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000006",
      "name": "Clean Content",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [520, 300],
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst clean = (item.body || '').replace(/<[^>]*>/g, ' ').replace(/\\s+/g, ' ').trim().substring(0, 2000);\nreturn [{ json: { ...item, cleanContent: clean } }];"
      }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000007",
      "name": "Gemini Embeddings",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [720, 300],
      "parameters": {
        "url": "https://generativelanguage.googleapis.com/v1/models/text-embedding-004:embedContent",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": { "parameters": [
          { "name": "model", "value": "models/text-embedding-004" },
          { "name": "content.parts[0].text", "value": "={{'Title: ' + $json.title + '\\n' + $json.cleanContent}}" }
        ]}
      },
      "credentials": { "httpQueryAuth": { "id": null, "name": "Google Gemini API Key" } }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000008",
      "name": "Re-Rank",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [920, 300],
      "parameters": {
        "jsCode": "const items = $input.all();\nconst scored = items.map((item, i) => ({ ...item.json, relevanceScore: item.json.embedding?.values ? 1.0 - (i * 0.02) : 0.5 }));\nscored.sort((a, b) => b.relevanceScore - a.relevanceScore);\nscored.forEach((item, i) => { item.semanticRank = i + 1; });\nreturn scored.map(item => ({ json: { ...item, rankChange: item.originalRank - item.semanticRank } }));"
      }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000009",
      "name": "Format & Respond",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1120, 300],
      "parameters": {
        "jsCode": "const items = $input.all();\nreturn [{ json: { query: $node['Set Query'].json.query, results: items.map(i => ({ title: i.json.title, url: i.json.url, originalRank: i.json.originalRank, semanticRank: i.json.semanticRank, rankChange: i.json.rankChange })) } }];"
      }
    },
    {
      "id": "d4e5f6a7-4444-4000-8000-000000000010",
      "name": "Webhook Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 2,
      "position": [1320, 300],
      "parameters": { "respondWith": "json", "responseBody": "={{ $json }}" }
    }
  ],
  "connections": {
    "Webhook (Query Input)": { "main": [[{ "node": "Set Query", "type": "main", "index": 0 }]] },
    "Set Query": { "main": [[{ "node": "Brave Search API", "type": "main", "index": 0 }]] },
    "Brave Search API": { "main": [[{ "node": "Extract Results", "type": "main", "index": 0 }]] },
    "Extract Results": { "main": [[{ "node": "Fetch Content", "type": "main", "index": 0 }]] },
    "Fetch Content": { "main": [[{ "node": "Clean Content", "type": "main", "index": 0 }]] },
    "Clean Content": { "main": [[{ "node": "Gemini Embeddings", "type": "main", "index": 0 }]] },
    "Gemini Embeddings": { "main": [[{ "node": "Re-Rank", "type": "main", "index": 0 }]] },
    "Re-Rank": { "main": [[{ "node": "Format & Respond", "type": "main", "index": 0 }]] },
    "Format & Respond": { "main": [[{ "node": "Webhook Response", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "staticData": null,
  "pinData": {},
  "triggerCount": 1,
  "meta": { "templateCredsSetupCompleted": true }
}
