自动将数据从 mysql 导入 solr [英] Auto importing of data from mysql to solr

查看:52
本文介绍了自动将数据从 mysql 导入 solr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将值从mysql导入到solr..我通过调用php脚本自动导入

I want to import values from mysql to solr.. I did automatic import by calling a php script

使用mysql触发器.但我读到这不是一个好方法..

using mysql trigger. But i read that its not a good method.. Is there any other solution for

自动导入数据?

有人可以帮我吗...

推荐答案

尽管有一个内置的机制来解决这个问题,Data Import Handler (DIH),正如其他回复中提到的,我发现这个工具不是很灵活.我的意思是,如果我想在索引之前做任何数据按摩,我只能依赖 MySQL 函数,而我可以使用 PHP 函数.

Even though there is a built in mechanism for this very thing, Data Import Handler (DIH), as mentioned in the other responses, I found this tool not very flexible. What I mean by this is, if I wanted to do any data massaging before indexing I could only depend on MySQL functions, when I could have used PHP functions.

我最终将自己的数据导入处理程序编写为 PHP 脚本,在其中执行初始查询,然后在插入 SOLR 索引时逐步处理结果和处理(和缓存)数据.它并不太复杂,看起来像(仅用于演示):

I ended up writing my own Data Import Handler as a PHP script, where it does the initial query, then steps through the results and massages (and caches) data upon insert into the SOLR index. It wasn't too complicated, and would look something like (demonstrative only):

SELECT 
  book.id AS book_id,
  book.name AS book_name,
  GROUP_CONCAT(DISTINCT author.name) AS authors
FROM
  book
INNER JOIN
  link_book_author AS alink ON alink.book_id = book.id
INNER JOIN
  author ON author.id = alink.author_id
GROUP BY
  book.id;

$stmt = $dbo->prepare($sql);

$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {

    try {

        $document = new Apache_Solr_Document();

        $document->Id = $row->book_id;
        $document->BookName = $row->book_name;

        $document->Author = explode(',' $row->author);

        $this->getSearchEngineInstance()->addDocument($document);

    } catch (Exception $e) {

        error_log(sprintf('Unable to add document to index: (%s)', $e->getMessage());
    }
}

这只是您可以执行的操作的一个示例,在我的情况下,我还涉及缓存以在执行完全导入时提高性能.使用原生 DIH 无法做到的事情.

This is just an example of what you can do, In my situation I also involve caching to increase performance when I do a full import. Something you cannot do using the native DIH.

我用来通过 PHP 访问 SOLR 的 API 是 solr-php-client,可能有是其他人,所以谷歌搜索.

The API I use to access SOLR through PHP is solr-php-client, there may be others out there, so google around.

这篇关于自动将数据从 mysql 导入 solr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆