Repository navigation
Expand file tree
/
Copy pathconfiguration.cs
More file actions
238 lines (211 loc) · 7.84 KB
/
Copy pathconfiguration.cs
File metadata and controls
238 lines (211 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="MongoDB.Bson" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
</Project>
<!-- appsettings.json configuration -->
<!--
{
"MongoLucene": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "SearchDatabase",
"DefaultIndexName": "DocumentIndex",
"Settings": {
"MaxResultsPerQuery": 1000,
"DefaultAnalyzer": "StandardAnalyzer",
"EnableTextIndexes": true,
"IndexRefreshInterval": "00:05:00"
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"MongoLucene": "Debug"
}
}
}
-->
<!-- Additional Configuration and Dependency Injection Setup -->
<!--
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MongoLucene;
namespace MongoLucene.Configuration
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMongoLucene(this IServiceCollection services, IConfiguration configuration)
{
var config = configuration.GetSection("MongoLucene").Get<MongoLuceneConfig>();
services.AddSingleton(config);
services.AddSingleton<IAnalyzer, StandardAnalyzer>();
services.AddScoped<MongoLuceneIndex>(provider =>
{
return new MongoLuceneIndex(
config.ConnectionString,
config.DatabaseName,
config.DefaultIndexName
);
});
services.AddScoped<IndexWriter>(provider =>
{
var index = provider.GetRequiredService<MongoLuceneIndex>();
var analyzer = provider.GetRequiredService<IAnalyzer>();
return index.GetWriter(analyzer);
});
services.AddScoped<IndexSearcher>(provider =>
{
var index = provider.GetRequiredService<MongoLuceneIndex>();
return index.GetSearcher();
});
return services;
}
}
public class MongoLuceneConfig
{
public string ConnectionString { get; set; } = "mongodb://localhost:27017";
public string DatabaseName { get; set; } = "SearchDatabase";
public string DefaultIndexName { get; set; } = "DocumentIndex";
public MongoLuceneSettings Settings { get; set; } = new();
}
public class MongoLuceneSettings
{
public int MaxResultsPerQuery { get; set; } = 1000;
public string DefaultAnalyzer { get; set; } = "StandardAnalyzer";
public bool EnableTextIndexes { get; set; } = true;
public TimeSpan IndexRefreshInterval { get; set; } = TimeSpan.FromMinutes(5);
}
}
// Performance Monitoring and Metrics
namespace MongoLucene.Monitoring
{
public class SearchMetrics
{
public int TotalSearches { get; set; }
public double AverageQueryTimeMs { get; set; }
public int TotalDocumentsIndexed { get; set; }
public DateTime LastIndexUpdate { get; set; }
public Dictionary<string, int> PopularSearchTerms { get; set; } = new();
}
public class MongoLuceneMetricsCollector
{
private readonly SearchMetrics _metrics = new();
private readonly object _lock = new();
public void RecordSearch(TimeSpan queryTime, string query)
{
lock (_lock)
{
_metrics.TotalSearches++;
_metrics.AverageQueryTimeMs =
(_metrics.AverageQueryTimeMs * (_metrics.TotalSearches - 1) + queryTime.TotalMilliseconds)
/ _metrics.TotalSearches;
// Track popular terms
var terms = query.Split(' ', StringSplitOptions.RemoveEmptyEntries);
foreach (var term in terms)
{
_metrics.PopularSearchTerms[term.ToLower()] =
_metrics.PopularSearchTerms.GetValueOrDefault(term.ToLower(), 0) + 1;
}
}
}
public void RecordIndexUpdate()
{
lock (_lock)
{
_metrics.TotalDocumentsIndexed++;
_metrics.LastIndexUpdate = DateTime.UtcNow;
}
}
public SearchMetrics GetMetrics()
{
lock (_lock)
{
return new SearchMetrics
{
TotalSearches = _metrics.TotalSearches,
AverageQueryTimeMs = _metrics.AverageQueryTimeMs,
TotalDocumentsIndexed = _metrics.TotalDocumentsIndexed,
LastIndexUpdate = _metrics.LastIndexUpdate,
PopularSearchTerms = new Dictionary<string, int>(_metrics.PopularSearchTerms)
};
}
}
}
}
// Advanced Features: Faceted Search
namespace MongoLucene.Facets
{
public class FacetRequest
{
public string Field { get; set; }
public int MaxCount { get; set; } = 10;
}
public class FacetResult
{
public string Field { get; set; }
public Dictionary<string, long> Values { get; set; } = new();
}
public class FacetedSearchResult : SearchResults
{
public Dictionary<string, FacetResult> Facets { get; set; } = new();
}
public static class FacetedSearchExtensions
{
public static async Task<FacetedSearchResult> SearchWithFacetsAsync(
this IndexSearcher searcher,
IMongoCollection<IndexedDocument> collection,
Query query,
int maxResults = 10,
int skip = 0,
params FacetRequest[] facetRequests)
{
var baseResults = await searcher.SearchAsync(query, maxResults, skip);
var facetedResult = new FacetedSearchResult
{
Results = baseResults.Results,
TotalHits = baseResults.TotalHits,
QueryTime = baseResults.QueryTime
};
// Calculate facets
foreach (var facetRequest in facetRequests)
{
var pipeline = new[]
{
new BsonDocument("$match", query.ToMongoFilter().Render(
collection.DocumentSerializer,
collection.Settings.SerializerRegistry)),
new BsonDocument("$group", new BsonDocument
{
{ "_id", $"$Fields.{facetRequest.Field}" },
{ "count", new BsonDocument("$sum", 1) }
}),
new BsonDocument("$sort", new BsonDocument("count", -1)),
new BsonDocument("$limit", facetRequest.MaxCount)
};
var facetResults = await collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
var facetValues = facetResults.ToDictionary(
doc => doc["_id"].ToString(),
doc => doc["count"].AsInt64
);
facetedResult.Facets[facetRequest.Field] = new FacetResult
{
Field = facetRequest.Field,
Values = facetValues
};
}
return facetedResult;
}
}
}
-->