Re-index lucene when reindexing hibernate, even if resource unchanged. (#3311)

This commit is contained in:
michaelabuckley 2022-01-19 18:02:39 -05:00 committed by GitHub
parent 3d4f8be645
commit 283ff19375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -40,6 +40,7 @@ import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.server.util.ISearchParamRegistry; import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
import org.hibernate.search.mapper.orm.Search; import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession; import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.mapper.orm.work.SearchIndexingPlan;
import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -47,6 +48,7 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Nonnull;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType; import javax.persistence.PersistenceContextType;
@ -100,9 +102,15 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc {
return requiresHibernateSearchAccess; return requiresHibernateSearchAccess;
} }
@Override
public void reindex(ResourceTable theEntity) {
SearchIndexingPlan plan = getSearchSession().indexingPlan();
plan.addOrUpdate(theEntity);
}
private List<ResourcePersistentId> doSearch(String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) { private List<ResourcePersistentId> doSearch(String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) {
// keep this in sync with supportsSomeOf(); // keep this in sync with supportsSomeOf();
SearchSession session = Search.session(myEntityManager); SearchSession session = getSearchSession();
List<Long> longPids = session.search(ResourceTable.class) List<Long> longPids = session.search(ResourceTable.class)
// Selects are replacements for projection and convert more cleanly than the old implementation. // Selects are replacements for projection and convert more cleanly than the old implementation.
@ -155,6 +163,11 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc {
return convertLongsToResourcePersistentIds(longPids); return convertLongsToResourcePersistentIds(longPids);
} }
@Nonnull
private SearchSession getSearchSession() {
return Search.session(myEntityManager);
}
private List<ResourcePersistentId> convertLongsToResourcePersistentIds(List<Long> theLongPids) { private List<ResourcePersistentId> convertLongsToResourcePersistentIds(List<Long> theLongPids) {
return theLongPids.stream() return theLongPids.stream()
.map(ResourcePersistentId::new) .map(ResourcePersistentId::new)
@ -193,7 +206,7 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc {
if (retVal == null) { if (retVal == null) {
retVal = new TransactionTemplate(myTxManager).execute(t -> { retVal = new TransactionTemplate(myTxManager).execute(t -> {
try { try {
SearchSession searchSession = Search.session(myEntityManager); SearchSession searchSession = getSearchSession();
searchSession.search(ResourceTable.class); searchSession.search(ResourceTable.class);
return Boolean.FALSE; return Boolean.FALSE;
} catch (Exception e) { } catch (Exception e) {

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.dao;
import java.util.List; import java.util.List;
import ca.uhn.fhir.jpa.model.entity.ResourceTable;
import ca.uhn.fhir.jpa.model.search.ExtendedLuceneIndexData; import ca.uhn.fhir.jpa.model.search.ExtendedLuceneIndexData;
import ca.uhn.fhir.jpa.searchparam.extractor.ResourceIndexedSearchParams; import ca.uhn.fhir.jpa.searchparam.extractor.ResourceIndexedSearchParams;
import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
@ -49,4 +50,14 @@ public interface IFulltextSearchSvc {
ExtendedLuceneIndexData extractLuceneIndexData(IBaseResource theResource, ResourceIndexedSearchParams theNewParams); ExtendedLuceneIndexData extractLuceneIndexData(IBaseResource theResource, ResourceIndexedSearchParams theNewParams);
boolean supportsSomeOf(SearchParameterMap myParams); boolean supportsSomeOf(SearchParameterMap myParams);
/**
* Re-publish the resource to the full-text index.
*
* During update, hibernate search only republishes the entity if it has changed.
* During $reindex, we want to force the re-index.
*
* @param theEntity the fully populated ResourceTable entity
*/
void reindex(ResourceTable theEntity);
} }

View File

@ -24,6 +24,7 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc;
import ca.uhn.fhir.jpa.dao.data.IForcedIdDao; import ca.uhn.fhir.jpa.dao.data.IForcedIdDao;
import ca.uhn.fhir.jpa.dao.data.IResourceHistoryTableDao; import ca.uhn.fhir.jpa.dao.data.IResourceHistoryTableDao;
import ca.uhn.fhir.jpa.dao.data.IResourceTableDao; import ca.uhn.fhir.jpa.dao.data.IResourceTableDao;
@ -50,6 +51,8 @@ public class ResourceReindexer {
private IResourceTableDao myResourceTableDao; private IResourceTableDao myResourceTableDao;
@Autowired @Autowired
private DaoRegistry myDaoRegistry; private DaoRegistry myDaoRegistry;
@Autowired(required = false)
private IFulltextSearchSvc myFulltextSearchSvc;
private final FhirContext myFhirContext; private final FhirContext myFhirContext;
@ -98,5 +101,10 @@ public class ResourceReindexer {
Class<T> resourceClass = (Class<T>) resourceDefinition.getImplementingClass(); Class<T> resourceClass = (Class<T>) resourceDefinition.getImplementingClass();
final IFhirResourceDao<T> dao = myDaoRegistry.getResourceDao(resourceClass); final IFhirResourceDao<T> dao = myDaoRegistry.getResourceDao(resourceClass);
dao.reindex(theResource, theResourceTable); dao.reindex(theResource, theResourceTable);
if (myFulltextSearchSvc != null) {
// update the full-text index, if active.
myFulltextSearchSvc.reindex(theResourceTable);
}
} }
} }