lucene7 创建索引

//创建索引

public static <T> ResultVO<T> addTest (String path) {
        IndexWriter writer=null;
        try {
            Directory dir =FSDirectory.open(Paths.get(path));
            //指定一个分词器
            StandardAnalyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig config=new IndexWriterConfig(analyzer);
            writer=new IndexWriter(dir,config);
            
            Document doc=new Document();
            //数字类型使用point添加到索引中,同时如果需要存储,由于没有Stroe,所以需要再创建一个StoredField进行存储
            //IntPoint,DoublePoint,LongPoint等
            doc.add(new IntPoint("count", 1));
            //存储
            doc.add(new StoredField("count",1));
            //添加排序支持
            doc.add(new NumericDocValuesField("count",1));
            
            //分别设置存储和索引
            FieldType fieldType = new FieldType();
            fieldType.setStored(true); // 设置为true,存储此字段
            fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);//索引设置
            doc.add(new Field("id_1", UUID.randomUUID().toString(), fieldType));
            //存储+索引
            doc.add(new TextField("id_2", UUID.randomUUID().toString(),Store.YES));
            //将doc对象写入索引库,此过程创建索引,并将索引和文档对象写入索引库
            writer.addDocument(doc);
            writer.close();
            return new ResultVO<T>(true,"创建成功");
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO<T>(false,"创建失败:"+ e.getMessage());
        }
    }

public static void main(String[] args) throws IOException {
        //索引存储路径
        String path = "E:\\\\message";
        addTest(path);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值