Re: database search using Lucene
when i am executing below code, getting Exception :->
"no segments* file found in org.apache.lucene.store.FSDirectory@C:
\dbindex: files:"
on line "IndexWriter writer=new IndexWriter(new File("c:\
\dbindex"),new StandardAnalyzer(),false);"
I m getting above Exception
import java.sql.*;
import java.io.*;
import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.*;
/**
* @author apurohit
*
*/
public class DBIndex {
/**
* @param args
*/
private Connection con;
private String dbDriver,connectionURL,user,password;
public DBIndex()
{
con=null;
dbDriver="com.mysql.jdbc.Driver";
connectionURL="jdbc:mysql://192.168.1.4:3306/startvisitindia";
user="root";
password="";
}
public void setDBDriver(String driver)
{
this.dbDriver=driver;
}
public void setConnectionURL(String connectionURL)
{
this.connectionURL=connectionURL;
}
public void setAuthentication(String user,String password)
{
this.user=user;
this.password=password;
}
public Connection getConnection()
{
try{
Class.forName(dbDriver);
con=DriverManager.getConnection(connectionURL,user,password);
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
private boolean isIndexExist(String indexPath)
{
boolean exist=false;
try{
IndexReader ir=IndexReader.open(indexPath);
exist=true;
ir.close();
}catch(IOException e){
System.out.println("ioexception:-> "+e);
}catch(Exception e){
System.out.println("exception:-> "+e);
}
return exist;
}
public void performIndexing(String indexPath)
{
try{
Connection connection=getConnection();
String query="select DestId,Name,StateName,DestinationDescription
from destination";
Statement statement=connection.createStatement();
ResultSet contentResutlset=statement.executeQuery(query);
IndexWriter writer=new IndexWriter(new File("c:\\dbindex"),new
StandardAnalyzer(),false);
while(contentResutlset.next()){
//Adding all fields' contents to a single string for indexing
String contents=contentResutlset.getString(2)+"
"+contentResutlset.getString(3)+" "+contentResutlset.getString(4);
//Extracting and adding tags to contents string for creating index
/*
String tagQuery="select t.tag_name from tags t,taggedcontents tc
where tc.content_id = '"+contentResutlset.getString(1)+"' and
tc.tag_id=t.tag_id";
Statement tagStatement=connection.createStatement();
ResultSet tagResultset=tagStatement.executeQuery(tagQuery);
while(tagResultset.next()){contents=contents+"
"+tagResultset.getString(1);}
System.out.println("Indexing Content no.(ID) " +
contentResutlset.getShort(1)+"\n"+contents);
*/
//Creating index for a single content(record in contents table)
Document doc = new Document();
doc.add(new Field("contents",
contents,Store.NO,Index.TOKENIZED,TermVector.YES));
doc.add(new
Field("id",contentResutlset.getString(1),Store.YES,Index.UN_TOKENIZED,TermVector.NO));
writer.addDocument(doc);
//writer.close();
}
writer.close();
}//try
catch(Exception e){
e.printStackTrace();
}//catch
}
public static void main(String[] args) {
DBIndex dbi=new DBIndex();
try{
Connection connection=dbi.getConnection();
String query="select DestId,Name,StateName,DestinationDescription
from destination";
Statement statement=connection.createStatement();
ResultSet contentResutlset=statement.executeQuery(query);
System.out.println("111");
IndexWriter writer = new IndexWriter(new File("c:\\dbindex"),new
StandardAnalyzer(),false);
//System.out.println("IndexWriter:-> "+writer);
while(contentResutlset.next()){
//Adding all fields' contents to a single string for indexing
String contents=contentResutlset.getString(2)+"
"+contentResutlset.getString(3)+" "+contentResutlset.getString(4);
//Extracting and adding tags to contents string for creating index
/*
String tagQuery="select t.tag_name from tags t,taggedcontents tc
where tc.content_id = '"+contentResutlset.getString(1)+"' and
tc.tag_id=t.tag_id";
Statement tagStatement=connection.createStatement();
ResultSet tagResultset=tagStatement.executeQuery(tagQuery);
while(tagResultset.next())
{
contents=contents+" "+tagResultset.getString(1);
}
*/
//System.out.println("Indexing Content no.(ID) " +
contentResutlset.getShort(1)+"\n"+contents);
System.out.println("Indexing Content no.(ID) " +
contentResutlset.getString(1));
//Creating index for a single content(record in contents table)
//Document doc = new Document();
//doc.add(new Field("contents",
contents,Store.NO,Index.TOKENIZED,TermVector.YES));
//doc.add(new
Field("id",contentResutlset.getString(1),Store.YES,Index.UN_TOKENIZED,TermVector.NO));
//writer.addDocument(doc);
//writer.close();
}
//writer.close();
contentResutlset.close();
statement.close();
connection.close();
}//try
catch(Exception e){
System.out.println(e.getMessage());
}//catch*/
}
}