lucene创建索引

来源:互联网 发布:2017网络新技术 编辑:程序博客网 时间:2024/05/21 06:37
package org.lucene.createindex;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.LockObtainFailedException;import org.apache.lucene.util.Version;import org.wltea.analyzer.lucene.IKAnalyzer;public class CreateIndex {public static void main(String[] args) throws CorruptIndexException,LockObtainFailedException, Exception {// 1.创建索引的存放地File index = new File("C:\\suoyin");IKAnalyzer ikAnalyzer = new IKAnalyzer();IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,ikAnalyzer).setOpenMode(OpenMode.CREATE);IndexWriter indexWriter = new IndexWriter(FSDirectory.open(index), iwc);//2.获取本地文件夹中的文件File[] textFiles = new File("C:\\source").listFiles();//3循环文件判断是不是.txt文件for (int j = 0; j < textFiles.length; j++) {if (textFiles[j].isFile() && textFiles[j].getName().endsWith(".txt")) {//4如果是的话就读取里面的文本,并且new一个document(Document document = new Document();)String fileContent = FileReaderAll(textFiles[j].getCanonicalPath(),"GBK");//5new Document()新建一个文档Document document = new Document();Field FieldBody = new Field("fileContent", fileContent, Field.Store.YES,   Field.Index.ANALYZED);document.add(FieldBody);Field fileName = new Field("mm", textFiles[j].getName(), Field.Store.NO,   Field.Index.NOT_ANALYZED);document.add(fileName);//6.最后往索引文件夹里面写索引indexWriter.addDocument(document);}}System.out.println("创建索引成功");indexWriter.close();   // 这里不关闭建立索引会失败 }private static String FileReaderAll(String FileName, String charset) throws Exception {BufferedReader reader = new BufferedReader(new InputStreamReader(   new FileInputStream(FileName), charset));   String line = new String();   String temp = new String();    while ((line = reader.readLine()) != null) {   temp += line;   }    reader.close();   return temp; }}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 普票抬头开错了怎么办 发票号码打错了怎么办 个人税票开错了怎么办 普票地址错了怎么办 专票税率开高了怎么办 专票税率开错了怎么办 发票姓名写错了怎么办 首付发票姓名写错怎么办 一般纳税人税率开错了怎么办 电子税票开错了怎么办 税率是6%开成3%怎么办 医院收费收错了怎么办 发票少打一个字怎么办 增值税发票抬头错了一个字怎么办 暂估入账跨年了怎么办 电子发票写错了怎么办 发票纳税人识别号写错了怎么办 电子发票忘填邮箱怎么办 公司名跟发票抬头不一样怎么办 买空调不给发票怎么办 卖苹果手机没有发票怎么办 刷机字库刷坏了怎么办 维棠不能播放视频怎么办 m3u8卡顿的厉害怎么办 下载了PDF不能用怎么办 pdf格式在电脑打不开怎么办 电脑上arm格式打不开怎么办 极米桌面无响应怎么办 jpg格式的文件打印模糊怎么办 苹果迅雷mp4格式看不了怎么办 太阳镜镜片磨花了怎么办 ∪盘插上电脑读不出怎么办 网站被路由器屏蔽了怎么办 电脑无法进入路由器设置页面怎么办 笔记本电脑无线网卡坏了怎么办 笔记本内置无线网卡坏了怎么办 笔记本无线网卡坏了怎么办 wif连上不能上网怎么办 手机wifi有个感叹号怎么办 分手不删qq微信怎么办 优盘提示被锁了怎么办