1. 程式人生 > >lucene 對索引的文件進行新增 修改 刪除

lucene 對索引的文件進行新增 修改 刪除

第一步:新增相關的依賴

<!-- https://mvnrepository.com/artifact/junit/junit -->(新增  junit依賴)
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>

</dependency>

第二步:準備luke外掛 

找到個luke所有版本的下載:https://github.com/DmitryKey/luke/releases

Luke是一個用於Lucene搜尋引擎的,方便開發和診斷的第三方工具,它可以訪問現有Lucene的索引,並允許您顯示和修改。

第三步:新建一個類  IndexingTest

/**
 * 對索引的文件進行新增  修改  刪除
 * @author 王晨
 *
 */
public class IndexingTest {

private String[] ids = {"1","2","3"};

private String[] citys = {"beijing","shanghai","guangzhou"};
private String[] descs = {
"beijing is a beautiful city",
"shanghai is a city of culture",
"guangzhou is a bulsting city"
};

private Directory dir;

/**
* 每次執行時都會執行一次寫索引
* @throws Exception
*/
@Before
public void setUp() throws Exception {
//獲取到索引輸出的目錄
dir = FSDirectory.open(Paths.get("D:\\lucene2"));
IndexWriter writer = getWriter();
//新建document物件  將資料加入
for(int i = 0; i < ids.length; i++){
Document document = new Document();
document.add(new StringField("id", ids[i], Field.Store.YES));
document.add(new StringField("city", citys[i], Field.Store.YES));
document.add(new TextField("desc",descs[i], Field.Store.NO));
writer.addDocument(document);
}
writer.close();
}

/**
* 獲取到IndexWriter的例項
* @return
*/
private IndexWriter getWriter() throws Exception{
// TODO Auto-generated method stub
Analyzer analyzer = new StandardAnalyzer();  //標準分詞器
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, conf);
return writer;
}

/**
* 測試一共寫了多少個文件
* @throws Exception
*/
@Test
public void testIndexWriter() throws Exception{
IndexWriter writer = getWriter();
System.out.println("一共寫入了"+writer.numDocs()+"個文件");
writer.close();
}

/**
* 測試讀取文件
* @throws Exception
*/
@Test
public void testIndexReader() throws Exception{
IndexReader reader = DirectoryReader.open(dir);
System.out.println("最大文件數:"+reader.maxDoc());
System.out.println("實際文件數:"+reader.numDocs());
reader.close();
}

/**
* 測試刪除文件  在合併前
* @throws Exception
*/
@Test
public void testDeleteBeforeMerge() throws Exception{
IndexWriter writer = getWriter();
System.out.println("再刪除前一個存在的文件數"+writer.numDocs());
writer.deleteDocuments(new Term("id","1"));
writer.commit();
System.out.println("刪除後最大存在的文件數:"+writer.maxDoc());
System.out.println("刪除後實際存在的文件數:"+writer.numDocs());
writer.close();
}


/**
* 測試刪除文件  在合併後
* @throws Exception
*/
@Test
public void testDeleteAfterMerge() throws Exception{
IndexWriter writer = getWriter();
System.out.println("在刪除前一個存在的文件數"+writer.numDocs());
writer.deleteDocuments(new Term("id","1"));
writer.forceMergeDeletes();   //強制合併索引    資料量大時不建議進行此操作
writer.commit();
System.out.println("刪除後最大存在的文件數:"+writer.maxDoc());
System.out.println("刪除後實際存在的文件數:"+writer.numDocs());
writer.close();
}

/**
* 測試更新
* @throws Exception
*/
@Test
public void testUpdate() throws Exception{
IndexWriter writer = getWriter();
Document doc = new Document();
doc.add(new StringField("id", "beijing", Field.Store.YES));
doc.add(new StringField("city", "beijing", Field.Store.YES));
doc.add(new TextField("desc", "bbbjjj is city", Field.Store.NO));
writer.updateDocument(new Term("id","1"), doc);    //底層是先刪除  在新增
writer.close();




}