Lucene의 삭제 수정 작업

2125 단어 Lucene
@Test
	public void saveIndex() throws Exception
	{
		File file = new File(indexPath);
		FSDirectory dir = FSDirectory.getDirectory(file);
		Document doc = File2DocumentUtils.file2Document(filePath);
		IndexWriter indexWriter = new IndexWriter(dir, analyzer, MaxFieldLength.LIMITED);
		indexWriter.addDocument(doc);
		indexWriter.close();
	}

	@Test
	public void deleteIndex() throws Exception
	{
		IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, MaxFieldLength.LIMITED);
		Term term = new Term("path", filePath);
		indexWriter.deleteDocuments(term);
		indexWriter.close();
	}
	
	@Test
	public void updateIndex() throws Exception
	{
		IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, MaxFieldLength.LIMITED);
		Term term = new Term("path", filePath);
		Document doc = File2DocumentUtils.file2Document(filePath);
		// 
		indexWriter.updateDocument(term, doc);
		indexWriter.close();
	}
	@Test
	public void searchIndex() throws Exception
	{
		String queryString = " ";

		//  Query
		String[] fields = {"name", "content"};
		QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
		Query query = queryParser.parse(queryString);

		//  
		IndexSearcher indexSearcher = new IndexSearcher(indexPath);
		Filter filter = null;
		//  List 
		TopDocs topDocs = indexSearcher.search(query, filter, 10000);

		int firstResult = 0;
		int max = 3;
		int end = Math.min(firstResult+max, topDocs.totalHits);
		
		//  
		for (int i=firstResult; i<end;i++)
		{
			ScoreDoc scoreDoc = topDocs.scoreDocs[i];
			int docSn = scoreDoc.doc;//  
			Document doc = indexSearcher.doc(docSn); //  
			File2DocumentUtils.printDocumentInfo(doc);

		}
		System.out.println(" [" + topDocs.totalHits + "] ");
	}

좋은 웹페이지 즐겨찾기