Lucene Field的常用类型

通过代码我们发现,我们所有的索引都是用的TextField方法,我们经常要对索引进行增删改查,如果都是用TextField,比如文件的路径,我们不会对路径进行分词,是否有索引也没有那么重要,只需要存储即可,那么如果这个时候还是用TextFiled会不会有那么些浪费?Field有多种方法,我们来看一下.
image.png
可以将我们的代码进行改造一下:
package com.itheima;


import org.apache.commons.io.FileUtils;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;

/**
 * @ClassName lucene
 * @Description TODO
 * @Author gkz
 * @Date 2019/8/21 18:02
 * @Version 1.0
 **/
public class luceneFirst {

    @Test
    public void createIndex() throws Exception{
//        1.创建一个Director对象,指定索引库的位置。

        //把索引保存在内存中
//        Directory dictionary=new RAMDirectory();

        //把索引保存在磁盘中
          Directory directory= FSDirectory.open(new File("E:\\Desktop").toPath());
//        2.基于Directory对象来创建一个indexWriter对象
        IndexWriterConfig config=new IndexWriterConfig(new IKAnalyzer());
          IndexWriter indexWriter=new IndexWriter(directory,config);
//        3.读取磁盘上的文件,对应每个文件创建一个文档对象。
          File file=new File("E:\\Desktop\\87.lucene\\lucene\\02.参考资料\\searchsource");
          File[] files=file.listFiles();
        for (File file1 : files) {
            //取文件名
            String file1Name=file1.getName();
            //文件的路径
            String path=file1.getPath();
            //文件的内容
            String fileContext = FileUtils.readFileToString(file1, "utf-8");
            //文件的大小
            long size = FileUtils.sizeOf(file1);
            //创建Field
            //参数1:域的名称,参数2:域的内容,参数3:是否储存
            Field fieldName=new TextField("name",file1Name, Field.Store.YES);
            Field fieldPath=new StoredField("path",path);
            Field fieldContext=new TextField("context",fileContext, Field.Store.YES);
            Field fieldSizeValue=new LongPoint("size",size);
            Field fieldSizeStore=new StoredField("size",size);
            //创建文档对象
            Document document=new Document();
//        4.向文档对象中添加域
            document.add(fieldName);
            document.add(fieldPath);
            document.add(fieldContext);
            document.add(fieldSizeValue);
            document.add(fieldSizeStore);
//        5.把文档对象写入索引库
            indexWriter.addDocument(document);
        }
//        6.关闭indexWriter对象
            indexWriter.close();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.Lucene简介 Lucene是apache下的一个开源的全文检索引擎工具包。 1.1.全文检索(Full-t...
    唯死撑尔阅读 546评论 0 0
  • 目录结构:1.全文检索 2.Lucene入门3.Lucene进阶 全文检索 一, 生活中的搜索:1.Win...
    CoderZS阅读 1,736评论 0 12
  • 1、IndexWriter详解 问题1:索引创建过程完成什么事? 分词、存储到反向索引中。 Lucene索引创建A...
    WinnXUDONG阅读 2,135评论 0 0
  • 常用概念: 自然语言处理(NLP) 数据挖掘 推荐算法 用户画像 知识图谱 信息检索 文本分类 常用技术: 词级别...
    御风之星阅读 9,334评论 1 25
  • 概述:本题是pwn的入门级题目,几乎把所有利用的难度都降到最低,应该只是用来让入门者大致了解pwn题的玩法。 1、...
    就叫rafa阅读 1,070评论 0 1