你如何在 .net 上运行 Lucene?

2022-01-15 00:00:00 indexing search lucene .net java

Lucene 是一个优秀的搜索引擎,但是 .NET 版本落后于 Java 官方版本(.NET 最新稳定版本是 2.0,而 Java Lucene 最新版本是 2.4,它有更多功能).

Lucene is an excellent search engine, but the .NET version is behind the official Java release (latest stable .NET release is 2.0, but the latest Java Lucene version is 2.4, which has more features).

你如何解决这个问题?

推荐答案

我发现了一种让我感到惊讶的方法:从 Java .jar 文件创建一个 .NET DLL!使用 IKVM 你可以 下载Lucene,获取.jar文件,运行:

One way I found, which was surprised could work: Create a .NET DLL from a Java .jar file! Using IKVM you can download Lucene, get the .jar file, and run:

ikvmc -target:library <path-to-lucene.jar>

生成一个像这样的 .NET dll:lucene-core-2.4.0.dll

which generates a .NET dll like this: lucene-core-2.4.0.dll

然后您就可以从您的项目中引用此 DLL,一切顺利!您将需要一些 java 类型,因此还要参考 IKVM.OpenJDK.ClassLibrary.dll.你的代码可能有点像这样:

You can then just reference this DLL from your project and you're good to go! There are some java types you will need, so also reference IKVM.OpenJDK.ClassLibrary.dll. Your code might look a bit like this:

QueryParser parser = new QueryParser("field1", analyzer);
java.util.Map boosts = new java.util.HashMap();
boosts.put("field1", new java.lang.Float(1.0));
boosts.put("field2", new java.lang.Float(10.0));

MultiFieldQueryParser multiParser = new MultiFieldQueryParser
                      (new string[] { "field1", "field2" }, analyzer, boosts);
multiParser.setDefaultOperator(QueryParser.Operator.OR);

Query query = multiParser.parse("ABC");
Hits hits = isearcher.search(query);

我从来不知道您可以如此轻松地实现 Java 到 .NET 的互操作性.最好的部分是 C# 和 Java几乎"源代码兼容(涉及 Lucene 示例).只需将 System.Out 替换为 Console.Writeln :).

I never knew you could have Java to .NET interoperability so easily. The best part is that C# and Java is "almost" source code compatible (where Lucene examples are concerned). Just replace System.Out with Console.Writeln :).

=======

更新:在构建像 Lucene 荧光笔这样的库时,请确保您引用了核心程序集(否则您会收到有关缺少类的警告).所以荧光笔是这样构建的:

Update: When building libraries like the Lucene highlighter, make sure you reference the core assembly (else you'll get warnings about missing classes). So the highlighter is built like this:

ikvmc -target:library lucene-highlighter-2.4.0.jar -r:lucene-core-2.4.0.dll

相关文章