Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

php - ElasticSearch usage with 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.

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);

}

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

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).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ElasticSearch does not fit very well for updating/deleting documents in large scale.

There are many aproaches that try to minimize the overloading about this downside on it's architecture, but if think that increases the complexity of your solution.

I suggest that you keep CRUD operations only on MySQL and use ES as append-only. Actually, StackOverflow itself, and many others great TI companies uses this approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...