Originally I thought I had to implement my own hooks but after seeing that apachesolr took care of deletes I wondered why it hadn't taken care of my updates and inserts.
There is actually a apachesolr_entity_update/insert() which calls apachesolr_entity_should_index(). That checks whether your entity info (returned by entity_get_info($entity_type)) has the following key set:
$info['bundles'][$bundle]['apachesolr']['index']
Where $bundle is the $entity bundle. This made it pretty easy for me to figure out that I just had to add some stuff to my hook_entity_info_alter():
function my_entity_entity_info_alter(&$entity_info) {
foreach (my_entity_get_types() as $type => $info) {
$entity_info['my_entity']['bundles'][$type] = array(
'apachesolr' => array('index' => TRUE),
);
}
}
After adding that and clearing cache I no longer needed my entity update/insert hooks.
Note: Don't get this confused with hook_apachesolr_entity_info_alter like I did!
Original Answer
I found the function apachesolr_index_get_entities_to_index() which checks the indexer table for your entity_type for a changed timestamp that's greater than what it's last seen (I have yet to find out where it sets that). Essentially we just need to automate the updating of that table. This can be done with 2 hooks:
hook_entity_insert()
hook_entity_update()
/**
* Implements hook_entity_insert().
*/
function my_entity_entity_insert($entity, $type) {
// Update our indexer table
if ($type != 'my_entity') {
return;
}
$indexer_table = apachesolr_get_indexer_table($type);
// Insert the new entity into a fictional table of all entities.
$info = entity_get_info($type);
list($id) = entity_extract_ids($type, $entity);
db_insert($indexer_table)->fields(array(
'entity_type' => $type,
'bundle' => $entity->type,
'entity_id' => $id,
'changed' => REQUEST_TIME,
))->execute();
}
/**
* Implements hook_entity_update().
*/
function my_entity_entity_update($entity, $type) {
// Update our indexer table
if ($type != 'my_entity') {
return;
}
$indexer_table = apachesolr_get_indexer_table($type);
db_update($indexer_table)
->fields(array(
'changed' => REQUEST_TIME,
))
->condition('entity_id', $entity->entity_id)
->execute();
}
You may have to add this line into the insert statement in hook_entity_insert() if you have a status coming from $entity (mine is always 1 so the db table defaults it to that).
'status' => $entity->status,
Edit: You may be wondering "What about delete??", this seems to be taken care of by apachesolr_entity_delete, I'm going to see why apachesolr_entity_update and apachesolr_entity_insert didn't take care of my entities. However this answer is still valid.