問題描述
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
這篇關于你如何在 .net 上運行 Lucene?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!