有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Lucene索引的增量更新

我对Lucene有点陌生,我使用的是一个巨大的数据库,我之前对它进行了索引。问题是,如果每次向整个表/数据库中添加新的内容,它并不是一种有效的方法来索引整个表/数据库。我在用lucene3。6.2. 我想制作一个索引函数,将新数据添加到现有的Lucene索引文件中,而无需更新文档(或在Lucene中删除并重新索引)。我的意思是,它不应该创建新文件来存储新文档,而应该将它们插入到以前的索引文件中,而不删除索引文件中以前的数据,也不重新索引整个数据库。其索引应该从先前索引项的最后一个索引位置开始,并且应该可以与先前生成的索引一起搜索。这是我创建索引的索引器代码:

public String TestIndex() throws IOException,SQLException
{
     System.out.println("preparing dictionary");
     String output="";
     Long i=0l;
     ResultSet rs = null;
     URL u = this.getClass().getClassLoader(). getResource(SearchConstant.INDEX_DIRECTORY_DICTIONARYDETAILS);
     String dirLoc = u.getPath().replace("%20", " ");
     Directory index = FSDirectory.open(new File(dirLoc));                                                                                //new RAMDirectory();
     StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
     IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_30,analyzer);
     config.setOpenMode(OpenMode.CREATE);
     IndexWriter w = new IndexWriter(index, config);

     try {
         String SQL = "Select * from test";

         cm = new DbUtility();
         rs = cm.getData(SQL);

         // 1. create the index
         while (rs.next()) {
            Document doc = new Document();

            doc.add(new Field("id",rs.getObject(1).toString() , Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("Heading",rs.getObject(2).toString() , Field.Store.YES, Field.Index.ANALYZED));

              w.addDocument(doc);
              i = i + 1;
           }

            System.out.println("I " + i.toString());
        }
        catch (Exception e) {
            System.out.println("I in Error " + i.toString());
            System.out.println("Error while retrieving data: "+e.getMessage());
        }

        w.close();
        rs.close();

        return output;
   }

共 (0) 个答案