{"id":32,"date":"2006-06-04T21:34:33","date_gmt":"2006-06-04T19:34:33","guid":{"rendered":"http:\/\/www.devious.dk\/wordpress\/2006\/06\/04\/search-engine-for-java\/"},"modified":"2006-06-09T19:59:47","modified_gmt":"2006-06-09T17:59:47","slug":"search-engine-for-java","status":"publish","type":"post","link":"https:\/\/tommy.winther.nu\/wordpress\/2006\/06\/04\/search-engine-for-java\/","title":{"rendered":"Search Engine for Java"},"content":{"rendered":"<p><img decoding=\"async\" align=\"right\" alt=\"Apache Lucene logo\" id=\"image33\" src=\"http:\/\/www.devious.dk\/files\/2006\/06\/lucene_green_150.gif\" \/>For a project at work we need some search engine like functionality. We&#8217;ve tried to do it ourselves, but usually our implementations were too slow, too memory intensive or too much work to maintain. I was recently recommend by one of my coworkers to have a look at the <a target=\"_blank\" href=\"http:\/\/lucene.apache.org\">Apache Lucene<\/a> project, so I&#8217;ve spent the last couple of nights studying it and doing a test implementation. Coincidentially, Lucene released the 2.0.0 version in late May. \ud83d\ude42<\/p>\n<p>First impressions are quite impressive &#8211; Lucene is a very easy to use search engine and it&#8217;s quite fast as well. It is possible to implement a fully working search engine with only a few lines of code. To do this you need two parts &#8211; an indexer and a searcher.<\/p>\n<p>The indexer can be implemented with the following lines of java code.<\/p>\n<p><code> IndexWriter indexWriter = new IndexWriter(\"\/tmp\/index\", new StandardAnalyzer(), true);<br \/>\nDocument document = new Document();<br \/>\ndocument.add(new Field(\"article\", \"This is a sample article to test the Apache Lucene search engine\", Field.Store.YES, Field.Index.TOKENIZED));<br \/>\nindexWriter.addDocument(document);<br \/>\nindexWriter.close();<\/code><\/p>\n<p>These lines of code creates the index for the Lucene&#8217;s searcher. Of course the index is rather small with just one document, but it&#8217;s easy to add more fields to a document and more documents to the index.<\/p>\n<p>The searcher can also be implemented with a few lines of code.<\/p>\n<p><code>IndexReader indexReader = IndexReader.open(\"\/tmp\/index\");<br \/>\nSearcher searcher = new IndexSearcher(indexReader);<br \/>\nQueryParser parser = new QueryParser(\"\", new StandardAnalyzer());<br \/>\nHits hits = searcher.search(parser.parse(\"sample\"));<br \/>\nSystem.out.println(\"First hit: score \" + hits.score(1) + \" - \" + hits.doc(1).get(\"article\"));<br \/>\nindexReader.close();<\/code><\/p>\n<p>This code creates a Searcher that will search the index for the search word &#8220;sample&#8221; and print the first hit to System.out. This is really all that is required to build a basic search engine. You will of course need all the glue code to make it user friendly and customize it to your needs.<\/p>\n<p>All classes are from the Lucene project, so all that is required is the lucene-core-2.0.0.jar file somewhere in your classpath. Oh.. yes.. you need to catch a few exceptions as well, so the above code won&#8217;t run directly.  \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a project at work we need some search engine like functionality. We&#8217;ve tried to do it ourselves, but usually our implementations were too slow, too memory intensive or too much work to maintain. I was recently recommend by one of my coworkers to have a look at the Apache Lucene project, so I&#8217;ve spent [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-code","post-preview"],"_links":{"self":[{"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tommy.winther.nu\/wordpress\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}