Simple Lucene Example Code

Simple Lucene Example Code

Lucene is a great core for a Java search engine. Here is simple Lucene example code to index simple single field data along with a very basic search function. This will create simple Java search engine. For this simple lucene example code, each block is catching the thrown exceptions so you can see what is thrown. In a real world lucene implementation, you may handle this differently.<script type="text/javascript"> <!-- google_ad_client = "pub-0697218185318494"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel ="8231438957"; google_ad_type = "text_image"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "666666"; google_color_text = "333333"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

Lucene Example Code: Steps to Index the data

  1. Create a new Lucene index using an IndexWriter
  2. Create a Lucene Document
  3. Add the Lucene document to the index
  4. Optimize and close the index

Create a new Lucene index using an IndexWriter

   String indexPath = "/path/to/whereYou/wantThe/IndexStored";
   IndexWriter writer = null;
   try {
         // Make a lucene  writer and create new Lucene index with arg3 = true
	writer = new IndexWriter(indexPath, new StandardAnalyzer(), true);
   } catch (IOException e) {
	System.out.println("IOException opening Lucene IndexWriter: " + e.getMessage());
   }

Create a Lucene document

   String content = "This is the example text I want to have Lucene index";
   Document doc = new Document();
   doc.add(Field.Text("content",content));

Add the document to the index

   try {
	writer.addDocument(doc);
   } catch (IOException e) {
        System.out.println("IOException adding Lucene Document: " + e.getMessage());
   }

Optimize and close the IndexWriter

   try {
	writer.optimize();
	writer.close();
   catch (IOException e) {
        System.out.println("IOException closing Lucene IndexWriter: " + e.getMessage());
   }
 
<script type="text/javascript"> <!-- google_ad_client = "pub-0697218185318494"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel ="8231438957"; google_ad_type = "text_image"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "666666"; google_color_text = "333333"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

Lucene Example Code: Steps to Search the Lucene Index

Open a Lucene IndexSearcher

   IndexSearcher indexSearcher = new IndexSearcher(indexPath);
If you are using the Lucene search engine from a web page, you should store and reuse the same IndexSearcher for each query. The Lucene IndexSearcher caches information to make queries after the first one faster. Reusing the Lucene IndexSearcher also takes it easy on the Java garbage collector, increasing performance and memory utilization. Not reusing the IndexSearcher is a common mistake and cause of frustration for many first time lucene users. For use on the web, here is some simple JSP code to store the IndexSearcher in an application attribute and reuse it for future page loads.
   indexSearcher = (IndexSearcher) application.getAttribute("searcher");
   if(indexSearcher == null){
	indexSearcher = new IndexSearcher(indexPath);
	application.setAttribute("searcher",indexSearcher);
   }

Construct a Lucene Query

   String queryString = "example";
   try {
	Query query = QueryParser.parse(queryString,"content",new StandardAnalyzer());
   } catch (ParseException e) {
	System.out.println("Lucene ParseException: " + e. getMessage);
	e.printStackTrace();
   }

Have Lucene perform the Search

   Hits hits = null;
   try {
	Hits hits = indexSearcher.search(query);
   catch (IOException e) {
        System.out.println("Lucene Searching Exception: " + e.getMessage());
   }

Display the top Lucene Hits

   int hitCount = hits.length();
   for(int i=0; (i < hitCount && i < 10); i++){
	Document doc = hits.doc(i);
	System.out.println(doc.get("content"));
   }

That's it!

Those are the bits needed to create a simple, one field, Lucene search engine in Java. In terms of the try and catch block and variables, you'd probably implement things in a more combined manor, but the samples on this page are designed at least at some level to exist in isolation from each other.

Want a more powerful Search Engine?

We also have a Multi-Field Search Engine Example if you want to get a little bit more powerful.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值