Apache Lucene 5.1.0 indexing and searching java example


      Click here to attend Spring Framework 4.x and certification course with a discount


import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.Store;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

public class LuceneExamples {
	
	public static void main(String[] args) {
		indexDirectory();
		search("java");
	}	
		
	private static void indexDirectory() {		
		 //Apache Lucene Indexing Directory .txt files     
	     try {	
		 //indexing directory	 
		 Path path = Paths.get("C:/Users/Tuna/Desktop/lucene-5.1.0/indexes");
		 Directory directory = FSDirectory.open(path);
		 IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());		
		 IndexWriter indexWriter = new IndexWriter(directory, config);
		 indexWriter.deleteAll();
		 File f = new File("C:/Users/Tuna/Desktop/sample"); // current directory     
		     for (File file : f.listFiles()) {
			        System.out.println("indexed " + file.getCanonicalPath());		        
					Document doc = new Document();
					doc.add(new TextField("path", file.getName(), Store.YES));
					FileInputStream is = new FileInputStream(file);
					BufferedReader reader = new BufferedReader(new InputStreamReader(is));
					StringBuffer stringBuffer = new StringBuffer();
					String line = null;
					while((line = reader.readLine())!=null){
					  stringBuffer.append(line).append("\n");
					}
					reader.close();
					doc.add(new TextField("contents", stringBuffer.toString(), Store.YES));
					indexWriter.addDocument(doc);			
		     }	 		     
		     indexWriter.close();		    
		     directory.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}					
	}
	
	private static void search(String text) {	
		//Apache Lucene searching text inside .txt files
		try {	
			Path path = Paths.get("C:/Users/Tuna/Desktop/lucene-5.1.0/indexes");
			Directory directory = FSDirectory.open(path);		
			IndexReader indexReader =  DirectoryReader.open(directory);
			IndexSearcher indexSearcher = new IndexSearcher(indexReader);
			QueryParser queryParser = new QueryParser("contents",  new StandardAnalyzer());  
			Query query = queryParser.parse(text);
			TopDocs topDocs = indexSearcher.search(query,10);
	        System.out.println("totalHits " + topDocs.totalHits);
			for (ScoreDoc scoreDoc : topDocs.scoreDocs) {			
			    Document document = indexSearcher.doc(scoreDoc.doc);
			    System.out.println("path " + document.get("path"));
			    System.out.println("content " + document.get("contents"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}				
	}
  }

output

indexed C:\Users\Tuna\Desktop\sample\New Text Document (2).txt
indexed C:\Users\Tuna\Desktop\sample\New Text Document (3).txt
indexed C:\Users\Tuna\Desktop\sample\New Text Document (4).txt
indexed C:\Users\Tuna\Desktop\sample\New Text Document (5).txt
indexed C:\Users\Tuna\Desktop\sample\New Text Document.txt
totalHits 1
path New Text Document.txt
content package nl.kameroom.emailservice;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.mail.internet.MimeMessage;
import javax.sql.DataSource;

import nl.kameroom.user.UserManager;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.Assert;

public class EmailService {

/* Email From param */
public static final String FROM = “from”;

/* Email To param */
public static final String TO = “to”;

/* Email Subject param */
public static final String SUBJECT = “subject”;

/* Email CC param */
public static final String CC_LIST = “ccList”;
public static final String KEY = “key”;

@Autowired
private JavaMailSender mailSender;

@Autowired
private VelocityEngine velocityEngine;

@Autowired
private ReloadableResourceBundleMessageSource messageSourceVM = null;
public void setResource(ReloadableResourceBundleMessageSource resource){
this.messageSourceVM = resource;
}
public boolean send(final String templateName, final Map model) {
boolean r = false;
try {

MimeMessagePreparator preparator = new MimeMessagePreparator() {

@SuppressWarnings(“unchecked”)
public void prepare(MimeMessage mimeMessage) throws Exception {
String from = (String) model.get(FROM);
String to = (String) model.get(TO);
String subject = (String) model.get(SUBJECT);
String key = (String) model.get(KEY);
Assert.notNull(from);
Assert.notNull(to);
Assert.notNull(subject);
List ccList = (List) model.get(CC_LIST);
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
if (ccList != null) {
for (String cc : ccList) {
//message.addCc(cc);
message.addBcc(cc);
}
}
model.put(“messageSource”,messageSourceVM);
model.put(“noArgs”, new Object[]{});
model.put(“key”, key);

model.put(“locale”, Locale.ENGLISH);

String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, templateName,”utf-8″, model);
message.setText(text, true);
}
};

mailSender.send(preparator);
r = true;
}catch(Exception e) {
e.printStackTrace();
}
return r;
}
}

Advertisements

One thought on “Apache Lucene 5.1.0 indexing and searching java example

  1. Thanks for this. I am trying to run this but it is showing 0 hits although i have copied exact text from the txt file.
    indexed C:\TextFile\JK000001_JK000001.txt
    indexed C:\TextFile\JK000001_JK000002.txt
    totalHits 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s