ElasticSearch 在 MySQL 中的使用 [英] ElasticSearch usage with MySQL

查看:30
本文介绍了ElasticSearch 在 MySQL 中的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ElasticSearch 作为站点的搜索组件.正在编制索引并最终搜索的数据与保存在 MySQL 数据库中的数据相同.

I'm using ElasticSearch for the search component of a site. The data that is being indexed and eventually searched is the same data that is being saved in a MySQL DB.

我的做法是在相应的 CRUD MySQL 操作发生时在索引中添加/删除/修改数据.

My approach to this is to add/delete/modify data in the index when the corresponding CRUD MySQL operation happens.

例如,一个创建操作看起来像这样:

For instance, a create operation looks something like this:

public function savePost(Request $request) {
    //Firstly, create the object and save it to MySQL
    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    //...
    //and so on
    $post->save();

    //Secondly, index this new data:
    $elasticSearchClient = ClientBuilder::create()->build();

    $params = [
        'index' => 'some_index_elasticsearch',
        'id' =>  $post->id,
        'type' => 'post',
        'timestamp' => time(),
        'body' => [
            'id' => $post->id,
            'title' => $post->title,
            'body' => $post->body,
            //... and so on
        ],
    ];

    $elasticSearchClient->index($params);

}

如果数据在 MySQL 中被删除/更新,我只会删除它或从索引中更新它.

If the data is deleted/updated in MySQL I'd just delete it or update it from the index.

这是将 MySQL 与 ElasticSearch(或任何其他类似技术,如 Sphinx)结合使用的正确方法吗?或者您会推荐一种更好的方法来使用 MySQL 作为 ElasticSearch 的更多数据源吗?(这里根本没有发生这种情况,因为 ElasticSearch 和 MySQL 之间根本没有交互).

Is this the right approach to using MySQL with ElasticSearch (or any other comparable technology like Sphinx)? or would you recommend a better approach to using MySQL as a more of a data source for ElasticSearch? (which really isn't happening at all here because there is no interaction between ElasticSearch and MySQL at all).

我正在使用 https://github.com/elastic/elasticsearch-php如果有任何不同,则与 ElasticSearch 进行交互.

I'm using https://github.com/elastic/elasticsearch-php to interact with ElasticSearch if it makes any difference.

澄清一下:到目前为止,这种方法确实有效 - 我只是不确定它是否正确,或者是否有人可以看到我在使用这种方法时可能遇到的问题东西.

Just to clarify: this approach does work so far - I'm just not sure if it is the right way, or if anyone can see problems that I may run into with this way of doing things.

推荐答案

没有使用 Elasticsearch 的正确方法".正确"是相对的,因此正确方式"是一种支持您的用例的方式.Elasticsearch 不仅适用于一种特定用例,而且适用于越来越多的用例.

There is no "right way" to use Elasticsearch. "Right" is relative, so the "right way" is a way that supports your use case(s). Elasticsearch doesn't only work for one specific use case, but for increasingly many more than one use case.

您描述的案例是完全有效的案例,即在 ES 中索引您在另一个 RDBMS(如 MySQL)中的任何内容,并确保索引的内容与真实的主要来源同步.

The case you describe is a perfectly valid one, i.e. indexing in ES whatever content you have in another RDBMS such as MySQL and making sure the indexed content is in synch with the primary source of truth.

在您的用例中需要记住的一件困难的事情是,您必须保证 MySQL 和 ES 始终 1:1 同步,并且由于各种原因,这不一定容易做到:

One difficult thing in your use case that you need to keep in mind is that you have to guarantee that MySQL and ES are always 1:1 in synch, and that's not necessarily easy to do for various reasons:

  • 如果您需要关闭 ES 进行维护,但您的应用出于任何原因必须保持运行,会发生什么情况?
  • 如果 ES 出现问题并且文档没有被索引/更新/删除,会发生什么?(请记住,没有交易支持)

还有其他方法可以同步 MySQL 和 ES,但它们的脆弱性较低,例如使用二进制日志.

There are otherways to synch MySQL and ES that are less brittle, e.g. by using the binlog.

您需要问自己这些问题并找出减轻这些潜在问题的策略,因为我可以向您保证它们(和其他人)肯定会出现.

You need to ask yourself those questions and figure out a strategy to mitigate those potential problems, because I can assure you they (and others) will definitely arise.

总而言之,你的架构没有问题,成千上万的公司都在做同样的事情,但是,如果你的同步计划向南,你需要有一个计划.

To sum up, there's no problem with your architecture, thousands of companies do the exact same thing, however, you need to have a plan if your synch plan goes south.

这篇关于ElasticSearch 在 MySQL 中的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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