Lucene创建索引的优化

/**
	 * 操作索引库是用来(增删改)索引
	 * 
	 * @throws Exception
	 */
	@Test
	public void createIndex() throws Exception
	{
		long star = System.currentTimeMillis();
		IndexWriter indexWriter = null;
		RAMDirectory ramDir = null;
		FSDirectory dirs = FSDirectory.getDirectory(indexPath);
		IndexWriter indexWriters = new IndexWriter(dirs, analyzer, MaxFieldLength.LIMITED);
		for (int i = 0; i < 10000; i++)
		{
			File file = new File(indexPath);
			FSDirectory dir = FSDirectory.getDirectory(file);
			Document doc = File2DocumentUtils.file2Document(filePath);
			
			ramDir = new RAMDirectory();
			indexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
			indexWriter.addDocument(doc);
			indexWriter.close();
			indexWriters.addIndexesNoOptimize(new Directory[] {ramDir});
		}
		indexWriters.close();
		long end = System.currentTimeMillis();
		System.out.println("消耗时间:  " + (double) ((end - star) / 1000));
	}

	@Test
	public void createIndex1() throws Exception
	{
		long star = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++)
		{
			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();
		}
		long end = System.currentTimeMillis();
		System.out.println("消耗时间:  " + (double) ((end - star) / 1000));
	}

 

 

你可能感兴趣的:(Lucene)