网上随便一搜都能搜到很多关于lucene的教程,这里就不细展开了。简单说下过程:
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new IKAnalyzer(true)); indexWriter = new IndexWriter(directory, indexWriterConfig); Document doc = new Document(); indexWriter.addDocument(doc); indexWriter.commit(); indexWriter.close();
以上是一个完成的lucene创建文档索引的过程,这个过程肯定是没问题的,但是工程应用中,上面的流程肯定是有问题的,尤其是大批量内容初始化lucene索引的时候,如果按照上面那个流程,300w的数据大概需要15天才能写完...
其实本质很简单,索引的创建本质是磁盘的i/o问题,所以想办法提高i/o就行了。
indexWriter.addDocument(doc); 这一步操作是把文档写入内存里,然后indexWriter.commit();就是把内存的东西进行落盘。每执行一次就是一次磁盘写入,所以只要提高内存里的数据量,比如每add 1000个文档才commit一次,这样磁盘的i/o就慢下来了