[Java] 纯文本查看 复制代码
//创建索引
public void createIndex()throws Exception{
IndexWriter indexWriter = getIndexWriter();
File sourceFile = new File("E:\\项目二\\lucene\\day06\\资料\\searchsource");
for (File file :sourceFile.listFiles() ) {
String fileName = file.getName();
String filePath = file.getPath();
long size = FileUtils.sizeOf(file);
String fileContent = FileUtils.readFileToString(file);
Document document = new Document();
TextField fileNameField = new TextField("name", fileName, Store.YES);
StoredField pathField = new StoredField("path", filePath);
LongField sizeField = new LongField("size", size, Store.YES);
TextField contentFeild = new TextField("content",fileContent,Store.NO);
document.add(fileNameField);
document.add(pathField);
document.add(sizeField);
document.add(contentFeild);
indexWriter.addDocument(document);
}
indexWriter.close();
}
//查询索引
public void queryIndex() throws Exception{
Directory directory = FSDirectory.open(new File("E:\\index"));
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//term查询相当于等值查询
Query query = new TermQuery(new Term("name", "lucene"));
TopDocs topDocs = indexSearcher.search(query , 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int id = scoreDoc.doc;
Document document = indexSearcher.doc(id);
System.out.println(document.getField("name"));
System.out.println(document.getField("path"));
System.out.println(document.getField("content"));
System.out.println(document.getField("size"));
}
indexReader.close();
}
// 添加文档
public void addDocument() throws Exception {
IndexWriter indexWriter = this.getIndexWriter();
Document document = new Document();
TextField fileNameField = new TextField("name", "测试文档.txt", Store.YES);
StoredField pathField = new StoredField("path", "c:\\temp");
LongField sizeField = new LongField("size", 1111, Store.YES);
document.add(fileNameField);
document.add(pathField);
document.add(sizeField);
indexWriter.addDocument(document);
indexWriter.close();
}
// 根据query删除文档
public void deleteByQuery()throws Exception {
IndexWriter indexWriter = this.getIndexWriter();
Query query = new TermQuery(new Term("name","测试"));
indexWriter.deleteDocuments(query);
indexWriter.close();
}
// 删除全部文档
public void deleteAll()throws Exception {
IndexWriter indexWriter = this.getIndexWriter();
indexWriter.deleteAll();
indexWriter.close();
}
//更新文档
//先删除,然后再添加
public void update() throws Exception{
IndexWriter indexWriter = this.getIndexWriter();
Term term = new Term("name","spring");
Document doc = new Document();
TextField fileNameField = new TextField("name", "测试文档111.txt", Store.YES);
StoredField pathField = new StoredField("path", "c:\\temp");
LongField sizeField = new LongField("size", 1111, Store.YES);
doc.add(fileNameField);
doc.add(pathField);
doc.add(sizeField);
indexWriter.updateDocument(term, doc);
indexWriter.close();
}
//创建索引写入器
private IndexWriter getIndexWriter() throws IOException {
//指定索引库存放路径 ,可以存放在内存也可以存放在硬盘,一般存放在硬盘上。
Directory directory = FSDirectory.open(new File("E:\\index"));
//创建ik分词器 ik分词器即支持中文也支持英文
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
//创建索引写入器
IndexWriter indexWriter = new IndexWriter(directory, config);
return indexWriter;
}