用lucene爲數據庫搜索建立增量索引
用 lucene 建立索引不可能每次都重新開始建立,而是按照新增加的記錄,一次次的遞增
建立索引的IndexWriter類,有三個參數
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),isEmpty);
其中第三個參數是bool型的,指定它可以確定是增量索引,還是重建索引.
對於從數據庫中讀取的記錄,譬如要爲文章建立索引,我們可以記錄文章的id號,然後下次再次建立索引的時候讀取存下的id號,從此id後往下繼續增加索引,邏輯如下.
建立增量索引,主要代碼如下
public void createIndex(String path)
{
Statement myStatement = null;
String articleId="0";
//讀取文件,獲得文章id號碼,這裏只存最後一篇索引的文章id
try {
FileReader fr = new FileReader("**.txt");
BufferedReader br = new BufferedReader(fr);
articleId=br.readLine();
if(articleId==null||articleId=="")
articleId="0";
br.close();
fr.close();
} catch (IOException e) {
System.out.println("error343!");
e.printStackTrace();
}
try {
//sql語句,根據id讀取下面的內容
String sqlText = "*****"+articleId;
myStatement = conn.createStatement();
ResultSet rs = myStatement.executeQuery(sqlText);
//寫索引
while (rs.next()) {
Document doc = new Document();
doc.add(Field.Keyword("**", DateAdded));
doc.add(Field.Keyword("**", articleid));
doc.add(Field.Text("**", URL));
doc.add(Field.Text("**", Content));
doc.add(Field.Text("**", Title));
try{
writer.addDocument(doc);
}
catch(IOException e){
e.printStackTrace();
}
//將我索引的最後一篇文章的id寫入文件
try {
FileWriter fw = new FileWriter("**.txt");
PrintWriter out = new PrintWriter(fw);
out.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ind.Close();
System.out.println("ok.end");
}
catch (SQLException e){
e.printStackTrace();
}
finally {
//數據庫關閉操作
}
}
然後控制是都建立增量索引的時候根據能否都到id值來設置IndexWriter的第三個參數爲true 或者是false
boolean isEmpty = true;
try {
FileReader fr = new FileReader("**.txt");
BufferedReader br = new BufferedReader(fr);
if(br.readLine()!= null) {
isEmpty = false;
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
writer = new IndexWriter(Directory, new StandardAnalyzer(),isEmpty);