0
package lucene;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Lucenetest {
//database connection
public static final String PATH = "C:/dbindex/index.txt";
private static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String CONNECTION_URL = "jdbc:sqlserver://WMDENTW1\\SQLEXPRESS:1433;" + 
                    "database=FullTextDB;" + 
                    "user=root;" + 
                    "password=root123";
private static final String QUERY = "select FTID, ID, CLASSID, TEXT, PUBNOTICECONTENT, DOCUMENTCONTENT, contentSum_DE from METADATA_FULLTEXT";
public static void main(String[] args) throws Exception {
Lucenetest indexer = new Lucenetest();

//error here

***Directory indexDir = FSDirectory.open(new File(PATH).toPath());***

try {   
// Index Writer 
   Class . forName ( JDBC_DRIVER ). newInstance ();  

   Connection conn = DriverManager.getConnection(CONNECTION_URL); 

   StandardAnalyzer analyzer = new StandardAnalyzer();  

   IndexWriterConfig  config  =  new  IndexWriterConfig ( analyzer );

   IndexWriter index writer =  new  IndexWriter ( indexDir ,  config );  

   System.out.println("Indexing to directory '" + indexDir + "'...");  

   int indexedDocumentCount = indexer.indexDocs1(indexWriter, conn);  

   index writer . close ();  

   System.out.println(indexedDocumentCount + " records have been indexed successfully");

} catch (Exception e) {  
   e.printStackTrace();  
} 
}

@SuppressWarnings("deprecation")
int indexDocs1(IndexWriter writer, Connection conn) throws Exception {  
  String sql = QUERY;  
  Statement stmt = conn.createStatement();  
  ResultSet rs = stmt.executeQuery(sql);  
  int i=0;
  while (rs.next()) {  
     Document d = new Document();  
 d.add(new StringField("FTID", rs.getString("FTID"), StringField.Store.YES));
 d.add(new StringField("ID", rs.getString("ID"), StringField.Store.YES));
 d.add(new StringField("CLASSID", rs.getString("CLASSID"), StringField.Store.YES));
 d.add(new StringField("TEXT", rs.getString("TEXT"), StringField.Store.YES));
 d.add(new StringField("PUBNOTICECONTENT", rs.getString("PUBNOTICECONTENT"), StringField.Store.YES));
 d.add(new StringField("DOCUMENTCONTENT", rs.getString("DOCUMENTCONTENT"), StringField.Store.YES));
 d.add(new StringField("contentSum_DE", rs.getString("contentSum_DE"), StringField.Store.YES));

     writer.addDocument(d);  
     i++;
 }  
  return i;
}


    private static void indexDocs(IndexWriter writer, Connection conn) {
        // TODO Auto-generated method stub

    }
}

I am getting an exception, Indexing to directory:

MMapDirectory@C:\dbindex lockFactory=org.apache.lucene.store.NativeFSLockFactory@4493d195'...
java.lang.IllegalArgumentException: value cannot be null at
org.apache.lucene.document.Field.(Field.java:238) at
org.apache.lucene.document.StringField.(StringField.java:61) at
lucene.Lucenetest.indexDocs1(Lucenetest.java:80) at
lucene.Lucenetest.main(Lucenetest.java:56)

Not really sure what I missed.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
gupta k
  • 23
  • 1
  • 9
  • I am getting error as exception, Indexing to directory 'MMapDirectory@C:\dbindex lockFactory=org.apache.lucene.store.NativeFSLockFactory@4493d195'... java.lang.IllegalArgumentException: value cannot be null at org.apache.lucene.document.Field.(Field.java:238) at org.apache.lucene.document.StringField.(StringField.java:61) at lucene.Lucenetest.indexDocs1(Lucenetest.java:80) at lucene.Lucenetest.main(Lucenetest.java:56) ............... Not really sure what I missed – gupta k Mar 23 '16 at 12:44

1 Answers1

0

Where you have marked this exception occurring in your code doesn't make sense. That exception is being thrown within your IndexDocs1 method.

StringField and TextField will both throw an IllegalArgumentException if the value (or field name) are null. When importing from a nullable field in your database, you should check for null values and either skip the field, or replace it with a default value (using a default value is very useful if you want to be able to query for nulls).

Also: Your Directory should be a directory. The line:

Directory indexDir = FSDirectory.open(new File("C:/dbindex/index.txt").toPath());

will create a new directory called "index.txt", not a file. If such a file already exists, then that line will throw a FileAlreadyExistsException.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • confused: Forget about all these things.. make it clear what are you trying to say...better if you write and show in my code – gupta k Mar 24 '16 at 09:11
  • Not sure how to make it more clear. You need to check for null values when building a document. Simple as that. – femtoRgon Mar 24 '16 at 15:31