ASP.NET 核心 - 索引和搜索 JSON 文件

ASP.NET Core - Indexing and searching JSON files

我有将近 10K JSON 个文件(非常小)。我想提供搜索功能。由于这些 JSON 文件是针对特定版本固定的,我正在考虑在网站启动期间预先索引文件并加载索引。我不想使用外部搜索引擎。

我正在寻找支持此功能的库。 lucene.Net 是一个流行的图书馆。不知道这个库是否支持加载索引前数据

我不确定这是否可能。有哪些可能的选择?

由于 S3 不是特定于 .NET 的技术,并且 Lucene.NET 是 Lucene 的逐行端口,您可以扩大搜索范围以包括与 Lucene 相关的问题。有一个 指向可以移植到 .NET 的 Lucene 的 S3 实现。但是,据作者自己承认,实现的性能并不好。

NOTE: I don't consider this to be a duplicate question due to the fact that the answer most appropriate to you is not the accepted answer, since you explicitly stated you don't want to use an external solution.

Lucene.NET 有几个使用 Azure 而不是 AWS here and here 的实现。您可能会得到一些想法来帮助您为 S3 创建更优化的解决方案,但创建您自己的 Directory 实施是一项不平凡的任务。

Can IndexReader read index file from in-memory string?

可以使用 RAMDirectory,它有一个复制构造函数,可以将整个索引从磁盘移动到内存中。不过,复制构造函数仅在您的文件在磁盘上时才有用。您可能会从 S3 读取文件并将它们放入 RAMDirectory。此选项对于小型索引来说速度很快,但如果您的索引随着时间的推移而增长,则不会扩展。它也没有针对具有多个并发线程执行搜索的高流量网站进行优化。

来自documentation:

Warning: This class is not intended to work with huge indexes. Everything beyond several hundred megabytes will waste resources (GC cycles), because it uses an internal buffer size of 1024 bytes, producing millions of byte[1024] arrays. This class is optimized for small memory-resident indexes. It also has bad concurrency on multithreaded environments.

It is recommended to materialize large indexes on disk and use MMapDirectory, which is a high-performance directory implementation working directly on the file system cache of the operating system, so copying data to heap space is not useful.

当您调用 FSDirectory.Open() 方法时,它会选择一个针对当前操作系统优化的目录。在大多数情况下,它是 returns MMapDirectory,这是一种在多个视图下使用 System.IO.MemoryMappedFiles.MemoryMappedFile class 的实现。如果索引很大或者有很多并发用户,这个选项会更好地扩展。

要使用 Lucene.NET 的内置索引文件优化,您必须将索引文件放在可以像普通文件系统一样读取的介质中。与其尝试推出使用 S3 的 API 的 Lucene.NET 解决方案,不如查看 using S3 as a file system。虽然,我不确定与本地文件系统相比它的性能如何。