String conn = "jdbc:mysql://localhost:3306/db_name";
String username = "****";
String pwd = "****";
String sql = "select column_1 from table1";
Directory indexDirectory = FSDirectory.open(Paths.get("C:/index"));
Directory memoryIndex = new RAMDirectory();
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(memoryIndex, indexWriterConfig);
Connection con = DriverManager.getConnection(conn, username, pwd);
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = con.createStatement().executeQuery(sql);
while (rs.next()) {
Document doc = new Document();
doc.add(new StringField("RawData", rs.getString("RawData"), Field.Store.YES));
writer.addDocument(doc);
}
I've tried to follow the tutorial on the internet, one of the tutorials is looks like above. but I don't know what and how Lucene works in java. can someone help me? I am new to Lucene and is there a new way to implement Lucene on java?
and is there any source that could help me understanding Lucene from basic. most of the tutorials just use file streams, not a database. I want to know how if I use the database in Lucene. I want to extract the data from the database using Lucene.
You missed the most important steps :
Specify the index directory :
// eg. using filesystem
Directory indexDirectory = FSDirectory.open(Paths.get("C:/DEV/index"));
// ... or using RAM
Directory memoryIndex = new RAMDirectory();
Use an analyzer
:
StandardAnalyzer analyzer = new StandardAnalyzer();
Create an indexWriterConfig
with that analyzer :
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
Create the actual index writer using the writer config and index directory objects :
IndexWriter writer = new IndexWriter(memoryIndex, indexWriterConfig);
Now your writer should be able to index documents properly. Use Field()
and Document()
to build up the content to be indexed.
If you are not familiar with Lucene, you may prefer using one of the Field subclasses instead of Field()
directly. In your case, it would be TextField()
if you want the field content indexed for full-text search, or StringField()
if you want the field to stay UN_TOKENIZED
(content indexed as a single token), eg. :
Document doc = new Document();
doc.add(new StringField("RawData", rs.getString("RawData"), Field.Store.YES));
And, finally :
writer.addDocument(doc);