如何使用ASP.NET Core执行全文本搜索和索引编制?

时间:2018-11-16 17:20:24

标签: asp.net-web-api asp.net-core indexing lucene.net

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

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

在此技术堆栈中,全文搜索引擎是否还有其他选择?

2 个答案:

答案 0 :(得分:3)

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

使用Contains的EF和LINQ:

string criteria = "Find This";

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

答案 1 :(得分:0)

您可以使用此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下