How do I set up Search API index as cron task? From what I've read in Search API's settings, it sounds like pages should be indexing when cron is ran. This doesn't seem to be the case, but perhaps my logic is flawed.

I help manage a blog/news site. All of the content is indexed through search API, and I'm rendering lists of that indexed content using Views. One View display renders a list of the site's most popular content. This is calculated using the Statistics module. You can see here that I'm sorting this view using the Number of views field:


Initially, I didn't think that the sort criteria was working at all. I added the Number of views to the rendered output for testing, and found that the pages weren't being reordered when the page views changes.



However, I eventually discovered that the nodes will reorder, only after Search API is reindexed manually. Currently, Search API is set to index nodes immediately upon publishing, but I'm assuming that a full reindex does not occur when this happening. I tested this by creating a new node, and found that the order of nodes did not update. I've also run Cron manually, and have found that this does not work either.

Now, I'm of the impression that Search API is not reindexing when running Cron, but I'm unsure if this is a valid assumption. If it's not, I'd like to set up a nightly reindexing. How do I go about writing this cron task?
You are right insofar as a node will only get reindexed when it is updated. Someone viewing the node isn't seen as an update, so incrementing the number of views won't cause the node to be reindexed.

It's not normal to rebuild the entire index on a schedule. Cron only re-indexes those nodes that have been marked as needing to be indexed.

There are two ways I can think of you might schedule a nightly rebuild of the index. 


If you have access to the server at command line level you could run the drush command "search-api-reindex" via a custom cron job. Run it as the same user as the web-server (eg. apache or www-user) and use the -l option to specify the url so the cron line would be something like: 0 23 * * * cd /var/www/home &amp;&amp; drush sapi-r default
Otherwise you could write your own cron hook in a custom module to do the same. The module elysia_cron would help with scheduling it for the right time. You'd need to call search_api_index_reindex('default'); to have the same effect as the cron job in point 1.