栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【lucene-plus】保存文档的实现逻辑

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【lucene-plus】保存文档的实现逻辑

【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:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/319369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号