建立索引:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
public class TextFileIndexer
{
public static void main(String[] args) throws Exception
{
/**//* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */
File fileDir = new File("c:\\s");
/**//* 这里放索引文件的位置 */
File indexDir = new File("index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true, MaxFieldLength.UNLIMITED);
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
// 增加document到索引去
for (int i = 0; i < textFiles.length; i++)
{
if (textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt"))
{
System.out.println(" File " + textFiles[i].getCanonicalPath() + " 正在被索引 . ");
String temp = FileReaderAll(textFiles[i].getCanonicalPath(), "GBK");
System.out.println(temp);
Document document = new Document();
Field FieldPath = new Field("path", textFiles[i].getPath(), Field.Store.YES, Field.Index.NO);
Field FieldBody = new Field("body", temp, Field.Store.YES, Field.Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
// optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out.println(" 这花费了 " + (endTime - startTime) + " 毫秒来把文档增加到索引里面去! " + fileDir.getPath());
}
public static String FileReaderAll(String FileName, String charset) throws IOException
{
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;
}
}
分享到:
相关推荐
lucene jar 2.4.0使用jar包加高亮包.外加commons-logging-1.0.4包
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎
lucene 2.4.0 最新jar包!
基于SpringBoot编写的一个Lucene测试Demo把匹配到的结果高亮摘要显示在前端jsp上
NULL 博文链接:https://navylee.iteye.com/blog/740128
lucene-2.4.0-src.zip
java(结合lucene)版的公交搜索系统(javaWeb+lucene+jsp+tomcat+eclipse/idea) java(结合lucene)版的公交搜索系统(javaWeb+lucene+jsp+tomcat+eclipse/idea) java(结合lucene)版的公交搜索系统(javaWeb+lucene+...
lucene-core-2.4.0.jar,lucene-analyzer-2.4.0.jar,lucene-highlighter-2.4.0.jar,je-analysis-1.5.3.jar
lucene-highlighter-2.4.0.jar lucene highlighter
lucene-2.4.0 自己下的 !不知道对不对 lucene-2.4.0 自己下的 ! 可以用 我已经试过了 解压之后 更改后缀 再解压一次
lucene 对 xml建立索引 建立索引就是怎么简单 呵呵
Lucene全文检索框架+Solr搜索引擎(2018版.Java)Lucene实战应用
lucene3.0庖丁+索引搜索程序,里面有代码示例
Lucene.Net 2.0 源码+文档