• <bdo id='1pbmT'></bdo><ul id='1pbmT'></ul>

    1. <i id='1pbmT'><tr id='1pbmT'><dt id='1pbmT'><q id='1pbmT'><span id='1pbmT'><b id='1pbmT'><form id='1pbmT'><ins id='1pbmT'></ins><ul id='1pbmT'></ul><sub id='1pbmT'></sub></form><legend id='1pbmT'></legend><bdo id='1pbmT'><pre id='1pbmT'><center id='1pbmT'></center></pre></bdo></b><th id='1pbmT'></th></span></q></dt></tr></i><div id='1pbmT'><tfoot id='1pbmT'></tfoot><dl id='1pbmT'><fieldset id='1pbmT'></fieldset></dl></div>
    2. <tfoot id='1pbmT'></tfoot>

        <legend id='1pbmT'><style id='1pbmT'><dir id='1pbmT'><q id='1pbmT'></q></dir></style></legend>

        <small id='1pbmT'></small><noframes id='1pbmT'>

        使用 Apache Lucene 索引 MySQL 数据库,并使它们保持

        时间:2023-09-29

          <tbody id='jpeYx'></tbody>
          <bdo id='jpeYx'></bdo><ul id='jpeYx'></ul>
        • <i id='jpeYx'><tr id='jpeYx'><dt id='jpeYx'><q id='jpeYx'><span id='jpeYx'><b id='jpeYx'><form id='jpeYx'><ins id='jpeYx'></ins><ul id='jpeYx'></ul><sub id='jpeYx'></sub></form><legend id='jpeYx'></legend><bdo id='jpeYx'><pre id='jpeYx'><center id='jpeYx'></center></pre></bdo></b><th id='jpeYx'></th></span></q></dt></tr></i><div id='jpeYx'><tfoot id='jpeYx'></tfoot><dl id='jpeYx'><fieldset id='jpeYx'></fieldset></dl></div>
          <legend id='jpeYx'><style id='jpeYx'><dir id='jpeYx'><q id='jpeYx'></q></dir></style></legend>

                <small id='jpeYx'></small><noframes id='jpeYx'>

                <tfoot id='jpeYx'></tfoot>
                • 本文介绍了使用 Apache Lucene 索引 MySQL 数据库,并使它们保持同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  1. 在 MySQL 中添加新项目时,它也必须被 Lucene 索引.
                  2. 从 MySQL 中删除现有项目时,它也必须从 Lucene 的索引中删除.

                  我们的想法是编写一个脚本,该脚本将通过调度程序每 x 分钟调用一次(例如 CRON 任务).这是一种保持 MySQL 和 Lucene 同步的方法.到目前为止我所管理的:

                  The idea is to write a script that will be called every x minutes via a scheduler (e.g. a CRON task). This is a way to keep MySQL and Lucene synchronized. What I managed until yet:

                  1. 对于 MySQL 中的每个新添加项,Lucene 也会对其进行索引.
                  2. 对于 MySQL 中已添加的每个项目,Lucene 不会对其重新编制索引(没有重复的项目).

                  这就是我请求你帮助管理的一点:

                  This is the point I'm asking you some help to manage:

                  1. 对于每个先前添加的项目,然后从 MySQL 中删除,Lucene 也应该取消它的索引.

                  这是我使用的代码,它试图索引一个 MySQL 表 tag (id [PK] | name):

                  Here is the code I used, which tries to index a MySQL table tag (id [PK] | name):

                  public static void main(String[] args) throws Exception {
                  
                      Class.forName("com.mysql.jdbc.Driver").newInstance();
                      Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
                      StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
                      IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
                      IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), config);
                  
                      String query = "SELECT id, name FROM tag";
                      Statement statement = connection.createStatement();
                      ResultSet result = statement.executeQuery(query);
                  
                      while (result.next()) {
                          Document document = new Document();
                          document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));
                          document.add(new Field("name", result.getString("name"), Field.Store.NO, Field.Index.ANALYZED));
                          writer.updateDocument(new Term("id", result.getString("id")), document);
                      }
                  
                      writer.close();
                  
                  }
                  

                  PS:此代码仅用于测试目的,无需告诉我它有多糟糕:)

                  PS: this code is for tests purpose only, no need to tell me how awful it is :)

                  一种解决方案是删除任何预先添加的文档,并重新索引所有数据库:

                  One solution could be to delete any previsouly added document, and reindex all the database:

                  writer.deleteAll();
                  while (result.next()) {
                      Document document = new Document();
                      document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));
                      document.add(new Field("name", result.getString("name"), Field.Store.NO, Field.Index.ANALYZED));
                      writer.addDocument(document);
                  }
                  

                  我不确定这是最优化的解决方案,是吗?

                  I'm not sure it's the most optimized solution, is it?

                  推荐答案

                  只要让索引/重新索引与应用程序分开运行,就会出现同步问题.根据您的工作领域,这可能不是问题,但对于许多并发用户应用程序来说却是.

                  As long as you let the indexing/reindexing run separately from your application, you will have synchronization problems. Depending on your field of work, this might not be a problem, but for many concurrent-user-applications it is.

                  当我们的作业系统每隔几分钟运行一次异步索引时,我们也遇到了同样的问题.用户会使用搜索引擎找到产品,然后即使管理员从有效产品堆栈中删除了产品,仍然会在前端找到它,直到下一个重新索引作业运行.这会导致向一级支持报告非常混乱且很少可重现的错误.

                  We had the same problems when we had a job system running asynchronous indexing every few minutes. Users would find a product using the search engine, then even when an administrative person removed the product from the valid product stack, still found it in the frontend, until the next reindexing job ran. This leads to very confusing and seldomly reproducable errors reported to first level support.

                  我们看到了两种可能性:要么将业务逻辑紧密连接到搜索索引的更新,要么实现更紧密的异步更新任务.我们做了后者.

                  We saw two possibilities: Either connect the business logic tightly to updates of the search index, or implement a tighter asynchronous update task. We did the latter.

                  在后台,有一个类在 tomcat 应用程序内的专用线程中运行,该线程接受更新并并行运行它们.后台更新到前端的等待时间降至0.5-2秒,大大减少了一级支持的问题.而且,它尽可能地松散耦合,我们甚至可以实现不同的索引引擎.

                  In the background, there's a class running in a dedicated thread inside the tomcat application that takes updates and runs them in parallel. The waiting times for backoffice updates to frontend are down to 0.5-2 seconds, which greatly reduces the problems for first level support. And, it is as loosely coupled as can be, we could even implement a different indexing engine.

                  这篇关于使用 Apache Lucene 索引 MySQL 数据库,并使它们保持同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 lucene 改进多线程索引 下一篇:Elasticsearch 服务器发现配置

                  相关文章

                  最新文章

                  • <bdo id='3yBPD'></bdo><ul id='3yBPD'></ul>
                  <legend id='3yBPD'><style id='3yBPD'><dir id='3yBPD'><q id='3yBPD'></q></dir></style></legend>
                  <tfoot id='3yBPD'></tfoot>

                  <small id='3yBPD'></small><noframes id='3yBPD'>

                      <i id='3yBPD'><tr id='3yBPD'><dt id='3yBPD'><q id='3yBPD'><span id='3yBPD'><b id='3yBPD'><form id='3yBPD'><ins id='3yBPD'></ins><ul id='3yBPD'></ul><sub id='3yBPD'></sub></form><legend id='3yBPD'></legend><bdo id='3yBPD'><pre id='3yBPD'><center id='3yBPD'></center></pre></bdo></b><th id='3yBPD'></th></span></q></dt></tr></i><div id='3yBPD'><tfoot id='3yBPD'></tfoot><dl id='3yBPD'><fieldset id='3yBPD'></fieldset></dl></div>