![]() |
VOOZH | about |
dotnet add package Dotnetydd.Tools.LuceneNet --version 1.0.0
NuGet\Install-Package Dotnetydd.Tools.LuceneNet -Version 1.0.0
<PackageReference Include="Dotnetydd.Tools.LuceneNet" Version="1.0.0" />
<PackageVersion Include="Dotnetydd.Tools.LuceneNet" Version="1.0.0" />Directory.Packages.props
<PackageReference Include="Dotnetydd.Tools.LuceneNet" />Project file
paket add Dotnetydd.Tools.LuceneNet --version 1.0.0
#r "nuget: Dotnetydd.Tools.LuceneNet, 1.0.0"
#:package Dotnetydd.Tools.LuceneNet@1.0.0
#addin nuget:?package=Dotnetydd.Tools.LuceneNet&version=1.0.0Install as a Cake Addin
#tool nuget:?package=Dotnetydd.Tools.LuceneNet&version=1.0.0Install as a Cake Tool
基于lucene.net 实现的一款全文检索工具,支持结果短语查询,中英文分词查询,关键字查询,也可以自行安装符合Lucene.Net的分析器以及分词工具。本项目最低支持net framework4.5.1 目前大多数项目中可以引入。索引数据的操作需要自行实现,比如使用ef/efcore的change tracking 、savechange拦截器、领域事件等方式实现索引的操作与项目中数据的同步。
注意:该工具仅适用于简单搜索场景,不适用于分布式应用以及复杂的场景,专业项目请考虑使用大型的搜索引擎中间件做支撑,如:ElasticSearch,或考虑数据库全文检索,或数据库正则。 注意:对于复杂字段的索引,目前只是进行序列化方式,需要自行实现
IFieldSerializeProvider接口以实现字段的序列化,并且字段的特性LuceneIndexed中IsSerializeStore要设置成true 注意:请数据自定标识字段,并且字段的特性LuceneIndexed中IsIdentityField要设置成true 注意:需要指定版本的地方请使用LuceneSearchEngine.LuceneVersion
// net framework 中直接new就可以。或者使用依赖注入框架。
using var searchEngine = new LuceneSearchEngine(new SmartChineseAnalyzer(LuceneSearchEngine.LuceneVersion),new LuceneSearchEngineOptions
{
IndexDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "luceneIndexs"),
},new NewtonsoftMessageSerializeProvider());
builder.Services.Configure<LuceneSearchEngineOptions>(o =>
{
o.IndexDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "luceneIndexs");
});
// SmartChineseAnalyzer 的停用词可以自定义
builder.Services.AddScoped<Analyzer>(s => new SmartChineseAnalyzer(LuceneSearchEngine.LuceneVersion));
builder.Services.AddScoped<LuceneSearchEngine>();
// other service
public class Demo
{
private readonly LuceneSearchEngine _luceneSearchEngine;
public Demo(LuceneSearchEngine luceneSearchEngine)
{
_luceneSearchEngine = luceneSearchEngine;
}
}
var defaultStop= CharArraySet.UnmodifiableSet(WordlistLoader.GetWordSet(IOUtils.GetDecodingReader(typeof(SmartChineseAnalyzer), "stopwords.txt", Encoding.UTF8), "//", LuceneSearchEngine.LuceneVersion));
var stopwords = new CharArraySet(LuceneSearchEngine.LuceneVersion, new string[] {"的", "sb"}, true);
// demo model
public class Person
{
[LuceneIndexed("Id", true)]
public long Id { get; set; }
[LuceneIndexed("Name", false)]
public string Name { get; set; }
[LuceneIndexed("Remarks", false, IsTextField = true, IsHighLight = true, HightLightMaxNumber = 1)]
public string Remarks { get; set; }
}
var dataList=new List<Person>();
var successed = _luceneSearchEngine.CreateIndex(dataList);
var query = new TermQuery(new Term("Remarks", "搜索"));
var query = new PhraseQuery(new Term("Remarks", "搜索"));
var query = new BooleanQuery()
queryd.Add(new TermQuery(new Term(nameof(Person.Remarks), keyWords)), Occur.MUST);
var parser = new QueryParser(LuceneVersion.LUCENE_48, nameof(Person.Remarks), _luceneSearchEngine.Analyzer);
var query = parser.Parse(keyWords);
var ab = new MultiFieldQueryParser(LuceneSearchEngine.LuceneVersion, new[] { nameof(Person.Name), nameof(Person.Remarks) }, _luceneSearchEngine.Analyzer);
var query = ab.Parse(keyWords);
var builder = new QueryBuilder();
var query = builder.BuildQuery(keyWords);
var searchResult = _luceneSearchEngine.Search<Person>(new SearchModel(query, 100)
{
OrderBy = new SortField[] { SortField.FIELD_SCORE }, // 排序信息
Skip = 0, // 分页skip
Take = 20, // 分页 take
Score = 0, // 权重 0开始越大标识越精确
OnlyTyped = true, // 是否只在指定的泛型类型索引文档中搜索,false标识不区分泛型type约束全部查询
HighlightTag = ($"<a style='color:{highlight}'>", "</a>") // 高亮标记
});
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 381 | 5/13/2024 |