|
| 1 | +# Multi-Backend Configuration Example |
| 2 | + |
| 3 | +This example demonstrates how to configure and use the multi-backend retrieval architecture in NLWebNet. |
| 4 | + |
| 5 | +## Single Backend (Default/Legacy) |
| 6 | + |
| 7 | +```csharp |
| 8 | +// Traditional single backend setup - still works |
| 9 | +services.AddNLWebNet<MockDataBackend>(options => |
| 10 | +{ |
| 11 | + options.DefaultMode = QueryMode.List; |
| 12 | + options.MaxResultsPerQuery = 20; |
| 13 | +}); |
| 14 | +``` |
| 15 | + |
| 16 | +## Multi-Backend Configuration |
| 17 | + |
| 18 | +```csharp |
| 19 | +// New multi-backend setup |
| 20 | +services.AddNLWebNetMultiBackend( |
| 21 | + options => |
| 22 | + { |
| 23 | + options.DefaultMode = QueryMode.List; |
| 24 | + options.MaxResultsPerQuery = 50; |
| 25 | + }, |
| 26 | + multiBackendOptions => |
| 27 | + { |
| 28 | + multiBackendOptions.Enabled = true; |
| 29 | + multiBackendOptions.EnableParallelQuerying = true; |
| 30 | + multiBackendOptions.EnableResultDeduplication = true; |
| 31 | + multiBackendOptions.MaxConcurrentQueries = 3; |
| 32 | + multiBackendOptions.BackendTimeoutSeconds = 30; |
| 33 | + multiBackendOptions.WriteEndpoint = "primary_backend"; |
| 34 | + }); |
| 35 | +``` |
| 36 | + |
| 37 | +## Configuration via appsettings.json |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "NLWebNet": { |
| 42 | + "DefaultMode": "List", |
| 43 | + "MaxResultsPerQuery": 50, |
| 44 | + "MultiBackend": { |
| 45 | + "Enabled": true, |
| 46 | + "EnableParallelQuerying": true, |
| 47 | + "EnableResultDeduplication": true, |
| 48 | + "MaxConcurrentQueries": 3, |
| 49 | + "BackendTimeoutSeconds": 30, |
| 50 | + "WriteEndpoint": "primary_backend", |
| 51 | + "Endpoints": { |
| 52 | + "primary_backend": { |
| 53 | + "Enabled": true, |
| 54 | + "BackendType": "azure_ai_search", |
| 55 | + "Priority": 10, |
| 56 | + "Properties": { |
| 57 | + "ConnectionString": "your-connection-string", |
| 58 | + "IndexName": "your-index" |
| 59 | + } |
| 60 | + }, |
| 61 | + "secondary_backend": { |
| 62 | + "Enabled": true, |
| 63 | + "BackendType": "mock", |
| 64 | + "Priority": 5, |
| 65 | + "Properties": {} |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Usage Example |
| 74 | + |
| 75 | +```csharp |
| 76 | +public class ExampleController : ControllerBase |
| 77 | +{ |
| 78 | + private readonly INLWebService _nlWebService; |
| 79 | + private readonly IBackendManager _backendManager; |
| 80 | + |
| 81 | + public ExampleController(INLWebService nlWebService, IBackendManager backendManager) |
| 82 | + { |
| 83 | + _nlWebService = nlWebService; |
| 84 | + _backendManager = backendManager; |
| 85 | + } |
| 86 | + |
| 87 | + [HttpPost("search")] |
| 88 | + public async Task<IActionResult> Search([FromBody] NLWebRequest request) |
| 89 | + { |
| 90 | + // Multi-backend search automatically handled |
| 91 | + var response = await _nlWebService.ProcessRequestAsync(request); |
| 92 | + return Ok(response); |
| 93 | + } |
| 94 | + |
| 95 | + [HttpGet("backend-info")] |
| 96 | + public IActionResult GetBackendInfo() |
| 97 | + { |
| 98 | + // Get information about configured backends |
| 99 | + var backendInfo = _backendManager.GetBackendInfo(); |
| 100 | + return Ok(backendInfo); |
| 101 | + } |
| 102 | + |
| 103 | + [HttpGet("write-backend-capabilities")] |
| 104 | + public IActionResult GetWriteBackendCapabilities() |
| 105 | + { |
| 106 | + // Access the designated write backend |
| 107 | + var writeBackend = _backendManager.GetWriteBackend(); |
| 108 | + if (writeBackend == null) |
| 109 | + { |
| 110 | + return NotFound("No write backend configured"); |
| 111 | + } |
| 112 | + |
| 113 | + var capabilities = writeBackend.GetCapabilities(); |
| 114 | + return Ok(capabilities); |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Key Features |
| 120 | + |
| 121 | +### Parallel Querying |
| 122 | +- Queries execute simultaneously across all enabled backends |
| 123 | +- Configurable concurrency limits and timeouts |
| 124 | +- Graceful handling of backend failures |
| 125 | + |
| 126 | +### Result Deduplication |
| 127 | +- Automatic deduplication based on URL |
| 128 | +- Higher scoring results from different backends take precedence |
| 129 | +- Can be disabled for scenarios requiring all results |
| 130 | + |
| 131 | +### Write Endpoint |
| 132 | +- Designate one backend as the primary write endpoint |
| 133 | +- Other backends remain read-only for queries |
| 134 | +- Useful for hybrid architectures |
| 135 | + |
| 136 | +### Backward Compatibility |
| 137 | +- Existing single-backend configurations continue to work |
| 138 | +- No breaking changes to existing APIs |
| 139 | +- Gradual migration path available |
| 140 | + |
| 141 | +## Migration from Single Backend |
| 142 | + |
| 143 | +1. Replace `AddNLWebNet<T>()` with `AddNLWebNetMultiBackend()` |
| 144 | +2. Set `MultiBackend.Enabled = false` initially to maintain existing behavior |
| 145 | +3. Configure additional backends in the `Endpoints` section |
| 146 | +4. Enable multi-backend mode by setting `MultiBackend.Enabled = true` |
| 147 | +5. Test and adjust concurrency and timeout settings as needed |
0 commit comments