Message Boards

Incorrect search results after reindexing a model object

Bruce Godden, modified 3 Years ago.

Incorrect search results after reindexing a model object

New Member Posts: 2 Join Date: 1/13/21 Recent Posts

I have a model, call it Alpha, where search results need to be filtered by users that have been granted access to another model, call it Beta. In the database this requires joining across multiple tables to find the user ids but this is straightforward to do with a custom query. For searching I have chosen to do this by adding a list of user ids to the Alpha search document. Because this list of ids changes when a Beta object is altered I have implemented an AlphaBatchReindexer which is called from BetaLocalServiceImpl when the associated users are changed.

As far as I can tell, creating the index and filtering the search results are all working correctly. At least it does when using a newly built index. The problem happens if you change the users associated with a Beta object. Although I can see the reindexing of the associated Alpha object happening this is not reflected in the search results. Liferay still behaves as if the the reindexing had not happened and gives results for the previous state.

It is as if the search results have been cached and have not been discarded by the reindexing. Or, perhaps, as if the reindex is being ignored. Explicitly reindexing the Alpha objects via the admin interface puts it back to a working state again.

Am I doing something wrong?

If it is a caching issue, is there a way to discard the cache of Alpha search results?

The AlphaBatchReindexer code follows and was based on the Help Centre article https://help.liferay.com/hc/en-us/articles/360020754191-Indexing-Entries:

@Component(immediate = true, service =AlphaBatchReindexer.class)
public class AlphaBatchReindexerImpl implements AlphaBatchReindexer {

  @Reference(target = "(indexer.class.name=com.example.Alpha)")
  private IndexerDocumentBuilder _indexerDocumentBuilder;

  @Reference(target = "(indexer.class.name=com.example.Alpha)")
  private IndexerWriter<Alpha> _indexerWriter;

  @Override
  public void reindex(final long alphaId) {
    final BatchIndexingActionable batchIndexingActionable = _indexerWriter.getBatchIndexingActionable();

    batchIndexingActionable.setAddCriteriaMethod(dynamicQuery -> {
      final Property alphaIdProperty = PropertyFactoryUtil.forName(alphaId");
      dynamicQuery.add(alphaIdProperty.eq(alphaId));
    });

    batchIndexingActionable.setPerformActionMethod((final Alpha alpha) -> {
      final Document document = _indexerDocumentBuilder.getDocument(alpha);
      batchIndexingActionable.addDocuments(document);
    });

    batchIndexingActionable.performActions();

  }

}

The relevant BetaLocalServiceImpl code follows:

  private void updateUsers(final Beta beta, final long[] userIds, final ServiceContext serviceContext) throws PortalException {
    final long betaId = beta.getBeta Id();
    final Set<Long> updatedUserIds = LongStream.of(userIds).boxed().collect(toSet());
    final Set<Long> currentUserIds = _betaLinkedUserLocalService.getBetaUsers(betaId).stream().mapToLong(BetaLinkedUser::getLinkedUserId).boxed().collect(toSet());

    for (final long userId : Sets.difference(updatedUserIds, currentUserIds)) {
      _betaLinkedUserLocalService.linkUserToBeta(userId, betaId, serviceContext);
    }
    for (final long userId : Sets.difference(currentUserIds, updatedUsrerIds)) {
      _betaLinkedUserLocalService.unlinkUserFromBeta(userId, betaId);
    }

    _alphaBatchReindexer.reindex(beta.getAlphaId());
  }

  @Reference
  private BetaLinkedUserLocalService _betaLinkedUserLocalService;

  @Reference
  private AlphaBatchReindexer _alphaBatchReindexer;

The relevant part of the AlphaModelDocumentContributer follows:

  @Override
  public void contribute(final Document document, final Alpha alpha) {

    // ... blah ...

    final long[] userIds = _betaLinkedUserLocalService.getAlphaUserIds(alpha.getAlphaId());

    document.addNumber(USER_ID, userIds);

    // ... blah ...

  }

 

Bruce Godden, modified 3 Years ago.

RE: Incorrect search results after reindexing a model object

New Member Posts: 2 Join Date: 1/13/21 Recent Posts

The answer is that I failed to copy the Help Centre article code correctly. I missed out the following line:

    batchIndexingActionable.setCompanyId(companyId);

This has the effect of putting the reindexed object into the wrong elasticsearch index - hence the search fails to find it.