Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create index, migrate data with console command? #36

Closed
amelkikh opened this issue Oct 26, 2016 · 8 comments
Closed

How to create index, migrate data with console command? #36

amelkikh opened this issue Oct 26, 2016 · 8 comments

Comments

@amelkikh
Copy link

No description provided.

@Melcus
Copy link

Melcus commented Oct 26, 2016

The way I did it is :
php artisan make:command IndexModel
Make sure you register your command in kernel.

in it, changed protected $signature = 'command:ImportModelToES {min}';

The handle method looks like this :

public function handle()
    {
        $min = $this->argument('min');
        $max = $min+5000; //batch size
        $this->processModel($min, $max);
        $min = $min+5000; // these must match
        $this->info(memory_get_usage());
        unset($max);
        $this->call('command:ImportModelToES', ['min' => $min]);
    }

    protected function processModel($min, $max)
    {

        $models = Model::where('id', '>', $min)->where('id', '<=', $max)->get();

        Plastic::persist()->bulkSave($models);
        $this->info("Indexed to ES  from {min} to {max}.");

    }

Then you just call php artisan command:ImportModelToES 1 . I did it this way because of my requirements but there are more elegant ways to it. Look at chunk in laravel documentation

@amelkikh
Copy link
Author

And what about creating of indeсes?

@amelkikh
Copy link
Author

Is it required to create index manually or how to create index "on fly" if it does not exist?

@Melcus
Copy link

Melcus commented Oct 26, 2016

I would suggest you create the indices manually, as you can specify the shards, replicas or other settings.
You can use https://github.com/royrusso/elasticsearch-HQ to create indices or https://github.com/lmenezes/elasticsearch-kopf

My information might not be accurate, it's jut how I do it. You can wait for a response from sleimanx2 for certainty.

@amelkikh
Copy link
Author

Already use elasticsearch-HQ but how you see this in production?
In production usually needs to create some kinds of deployment commands, no possibility to use GUI and I'm really hate to use different stupid scripts to do this important things

@Melcus
Copy link

Melcus commented Oct 26, 2016

You just create the index on your production server ( with HQ or command), you do it once anyway. You specify the index in your plastic.php file, and that's it. No more configurations.

@sleimanx2
Copy link
Owner

You can create and delete indexes as follows

Plastic::getClient()->indices()->delete(['index' => Plastic::getDefaultIndex()]);

Plastic::getClient()->indices()->create(['index' => Plastic::getDefaultIndex()]);

added this section to documentation ...

@amelkikh
Copy link
Author

Thanks a lot. I think that need to add this code examples into readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants