{"id":539,"date":"2003-11-16T00:22:30","date_gmt":"2003-11-16T04:22:30","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=539"},"modified":"2003-11-16T00:22:30","modified_gmt":"2003-11-16T04:22:30","slug":"queryparser-in-nlucene","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/11\/16\/queryparser-in-nlucene\/","title":{"rendered":"QueryParser &#8230; in NLucene"},"content":{"rendered":"<p>Misleading title. I implemented the first of the examples that Erik Hatcher used in his <a href=\"http:\/\/today.java.net\/pub\/a\/today\/2003\/11\/07\/QueryParserRules.html\"><br \/>\narticle about the Lucene QueryParser<\/a>, only I used <a href=\"http:\/\/sourceforge.net\/projects\/nlucene\">NLucene<\/a>.  Lucene and NLucene are very similar, so if anything, it&#8217;s interesting only because it highlights a couple of the differences between C# and Java.  <\/p>\n<p>First, here&#8217;s the Java example taken directly from Erik&#8217;s article:<br \/>\n<code><br \/>\npublic static void search(File indexDir, String q) {<br \/>\n&nbsp;&nbsp;Directory fsDir = FSDirectory.getDirectory(indexDir, false);<br \/>\n&nbsp;&nbsp;IndexSearcher is = new IndexSearcher(fsDir);<br \/>\n&nbsp;&nbsp;Query query = QueryParser.parse(q, \"contents\", new StandardAnalyzer());<br \/>\n&nbsp;&nbsp;Hits hits = is.search(query);<br \/>\n&nbsp;&nbsp;System.out.println(\"Found \"   hits.length()  +<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\" document(s) that matched query '\"   q   \"':\");<br \/>\n&nbsp;&nbsp;for (int i = 0; i<br \/>\nThe NLucene version looks eerily similar:<br \/>\n<code><br \/>\npublic static void Search(DirectoryInfo indexDir, string q) {<br \/>\n&nbsp;&nbsp;DotnetPark.NLucene.Store.Directory fsDir = FsDirectory.GetDirectory(indexDir, false);<br \/>\n&nbsp;&nbsp;IndexSearcher searcher = new IndexSearcher(fsDir);<br \/>\n&nbsp;&nbsp;Query query = QueryParser.Parse(q, \"contents\", new StandardAnalyzer());<br \/>\n&nbsp;&nbsp;Hits hits = searcher.Search(query);<br \/>\n&nbsp;&nbsp;Console.WriteLine(\"Found \" + hits.Length +<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\" document(s) that matched query '\" + q + \"':\");<br \/>\n&nbsp;&nbsp;for (int i = 0; i<br \/>\nThe differences are mainly syntax.  <\/p>\n<p>First, Erik used the variable name 'is' for his IndexSearcher.  In C# <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfispg.asp\">'is' is a keyword<\/a>, so I switched the variable name to 'searcher'.  If you're really geeky, you might want to brush up on all the <a href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/nutsandbolts\/_keywords.html\">Java keywords<\/a> and the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfcsharpkeywords_pg.asp\">C# keywords<\/a>.<\/p>\n<p>Second, while Java uses the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/File.html\">File<\/a> class to describe directories and files, the .NET Framework uses the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemiodirectoryinfoclasstopic.asp\">DirectoryInfo<\/a> class. <\/p>\n<p>Third, Java programmers are encouraged to <a href=\"http:\/\/java.sun.com\/docs\/books\/jls\/second_edition\/html\/names.doc.html#29466\">capitalize class names<\/a> and use camel Case notation for <a href=\"http:\/\/java.sun.com\/docs\/books\/jls\/second_edition\/html\/names.doc.html#9322\">method and variable names<\/a> while  C# programmers are encouraged to <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpgenref\/html\/cpconcapitalizationstyles.asp\">Pascal notation for methods and camel Case for variables<\/a>, so I switched the static method name from 'search' to 'Search'.  <\/p>\n<p>Next, '<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemiodirectoryclasstopic.asp\">Directory<\/a>' is a system class, so the reference to the NLucene directory needed to be fully qualified:<br \/>\n<code><br \/>\nDotnetPark.NLucene.Store.Directory fsDir = FsDirectory.GetDirectory(indexDir, false);<br \/>\n<\/code><br \/>\nrather than this:<br \/>\n<code><br \/>\nDirectory fsDir = FsDirectory.GetDirectory(indexDir, false);<br \/>\n<\/code><br \/>\nFinally, the Hits class contains a couple differences.  Java programmers use the length() method on a variety of classes, so it made sense for the Java version to use a length() method as well.  C# introduced the idea of a <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfpropertiespg.asp\">property<\/a>, which is nothing more than syntactic sweetness that allows the API developer to encapsulate the implementation of a variable, but allow access to it as if it were a public field.  The end result is that instead of writing:<br \/>\n<code><br \/>\nfor (int i = 0; i<br \/>\nin Java, you'd use this in C#:<br \/>\n<code><br \/>\nfor (int i = 0; i<br \/>\nThe authors of Lucene also decided to use the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfindexedpropertiespg.asp\">C# indexer<\/a> functionality (which I <a href=\"http:\/\/cephas.net\/blog\/2003\/11\/06\/c_indexers.html#000594\">wrote about a couple days ago<\/a>) so that an instance of the Hits class can be accessed as if it were an array:<br \/>\n<code><br \/>\nDocument doc = hits[i].Document;<br \/>\n<\/code><br \/>\nI put together a complete sample that you can download and compile yourself if you're interested in using NLucene.  Download it <a href=\"\/images\/files\/QueryParserSample.cs\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Misleading title. I implemented the first of the examples that Erik Hatcher used in his article about the Lucene QueryParser, only I used NLucene. Lucene and NLucene are very similar, so if anything, it&#8217;s interesting only because it highlights a couple of the differences between C# and Java. First, here&#8217;s the Java example taken directly &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/11\/16\/queryparser-in-nlucene\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">QueryParser &#8230; in NLucene<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,3,19,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/539"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=539"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}