ElasticPress icon indicating copy to clipboard operation
ElasticPress copied to clipboard

Trigger posts index programmatically

Open elvismdev opened this issue 8 years ago • 5 comments

Hi,

I can see that exists the command wp elasticpress index to trigger the index of all posts from WP-CLI.

I wonder if there's a function from ElasticPress where we can fire the index sync from code? I'm importing tons of posts records from a CSV, and I would like to fire the index sync process from ElasticSearch right after the import finishes.

For instance, FacetWP plugin has: FWP()->indexer->index();

Is there something similar on ElasticPress?

elvismdev avatar Oct 12 '18 21:10 elvismdev

Hi @elvismdev ,

You can find useful functions for that matter here:

https://github.com/10up/ElasticPress/blob/develop/classes/class-ep-api.php#L2686 https://github.com/10up/ElasticPress/blob/develop/classes/class-ep-sync-manager.php#L277

Some functions worth taking a look are ep_sync_post() and ep_index_post() for what you describe.

Hope this helps and have a great day

oscarssanchez avatar Oct 13 '18 17:10 oscarssanchez

Hi @oscarssanchez , those functions looks like they are to sync to the index a single post given it's ID or the whole WP_Post object. I'm looking to trigger a whole index sync once my import completes pulling into WordPress multiple new posts.

Could you confirm to me if ep_refresh_index() is the one I'm looking to trigger that action?

Thank you!

elvismdev avatar Oct 13 '18 18:10 elvismdev

Hey @elvismdev ,

Sorry, I was thinking you were trying to index just a single post.

As far as my understanding, ep_refresh_index() will not help you and there's no method on the ep-api to trigger the index of every post in your site. You would have to write that manually. Maybe taking a look at action_wp_ajax_ep_index() might give you some insight how indexing is done: https://github.com/10up/ElasticPress/blob/develop/classes/class-ep-dashboard.php#L452

It is recommended though that if you are importing a lot of data you run a re-index from wp-cli.

oscarssanchez avatar Oct 19 '18 02:10 oscarssanchez

@oscarssanchez I actually end up using ep_sync_post() to index each post as they get imported to the WordPress database.

Thanks for your input!

elvismdev avatar Oct 19 '18 20:10 elvismdev

What's the 3.0 way of doing this?

marekdsi avatar Jan 23 '24 09:01 marekdsi