使用 apache lucene 的更新方式

The updated way of using apache lucene

String conn = "jdbc:mysql://localhost:3306/db_name";
String username = "****";
String pwd = "****";
String sql = "select coloumn_1 from table1";

   Directory indexDirectory = FSDirectory.open(Paths.get("C:/index"));  
Directory memoryIndex = new RAMDirectory();

            StandardAnalyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter writer = new IndexWriter(memoryIndex, indexWriterConfig);
            Connection con = DriverManager.getConnection(conn, username, pwd);
            PreparedStatement ps = con.prepareStatement(sql); 
                ResultSet rs = con.createStatement().executeQuery(sql);
                while (rs.next()) {
                    Document doc = new Document();
                    doc.add(new StringField("RawData", rs.getString("RawData"), Field.Store.YES));

                    writer.addDocument(doc);
                }

我试着按照互联网上的教程进行操作,其中一个教程与上图类似。但我不知道 Lucene 在 java 中的工作原理和工作原理。有人能帮我吗?我是 Lucene 的新手,是否有在 java 上实施 Lucene 的新方法? 是否有任何资源可以帮助我从基础理解 Lucene。大多数教程只使用文件流,而不是数据库。我想知道如何在 Lucene 中使用数据库。我想使用 Lucene 从数据库中提取数据。

您错过了最重要的步骤:

  • 指定索引目录:

    // eg. using filesystem 
    Directory indexDirectory = FSDirectory.open(Paths.get("C:/DEV/index"));     
    // ... or using RAM
    Directory memoryIndex = new RAMDirectory();
    
  • 使用 analyzer :

    StandardAnalyzer analyzer = new StandardAnalyzer();
    
  • 用那个分析器创建一个 indexWriterConfig :

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    
  • 使用编写器配置和索引目录对象创建实际的索引编写器:

    IndexWriter writer = new IndexWriter(memoryIndex, indexWriterConfig);
    

现在您的作者应该能够正确地索引文档了。使用 Field()Document() 构建要索引的内容。

如果您不熟悉 Lucene,您可能更喜欢使用 Field subclasses 之一而不是直接使用 Field()。在您的情况下,如果您希望为全文搜索索引字段内容,它将是 TextField(),如果您希望该字段保持 UN_TOKENIZED(内容索引为单个标记),则为 StringField() ),例如。 :

Document doc = new Document();
doc.add(new StringField("RawData", rs.getString("RawData"), Field.Store.YES));

最后:

writer.addDocument(doc);