如何使用 ASP.NET Core 进行全文搜索和索引?

How to perform full text search and indexing using ASP.NET Core?

我正在研究如何执行类似于 python 中的 Whoosh 的全文搜索和索引。

我查看了 Lucene.NET,但它似乎与 ASP.NET Core(2.0 或更高版本)不兼容。

在这个技术堆栈中,是否还有其他全文搜索引擎的替代品?

Entity Framework Core 2.1.0 使用 FreeText 引入了全文搜索兼容性,EF Core 2.2.0 引入了 Contains.

EF 和 LINQ 使用 Contains:

string criteria = "Find This";

var items = Inventory.Where(x => EF.Functions.Contains(x.KeySearchField, criteria)).ToList();

您可以使用这个 nuget 包 Bsa.Search.Core。

此包与 .Net Core 3.1 兼容,没有依赖项。

该库包含 3 种索引类型:

  • MemoryDocumentIndex - 快速内存索引
  • DiskDocumentIndex 将索引存储在磁盘上
  • DiskShardDocumentIndex 在超过 300 万个文档的磁盘上存储大型索引

内存索引使用示例

var field = "*"; // search in any field
var query = "(first & \"second and four*\") | (four* ~3 \'six\')"; //search word: first and phrase with wildcard or four- wildcard on distance 3 with six
var documentIndex = new MemoryDocumentIndex();// instance of new memory index
var content = "six first second four"; // text that is indexed
var searchService = new SearchServiceEngine(documentIndex);//service engine for working with indexes
var doc = new IndexDocument("ExternalId");//the document to be indexed and externalId
doc.Add("content".GetField(content)); // adding a new field: content
searchService.Index(new IndexDocument[]
{
  doc// saving the document to the index
});

var parsed = query.Parse(field); // parsing the text and making a Boolean query
var request = new SearchQueryRequest() // 
{
     Query = parsed,
     Field = field,
};
var result = searchService.Search(request); // 

Result will be

您可以使用这个 nuget 包 Bsa.Search.Core 但在 .net core 3.1 或 .net framework 472 下