学习lucene一

本文介绍了使用Lucene进行索引的写入与读取操作,包括如何通过Analyzer分词器进行分词处理,利用IndexWriterConfig配置索引写入器,并通过Field字段和Document文档对象构建索引数据。

索引的写入

  • 需要了解的一些概念:
    • Analyzer分词器

    • IndexWriter索引写入

    • IndexWriterConfig索引写入配置

    • FieldType字IndexWriterConfig段类型

    • Field字段

    • Document文档

    • Directory目录

      @Test
      public void testWriter() throws Exception{
      //打开一个文件目录
      Directory d=FSDirectory.open(new File("writer"));
      //初始化词法分析器
      Analyzer analyzer=new SimpleAnalyzer();
      //初始化索引写入器配置
      IndexWriterConfig conf=new IndexWriterConfig(Version.LATEST, analyzer);
      //创建索引写入器
      IndexWriter writer =new IndexWriter(d, conf);
      //创建一个索引字段
      FieldType type=new FieldType();
      type.setStored(true);//是否存储
      type.setTokenized(true);//是否分词
      type.setIndexed(true);//是否创建索引
      Field field1=new Field("id","1",type) ;
      Field field2=new Field("title","i am title1",type) ;
      Field field3=new Field("content","hello java",type) ;
      //创建一个文档对象
      Document doc=new Document();
      doc.add(field1);
      doc.add(field2);
      doc.add(field3);
      Field field4=new Field("id","2",type) ;
      Field field5=new Field("title","i am title2",type) ;
      Field field6=new Field("content","hello lucene",type) ;
      //创建一个文档对象
      Document doc2=new Document();
      doc2.add(field4);
      doc2.add(field5);
      doc2.add(field6);
      writer.addDocument(doc);
      writer.addDocument(doc2);
      writer.commit();
      writer.close();
      }
      复制代码

##索引的读取

  • 需要了解的一些概念
    • IndexReader索引读
    • IndexSeacher索引搜索
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值