相关疑难解决方法(0)

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

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

我们的想法是编写一个脚本,通过调度程序每隔x分钟调用一次(例如CRON任务).这是保持MySQL和Lucene同步的一种方法.直到我还管理的内容:

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

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

  1. 对于之前添加的每个已经从MySQL中删除的项目,Lucene也应该取消索引.

这是我使用的代码,它试图索引MySQL表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)); …
Run Code Online (Sandbox Code Playgroud)

java mysql lucene indexing synchronization

9
推荐指数
1
解决办法
9603
查看次数

如何避免在Lucene 6.0中重复文档索引

我正在为从数据库获取的值创建一个Lucene索引.我把Index OpenMode设为OpenMode.CREATE_OR_APPEND.

索引创建步骤是Spring Batch Job的一部分.

我的理解是,当我第一次运行作业时,索引可能需要一段时间,但是当我再次为相同的未更改的源数据重新运行作业时,它应该很快,因为文档已经存在,因此没有执行UPDATE OR INSERT.

但对于我的情况,后续索引尝试相同的未更改的源数据变得越来越慢.

回答这个问题说它会根据一个术语自动处理.

我不确定如何在我的案例中定义术语来处理这个问题?

以下是我的示例代码,

        public Integer createIndex(IndexWriter writer, String str, LuceneIndexerInputVO luceneInputVO) throws Exception {
            Integer count = 0;
            Document d = null;
            txtFieldType.setTokenized(false);
            strFieldType.setTokenized(false);

            List<IndexVO> indexVO = null;

            indexVO = jdbcTemplate.
                    query(Constants.SELECT_FROM_TABLE1, 
                            new Object[] {luceneInputVO.getId1(), luceneInputVO.getId2(), str}, 
                            new IndexRowMapper());

            while (!indexVO.isEmpty()) {
                d = new Document();
                d.add(getStringField(Constants.ID, String.valueOf(luceneInputVO.getId())));
                .....
                ....
                writer.addDocument(d);
                indexVO.remove(indexVO.get(count));
                count++;
            }
            return count;
        } …
Run Code Online (Sandbox Code Playgroud)

java lucene spring-batch

2
推荐指数
1
解决办法
2173
查看次数

标签 统计

java ×2

lucene ×2

indexing ×1

mysql ×1

spring-batch ×1

synchronization ×1