全文检索是现代应用程序中非常重要的搜索技术,它允许用户快速、精确地在大量文本数据中找到相关信息。在设备日志管理系统中,高效的全文检索尤为关键。
C#// 定义设备日志实体
public class DeviceLog
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public string DeviceId { get; set; }
public string LogLevel { get; set; }
public string Message { get; set; }
}
C#namespace App10
{
internal class Program
{
static void Main(string[] args)
{
var logService = new LogSearchService("devicelogs.db");
// 添加测试记录
var testLogs = new List<DeviceLog>
{
new DeviceLog
{
DeviceId = "DEV001",
Timestamp = DateTime.Now.AddHours(-2),
LogLevel = "INFO",
Message = "设备启动正常,开始运行"
},
new DeviceLog
{
DeviceId = "DEV001",
Timestamp = DateTime.Now.AddHours(-1),
LogLevel = "WARNING",
Message = "网络连接不稳定,正在重试"
},
new DeviceLog
{
DeviceId = "DEV002",
Timestamp = DateTime.Now.AddMinutes(-45),
LogLevel = "ERROR",
Message = "设备温度过高,紧急停机"
},
new DeviceLog
{
DeviceId = "DEV003",
Timestamp = DateTime.Now.AddMinutes(-30),
LogLevel = "INFO",
Message = "系统更新完成,版本号v2.1.0"
},
new DeviceLog
{
DeviceId = "DEV002",
Timestamp = DateTime.Now.AddMinutes(-25),
LogLevel = "ERROR",
Message = "网络连接异常,无法访问远程服务器"
},
new DeviceLog
{
DeviceId = "DEV001",
Timestamp = DateTime.Now.AddMinutes(-20),
LogLevel = "INFO",
Message = "数据备份完成,存储空间充足"
},
new DeviceLog
{
DeviceId = "DEV004",
Timestamp = DateTime.Now.AddMinutes(-15),
LogLevel = "WARNING",
Message = "内存使用率达到85%,建议优化"
},
new DeviceLog
{
DeviceId = "DEV003",
Timestamp = DateTime.Now.AddMinutes(-10),
LogLevel = "ERROR",
Message = "传感器数据异常,需要校准"
},
new DeviceLog
{
DeviceId = "DEV004",
Timestamp = DateTime.Now.AddMinutes(-5),
LogLevel = "INFO",
Message = "设备自检完成,运行正常"
},
new DeviceLog
{
DeviceId = "DEV002",
Timestamp = DateTime.Now,
LogLevel = "WARNING",
Message = "电源电压波动,已切换备用电源"
}
};
// 将测试记录插入数据库
logService.BatchInsert(testLogs);
Console.ReadKey();
}
}
}

C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiteDB;
namespace App10
{
public class LogSearchService
{
private readonly LiteDatabase _database;
private readonly ILiteCollection<DeviceLog> _logCollection;
public LogSearchService(string databasePath)
{
// 初始化 LiteDB 数据库
_database = new LiteDatabase(databasePath);
_logCollection = _database.GetCollection<DeviceLog>("device_logs");
// 为 Message 字段创建全文索引
_logCollection.EnsureIndex("FULL_TEXT", x => x.Message);
}
/// <summary>
/// 批量写入
/// </summary>
/// <param name="logs"></param>
public void BatchInsert(List<DeviceLog> logs)
{
_logCollection.InsertBulk(logs);
}
/// <summary>
/// 精确关键字搜索
/// </summary>
/// <param name="keyword">搜索关键字</param>
/// <returns>匹配的日志列表</returns>
public List<DeviceLog> SearchByKeyword(string keyword)
{
// 使用 Query.Contains 进行精确匹配
return _logCollection
.Find(Query.Contains("Message", keyword))
.ToList();
}
/// <summary>
/// 模糊搜索:支持部分匹配和相似度搜索
/// </summary>
/// <param name="searchTerm">搜索词</param>
/// <param name="similarityThreshold">相似度阈值</param>
/// <returns>相似的日志列表</returns>
public List<DeviceLog> FuzzySearch(string searchTerm, double similarityThreshold = 0.7)
{
// 先使用 LIKE 查询获取可能匹配的结果
var possibleMatches = _logCollection
.Find(Query.Contains("Message", searchTerm))
.ToList();
// 在内存中进行相似度过滤
return possibleMatches
.Where(log => CalculateSimilarity(
log.Message.ToLower(),
searchTerm.ToLower()) >= similarityThreshold)
.ToList();
}
/// <summary>
/// 计算字符串相似度(使用 Levenshtein 距离)
/// </summary>
private double CalculateSimilarity(string str1, string str2)
{
int levenshteinDistance = ComputeLevenshteinDistance(str1, str2);
int maxLength = Math.Max(str1.Length, str2.Length);
return 1.0 - (double)levenshteinDistance / maxLength;
}
/// <summary>
/// 计算 Levenshtein 距离
/// </summary>
private int ComputeLevenshteinDistance(string str1, string str2)
{
int[,] distance = new int[str1.Length + 1, str2.Length + 1];
for (int i = 0; i <= str1.Length; i++)
distance[i, 0] = i;
for (int j = 0; j <= str2.Length; j++)
distance[0, j] = j;
for (int i = 1; i <= str1.Length; i++)
{
for (int j = 1; j <= str2.Length; j++)
{
int cost = (str1[i - 1] == str2[j - 1]) ? 0 : 1;
distance[i, j] = Math.Min(
Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1),
distance[i - 1, j - 1] + cost
);
}
}
return distance[str1.Length, str2.Length];
}
/// <summary>
/// 高级搜索:组合多个搜索条件
/// </summary>
/// <param name="keyword">关键字</param>
/// <param name="logLevel">日志级别</param>
/// <param name="startTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <returns>符合条件的日志列表</returns>
public List<DeviceLog> AdvancedSearch(
string keyword = null,
string logLevel = null,
DateTime? startTime = null,
DateTime? endTime = null,
string deviceId = null)
{
try
{
var query = _logCollection.Query();
if (!string.IsNullOrEmpty(keyword))
{
query = query.Where(x => x.Message.Contains(keyword));
}
if (!string.IsNullOrEmpty(logLevel))
{
query = query.Where(x => x.LogLevel == logLevel);
}
if (startTime.HasValue)
{
query = query.Where(x => x.Timestamp >= startTime.Value);
}
if (endTime.HasValue)
{
query = query.Where(x => x.Timestamp <= endTime.Value);
}
// 新增条件
if (!string.IsNullOrEmpty(deviceId))
{
query = query.Where(x => x.DeviceId == deviceId);
}
return query.ToList();
}
catch (Exception ex)
{
throw new Exception($"Error during advanced search: {ex.Message}", ex);
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Dispose()
{
_database.Dispose();
}
}
}
C#namespace App10
{
internal class Program
{
static void Main(string[] args)
{
var logService = new LogSearchService("devicelogs.db");
// 精确搜索
var exactResults = logService.SearchByKeyword("网络连接");
foreach(var result in exactResults)
{
Console.WriteLine(result.DeviceId);
Console.WriteLine(result.Message);
Console.WriteLine(result.LogLevel);
Console.WriteLine(result.Timestamp);
Console.WriteLine();
}
Console.ReadKey();
}
}
}

C#namespace App10
{
internal class Program
{
static void Main(string[] args)
{
var logService = new LogSearchService("devicelogs.db");
// 模糊搜索
var fuzzyResults = logService.FuzzySearch("网络连接不稳定", 0.5);
foreach (var result in fuzzyResults)
{
Console.WriteLine(result.DeviceId);
Console.WriteLine(result.Message);
Console.WriteLine(result.LogLevel);
Console.WriteLine(result.Timestamp);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
C#namespace App10
{
internal class Program
{
static void Main(string[] args)
{
var logService = new LogSearchService("devicelogs.db");
// 高级搜索
var advancedResults = logService.AdvancedSearch(
keyword: "异常",
logLevel: "ERROR",
startTime: DateTime.Now.AddDays(-7),
endTime: DateTime.Now
);
foreach (var result in advancedResults)
{
Console.WriteLine(result.DeviceId);
Console.WriteLine(result.Message);
Console.WriteLine(result.LogLevel);
Console.WriteLine(result.Timestamp);
Console.WriteLine();
}
Console.ReadKey();
}
}
}

EnsureIndex() 创建全文索引Message)LiteDB 提供了强大且灵活的全文检索能力,通过合理设计和优化,可以在小型到中型应用中实现高效的日志搜索功能。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!