【lucene-plus】初始化索引_shenya2的专栏-CSDN博客
【lucene-plus】索引初始化的实现逻辑_shenya2的专栏-CSDN博客
lucene-plus尽可能的隐藏了索引细节,使之达到“配置一次,循环使用”的效果。下面介绍lucene-plus保存文档的实现逻辑。
核心代码:
public void adddocument(String indexName, Map params) throws IOException {
FieldInfoDTO fieldInfoDTO = this.createFields(indexName, params);
Map analyzerMap = fieldInfoDTO.getAnalyzerMap();
// 初始化writer
PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(), analyzerMap);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(wrapper);
Directory directory = this.indexPlusService.getDirectory(indexName);
try (IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig)) {
document document = new document();
fieldInfoDTO.getFieldList().forEach(document::add);
// 保存文档
indexWriter.adddocument(document);
indexWriter.commit();
}
}
| 参数 | 描述 |
| indexName | 索引名称 |
| params | 文档参数,key:字段名称;value:字段值 |
这个方法的核心逻辑在于:
FieldInfoDTO fieldInfoDTO = this.createFields(indexName, params);
这行代码完成了数据到文档的转换。
lucene-plus目前支持的字段类型:LONG、DOUBLE、INTEGER、TEXT、STRING,如果需要扩展字段类型,可参考枚举:FieldTypeEnum,并需要同时扩展forField方法:
ublic enum FieldTypeEnum {
STRING("String", String.class),
INTEGER("Integer", Integer.class),
LONG("Long", Long.class),
DOUBLE("Double", Double.class),
TEXT("Text", String.class);
private String type;
private Class> javaClazz;
FieldTypeEnum(String type, Class> javaClazz) {
this.type = type;
this.javaClazz = javaClazz;
}
public String getType() {
return type;
}
public static List forField(String fieldName, Object value, FieldTypeEnum typeEnum, Store store,
Boolean isPoint) {
List fieldList = CollUtil.newArrayList();
switch (typeEnum) {
case LONG:
if (Boolean.TRUE.equals(isPoint)) {
fieldList.add(new LongPoint(fieldName, Long.parseLong(value.toString())));
} else {
fieldList.add(new NumericDocValuesField(fieldName, Long.parseLong(value.toString())));
}
if (Store.YES.equals(store)) {
fieldList.add(new StoredField(fieldName, Long.parseLong(value.toString())));
}
break;
case DOUBLE:



