diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java index 0d431a8a3ee..cecb1566988 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java @@ -40,6 +40,7 @@ import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.server.util.ISearchParamRegistry; import org.hibernate.search.mapper.orm.Search; 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.IBaseResource; 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.support.TransactionTemplate; +import javax.annotation.Nonnull; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; @@ -100,9 +102,15 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc { return requiresHibernateSearchAccess; } + @Override + public void reindex(ResourceTable theEntity) { + SearchIndexingPlan plan = getSearchSession().indexingPlan(); + plan.addOrUpdate(theEntity); + } + private List doSearch(String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) { // keep this in sync with supportsSomeOf(); - SearchSession session = Search.session(myEntityManager); + SearchSession session = getSearchSession(); List longPids = session.search(ResourceTable.class) // 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); } + @Nonnull + private SearchSession getSearchSession() { + return Search.session(myEntityManager); + } + private List convertLongsToResourcePersistentIds(List theLongPids) { return theLongPids.stream() .map(ResourcePersistentId::new) @@ -193,7 +206,7 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc { if (retVal == null) { retVal = new TransactionTemplate(myTxManager).execute(t -> { try { - SearchSession searchSession = Search.session(myEntityManager); + SearchSession searchSession = getSearchSession(); searchSession.search(ResourceTable.class); return Boolean.FALSE; } catch (Exception e) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java index 6c330b261fb..8e19fba7c75 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java @@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.dao; 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.searchparam.extractor.ResourceIndexedSearchParams; import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; @@ -49,4 +50,14 @@ public interface IFulltextSearchSvc { ExtendedLuceneIndexData extractLuceneIndexData(IBaseResource theResource, ResourceIndexedSearchParams theNewParams); 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); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/reindex/ResourceReindexer.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/reindex/ResourceReindexer.java index a18360f6cdb..21612be0209 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/reindex/ResourceReindexer.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/reindex/ResourceReindexer.java @@ -24,6 +24,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; 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.IResourceHistoryTableDao; import ca.uhn.fhir.jpa.dao.data.IResourceTableDao; @@ -50,6 +51,8 @@ public class ResourceReindexer { private IResourceTableDao myResourceTableDao; @Autowired private DaoRegistry myDaoRegistry; + @Autowired(required = false) + private IFulltextSearchSvc myFulltextSearchSvc; private final FhirContext myFhirContext; @@ -98,5 +101,10 @@ public class ResourceReindexer { Class resourceClass = (Class) resourceDefinition.getImplementingClass(); final IFhirResourceDao dao = myDaoRegistry.getResourceDao(resourceClass); dao.reindex(theResource, theResourceTable); + if (myFulltextSearchSvc != null) { + // update the full-text index, if active. + myFulltextSearchSvc.reindex(theResourceTable); + } + } }