When new profile is created, it doesn't store the profile id causing the db_update under the apachesolr_mark_entity to not work because entity_id/pid is not in the indexer table.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

asgorobets’s picture

This is really a problem with the code:

The indexing works if you do a full reindex, and it's not working as expected on hook_entity_insert and hook_entity_update.
Apachesolr module provides a good way of thinking and a good API for other modules to implement.

I've found that in the code:

/**
 * Mark profile2 entities for reindexing when updated
 */
function profile2_apachesolr_profile2_update($profile){
  if(profile2_apachesolr_indexable($profile)){
    apachesolr_mark_entity('profile2', $profile->pid);
  }
}

/**
 * Mark profile2 entites for deletion in index
 */
function profile2_apachesolr_profile2_delete($profile){
  if($env_id = profile2_apachesolr_indexable($profile)){
    module_load_include('inc', 'apachesolr', 'apachesolr.index');
    apachesolr_index_delete_entity_from_index($env_id, 'profile2', $profile->pid);
  }
}

/**
 * Makr profile2 entities for index when they are created
 */
function profile2_apachesolr_profile2_insert($profile) {
  if($env_id = profile2_apachesolr_indexable($profile)){
    apachesolr_mark_entity('profile2', $profile->pid);
  }
}

The author is trying to implement his own reindexing mechanism instead of using apachesolr default. It's not working since apachesolr_mark_entity will do a db_update for a $profile->pid that's not yet in DB.
In this case profile2_apachesolr_profile2_insert will do nothing useful:

function apachesolr_mark_entity($entity_type, $entity_id) {
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  $table = apachesolr_get_indexer_table($entity_type);
  if (!empty($table)) {
    db_update($table)
      ->condition('entity_id', $entity_id)
      ->fields(array('changed' => REQUEST_TIME))
      ->execute();
  }
}

You got it right, the apachesolr_mark_entity will mark an entity for reindexing. But an existing one. It's useful to use it when you're posting a comment on a node and want to reindex node instead. Like here:

/**
 * Implements hook_comment_update().
 */
function apachesolr_comment_update($comment) {
  apachesolr_mark_entity('node', $comment->nid);
}

It's not really needed to use apachesolr_mark_entity to reindex the updated entity itself because apachesolr module already have a mechanism of doing this:
Take a look at this:

function apachesolr_entity_update($entity, $type) {
  // Include the index file for the status callback
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  if (apachesolr_entity_should_index($entity, $type)) {
    $info = entity_get_info($type);
    list($id, $vid, $bundle) = entity_extract_ids($type, $entity);

    // Check status callback before sending to the index
    $status_callbacks = apachesolr_entity_get_callback($type, 'status callback', $bundle);

    $status = TRUE;
    if (is_array($status_callbacks)) {
      foreach($status_callbacks as $status_callback) {
        if (is_callable($status_callback)) {
          // by placing $status in front we prevent calling any other callback
          // after one status callback returned false
          $status = $status && $status_callback($id, $type);
        }
      }
    }

    // Delete the entity from our index if the status callback returns FALSE
    if (!$status) {
      apachesolr_entity_delete($entity, $type);
      return;
    }

    $indexer_table = apachesolr_get_indexer_table($type);

    // If we haven't seen this entity before it may not be there, so merge
    // instead of update.
    db_merge($indexer_table)
      ->key(array(
      'entity_type' => $type,
      'entity_id' => $id,
      ))
      ->fields(array(
        'bundle' => $bundle,
        'status' => 1,
        'changed' => REQUEST_TIME,
      ))
      ->execute();
  }
}

In this case db_merge will not only update a table with new values, but can also insert values. There is also a apachesolr_entity_insert hook that's calling the
apachesolr_entity_update function. This will index all the entities that were inserted or updated.

For this thing to work properly you have to set some values in $entity_info to notify apachesolr_entity_should_index function that the entity should be indexed:

function apachesolr_entity_should_index($entity, $type) {
  $info = entity_get_info($type);
  list($id, $vid, $bundle) = entity_extract_ids($type, $entity);

  if ($bundle && isset($info['bundles'][$bundle]['apachesolr']['index']) && $info['bundles'][$bundle]['apachesolr']['index']) {
    return TRUE;
  }
  return FALSE;
}

The author did set some changes in hook_entity_info_alter():

if(isset($entity_info['profile2']['bundles'])){
  foreach ($entity_info['profile2']['bundles'] as $bundle_name => $bundle_array) {
    $entity_info['profile2']['apachesolr']['bundles'][$bundle_name]['index'] = TRUE;
  }
}

As you can see the $entity_info['profile2']['apachesolr']['bundles'][$bundle_name]['index'] is wrong according to $info['bundles'][$bundle]['apachesolr']['index'] from apachesolr_entity_should_index function. Probably the apachesolr module was changed and this thing worked before, but it looks like the issue was reported from version 1.2 and is pretty old.

I did a patch to fix this issue as well as clean-up the project from unnecessary functions mentioned above.

I would also like to contribute to the module since it's been a while since the module was updated. You can make a maintainer if you feel that you don't have too much time for the project.

Thank You,
Alexei.

asgorobets’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, indexing-on-insert-not-working-1784296-1.patch, failed testing.

pwaterz’s picture

Let me get this tested and will update it

asgorobets’s picture

Status: Needs work » Needs review
FileSize
3.24 KB

Hi, I've improved a little bit the patch to avoid some notices and to use proper apachesolr API hook for updating entity_info.

In case the patch will not apply it's because of different line endings, you can use:
git am --ignore-space-change indexing-on-insert-not-working-1784296-5.patch

Status: Needs review » Needs work

The last submitted patch, indexing-on-insert-not-working-1784296-5.patch, failed testing.

Ingmar’s picture

Please try to get this working. I could use this definitely!
Thanks for your effort!

pwaterz’s picture

@ingmar, why dont you try and patch and test it your self :)

pwaterz’s picture

@asgobets

why did you do this?

-function profile2_apachesolr_entity_info_alter(&$entity_info) {
- $entity_info['profile2']['apachesolr']['indexable'] = TRUE;
- $entity_info['profile2']['apachesolr']['status callback'][] = 'profile2_apachesolr_profile2_status_callback';
- $entity_info['profile2']['apachesolr']['document callback'][] = 'profile2_apachesolr_solr_document_profile2';
- $entity_info['profile2']['apachesolr']['reindex callback'] = 'profile2_apachesolr_solr_reindex_profile2';
- $entity_info['profile2']['apachesolr']['index_table'] = 'apachesolr_index_entities_profile2';
+function profile2_apachesolr_apachesolr_entity_info_alter(&$entity_info) {
+ $entity_info['profile2']['indexable'] = TRUE;
+ $entity_info['profile2']['status callback'][] = 'profile2_apachesolr_profile2_status_callback';
+ $entity_info['profile2']['document callback'][] = 'profile2_apachesolr_solr_document_profile2';
+ $entity_info['profile2']['reindex callback'] = 'profile2_apachesolr_solr_reindex_profile2';
+ $entity_info['profile2']['index_table'] = 'apachesolr_index_entities_profile2';
+}

I dont think that is right.

pwaterz’s picture

Ahh i see now, you are correct.

pwaterz’s picture

I tried to apply the patch and it failed.

error: patch failed: profile2_apachesolr.module:1
error: profile2_apachesolr.module: patch does not apply

asgorobets’s picture

Status: Needs work » Needs review

I see, there's an issue with line endings, your files in repository have CRLF line endings and the patch is LF line endings.
Using git am --ignore-space-change indexing-on-insert-not-working-1784296-5.patch works for me. Did you tried that?
There are some issues describing the problem that you will want to take a look at:
Provide Git core.*crlf settings recommendations based on OS sniffing
CRLF line endings cause problems with "git apply"
Maybe the files should be converted to LF for convenience.

pwaterz’s picture

Interesting, I use eclipse for development. I'll see if I can fix that.

pwaterz’s picture

I added a dev branch release, and committed this patch there.

asgorobets’s picture

Thanks pwaterz!

pwaterz’s picture

Status: Needs review » Closed (fixed)

In release 7.x-1.6