目的:公司最近要求建立对数据库内多表的索引文件,来实现对数据库数据处理的全文检索。
表有多个,内容风别是不同表内的字段。选用了IK分词和Lucene实现。中间封装方法很简单,所以就很简单的掠过了
步骤基本为:1.数据库提取数据转为已实例化的对象。
2.将实例化的对象,存索引。此处,是一个字段即为一个Field .基本为:document.add(new StringField(“这里实例化对象的属性(即字段名)”, “字段值”, Store.YES));这里的StringField,即在新版LUC内是只存储不对其分词但将其作为索引的建立索引方式。而TextField则是存储索引,且对其分词且索引。还有StoreField则是只存储不索引不分词。
3.写提交indexWriter每1000次提交一次。
先上代码。(很抱歉不大会用这个写博客,就先这么写上了。改天再研究)
/**
* 循环多线程创建索引
*/
@Override
public void CreateIndexFullSearch() {
ExecutorService executor = Executors.newCachedThreadPool();
// 获得所有源表名
Set<String> tableSourceList = FieldsTranslator.signInfoMap.keySet();
//因为此处是自己创建的一个翻译类内的内容,MAp(“数据库内的表名(也可以称之为数据库转存对象的对象名)”,“中文翻译,例如基本信息”)
//此处的遍历循环创建索引,采用线程池,加快其建立索引的速度。
for (String tablename : tableSourceList) {
CreateIndexSearch myThread = new CreateIndexSearch(tablename);
executor.execute(myThread);
}
executor.shutdown();
}
public class CreateIndexSearch extends Thread {
//因为在创建所以的时候需要传表名来获取该表内容的存储位置及该表内字段,但在线程重写的Run方法内是无参的,所以我们需要在内部类创建的时候就传入这个在内部可调用的参。
private String tableSource;
/**
* @param tableSoure
*/
public CreateIndexSearch(String tableSource) {
super();
this.tableSource = tableSource;
}
/**
* @return the tableSource
*/
public String getTableSource() {
return tableSource;
}
/**
* @param tableSource
* the tableSource to set
*/
public void setTableSource(String tableSource) {
this.tableSource = tableSource;
}
//上面这部分的构造其实只要有类构造方法就行,但下部的G/S方法涉及到下面的值获取。
/**
* 用于创建给定来源表的索引
*
* @param tableSource
* @throws IOException
*/