繁体   English   中英

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 子类之一而不是Field() 在您的情况下,如果您希望为全文搜索索引字段内容,则为TextField() ,如果您希望字段保持UN_TOKENIZED (索引为单个标记的内容),则为StringField() ),例如。

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

最后:

writer.addDocument(doc);

大部分代码不是 Lucene 代码,而是数据库访问代码。 唯一的 Lucene 代码是 while 循环中的三行代码。

这两个错误可能与同一个问题有关,即 while 循环中的第二行需要将getString("RawData")更改为rs.getString("RawData") ,如下所示:

 doc.add(new Field("RawData", rs, rs.getString("RawData"), Field.Store.YES, Field.Index.UN_TOKENIZED));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM