Skip to content

Instantly share code, notes, and snippets.

@johnrobertwilson
Created September 15, 2011 03:42
Show Gist options
  • Save johnrobertwilson/1218471 to your computer and use it in GitHub Desktop.
Save johnrobertwilson/1218471 to your computer and use it in GitHub Desktop.
get entities to index
/**
* Returns an array of rows from a query based on an indexing namespace.
*/
function apachesolr_indexer_get_entities_to_index($namespace, $limit) {
$rows = array();
if (variable_get('apachesolr_read_only', 0)) {
return $rows;
}
$last_change = variable_get('apachesolr_indexer_last_run', 0);
foreach (entity_get_info() as $type => $info) {
foreach ($info['bundles'] as $bundle => $bundle_info) {
if (isset($bundle_info['apachesolr']['index']) && $bundle_info['apachesolr']['index']) {
$bundles[] = $bundle;
}
}
// If we're not checking any bundles of this entity type, just skip them all.
if (empty($bundles)) {
continue;
}
$last_entity_id = variable_get('apachesolr_indexer_last_run_id_' . $type, 0);
// Find the next batch of entities to index for this entity type. Note that
// for ordering we're grabbing the oldest first and then ordering by ID so
// that we get a definitive order.
$query = db_select('apachesolr_indexer_entities', 'aie')
->fields('aie', array('entity_type', 'entity_id', 'changed'))
->condition('aie.status', 1)
->condition('aie.entity_type', $type)
->condition('aie.bundle', $bundles)
->condition(db_or()->condition('aie.changed', $last_change, '>')
->condition('aie.changed', $last_change)
->condition(db_and()->condition('aie.entity_id', $last_entity_id, '>')))
->orderBy('aie.changed')
->orderBy('aie.entity_id')
->range(0, $limit);
$records = $query->execute()->fetchAll();
// @todo This assumes an int ID, which technically the entity system
// doesn't require. Fix this later.
$max_id = 0;
foreach ($records as $record) {
$rows[] = $record;
$max_id = max($max_id, $record->entity_id);
}
variable_set('apachesolr_indexer_last_run_id_' . $type, $max_id);
}
return $rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment