使用Lucene的java api 寫入和讀取索引庫

來源:互聯網
上載者:User

標籤:new   nio   parse   .text   lis   arc   path   rect   comm   

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Day01 {
public String dataDir="E:\\data";
//2.search檢索
@Test
public void search() throws IOException, ParseException {
//01.
Path path=Paths.get("./luceneindex/");
Directory directory=FSDirectory.open(path);
IndexReader indexReader=DirectoryReader.open(directory);
//索引查詢工具
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
String word="戰鬥";
QueryParser queryParser=new QueryParser("filename",new StandardAnalyzer());
Query query=queryParser.parse(word);
TopDocs topDocs = indexSearcher.search(query, 10);

long count = topDocs.totalHits;
System.out.println("總記錄數:"+count);

ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc item:scoreDocs) {
int docid = item.doc; //文檔編號
Document document = indexReader.document(docid);
String filename = document.get("filename");
System.out.println(filename);
}
}
@Test
//1.建立索引
public void createIndex() throws IOException {
//01.準備一個寫入索引的目錄
Path path = Paths.get("./luceneindex/");
//02.將路徑與Directory進行綁定
Directory directory=FSDirectory.open(path);
//03.這行code在底層預設已經關聯綁定了一個StandardAnalyzer 標準分詞器
IndexWriterConfig config=new IndexWriterConfig();
//04.給config設定一個模式 Create:每次都抹掉原來的索引檔案,重新構建新的索引檔案
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
//05.索引寫入器需要兩個入參,一個是目錄,另一個是配置
IndexWriter writer=new IndexWriter(directory,config);
//06.準備加入索引庫的內容
File file = new File(dataDir);
//07.某個目錄下的檔案集合
File[] files = file.listFiles();
for (File item:files) {
//08.將內容幻化成----->Document
Document document=new Document();
String filename=item.getName(); //file--->String
System.out.println(filename);
System.out.println("===============================");
String filecontent=FileUtils.readFileToString(item);
Long modifydate=item.lastModified();
//09.真正的給Document的field賦值
document.add(new TextField("filename",filename,Field.Store.YES));
document.add(new TextField("filecontent",filecontent,Field.Store.YES));
document.add(new NumericDocValuesField("modifydate",modifydate));
//10.將單個Document(一條記錄) 儲存到 索引庫中
writer.addDocument(document);
}
//11.關閉索引庫
writer.close();
System.out.println("add indexes ok!");
}
}

使用Lucene的java api 寫入和讀取索引庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.