简单、通用的Lucene 索引管理

[b]目的[/b]
一般我们存储在数据库中的数据,都可以由pojo类表示。通用的Lucene索引管理就是要能够对所有的pojo能够实行Luncene索引。

[b]用法[/b]
 
//建立属性映射列表
List list = new ArrayList();
FieldAttribute nameAttr = new FieldAttribute("name", "name", "YES", "TOKENIZED");
//这是一个嵌套属性.
FieldAttribute nestedAttr = new FieldAttribute("txt", "nested.txt", "YES", "TOKENIZED");

list.add(nameAttr);
list.add(nestedAttr);

//建立主键属性映射
FieldAttribute idAttr = new FieldAttribute("id", "id", "YES", "UN_TOKENIZED");
//建立索引管理器
IndexManager engine = new IndexManager("c:/index", list, idAttr, null);
TestEntity data=new TestEntity(1, "自相矛盾");
data.setNested(new NestedObject("楚人有鬻盾与矛者,誉之曰:“吾盾之坚,物莫能陷也。”又誉其矛曰:“吾矛之利,于物无不陷也。”或曰:“以子之矛陷于之盾,何如?”其人弗能应也。——《韩非子》"));
TestEntity[] datas = new TestEntity[1];
datas[0] = data;
//创建索引
engine.createIndex(datas);

//添加索引
TestEntity entity = new TestEntity(2, "刻舟求剑");
entity.setNested(new NestedObject(" 楚人有涉江者,其剑自舟中坠于水,遽契其舟曰:'是吾剑之所从坠。'舟止,从其所契者入水求之。舟已行矣,而剑不行,求剑若此,不亦惑乎? —— 战国·吕不韦《吕氏春秋·察今》"));
engine.addIndex(entity);

//更新索引
TestEntity entity = new TestEntity(1, "新自相矛盾");
entity.setNested(new NestedObject("楚人"));
engine.updateIndex(entity);
//删除索引
engine.deleteIndex(new TestEntity(1));



[b]简单、通用的Lucene 索引管理包含的类[/b]


[b]FieldAttribute[/b]


它的功能是映射实体,并能够根据实体对象,得到一个Lucene的Field。

public class FieldAttribute {
private String fieldName;
//实体的属性,可以是简单属性,也可以使嵌套属性。
private String entityProperty;
//对应 Field.Store
private Field.Store storeStyle;
//对应 Field.Index
private Field.Index indexStyle;
//根据实体对象,得到一个Lucene的Field
public Field getField(Object entity) {
....
}
}



[b]IndexManager[/b]

索引管理类,负责创建、更新、删除索引。
public class IndexManager {
private String indexDir;
//对应于实体对象的除主键外的其他属性
private Map fieldAttributeMap = new HashMap();
//对应于实体对象的主键
private FieldAttribute keyFieldAttribute;
private Analyzer analyzer;
...................
/**
* 创建索引
* @param entities
*/
public synchronized void createIndex(E[] entities) {
.....
}
/**
* 添加索引
* @param entities
*/
public synchronized void addIndex(E[] entities) {
......
}
/**
* 添加索引
* @param entitie
*/
@SuppressWarnings("unchecked")
public synchronized void addIndex(E entity) {
.....
}
/**
* 删除索引
* @param entity
*/
public synchronized void deleteIndex(E entity){
......
}
/**
* 更新索引
* @param entity
*/
public synchronized void updateIndex(E entity){
..........
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值