001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.dao;
021
022import ca.uhn.fhir.jpa.model.entity.ResourceTable;
023import ca.uhn.fhir.jpa.model.search.ExtendedHSearchIndexData;
024import ca.uhn.fhir.jpa.search.autocomplete.ValueSetAutocompleteOptions;
025import ca.uhn.fhir.jpa.search.builder.ISearchQueryExecutor;
026import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
027import ca.uhn.fhir.jpa.searchparam.extractor.ResourceIndexedSearchParams;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031
032import java.util.Collection;
033import java.util.List;
034
035@SuppressWarnings({"rawtypes"})
036public interface IFulltextSearchSvc {
037
038        /**
039         * Search the Lucene/Elastic index for pids using params supported in theParams,
040         * consuming entries from theParams when used to query.
041         *
042         * @param theResourceName   the resource name to restrict the query.
043         * @param theParams         the full query - modified to return only params unused by the index.
044         * @param theRequestDetails The request details
045         * @return the pid list for the matchign resources.
046         */
047        <T extends IResourcePersistentId> List<T> search(
048                        String theResourceName, SearchParameterMap theParams, RequestDetails theRequestDetails);
049
050        /**
051         * Query the index for a plain list (non-scrollable) iterator of results.
052         *
053         * @param theResourceName      e.g. Patient
054         * @param theParams            The search query
055         * @param theMaxResultsToFetch maximum results to fetch
056         * @param theRequestDetails The request details
057         * @return Iterator of result PIDs
058         */
059        ISearchQueryExecutor searchNotScrolled(
060                        String theResourceName,
061                        SearchParameterMap theParams,
062                        Integer theMaxResultsToFetch,
063                        RequestDetails theRequestDetails);
064
065        /**
066         * Query the index for a complete iterator of ALL results. (scrollable search result).
067         *
068         * @param theResourceName      e.g. Patient
069         * @param theParams            The search query
070         * @param theRequestDetails The request details
071         * @return Iterator of result PIDs
072         */
073        ISearchQueryExecutor searchScrolled(
074                        String theResourceName, SearchParameterMap theParams, RequestDetails theRequestDetails);
075
076        /**
077         * Autocomplete search for NIH $expand contextDirection=existing
078         * @param theOptions operation options
079         * @return a ValueSet with the search hits as the expansion.
080         */
081        IBaseResource tokenAutocompleteValueSetSearch(ValueSetAutocompleteOptions theOptions);
082
083        <T extends IResourcePersistentId> List<T> everything(
084                        String theResourceName,
085                        SearchParameterMap theParams,
086                        T theReferencingPid,
087                        RequestDetails theRequestDetails);
088
089        boolean isDisabled();
090
091        ExtendedHSearchIndexData extractLuceneIndexData(
092                        IBaseResource theResource, ResourceTable theEntity, ResourceIndexedSearchParams theNewParams);
093
094        /**
095         * Returns true if the parameter map can be handled for hibernate search.
096         * We have to filter out any queries that might use search params
097         * we only know how to handle in JPA.
098         * -
099         * See {@link ca.uhn.fhir.jpa.dao.search.ExtendedHSearchSearchBuilder#addAndConsumeAdvancedQueryClauses}
100         */
101        boolean canUseHibernateSearch(String theResourceType, SearchParameterMap theParameterMap);
102
103        /**
104         * Re-publish the resource to the full-text index.
105         * -
106         * During update, hibernate search only republishes the entity if it has changed.
107         * During $reindex, we want to force the re-index.
108         *
109         * @param theEntity the fully populated ResourceTable entity
110         */
111        void reindex(ResourceTable theEntity);
112
113        List<IResourcePersistentId> lastN(SearchParameterMap theParams, Integer theMaximumResults);
114
115        /**
116         * Returns inlined resource stored along with index mappings for matched identifiers
117         *
118         * @param thePids raw pids - we dont support versioned references
119         * @return Resources list or empty if nothing found
120         */
121        List<IBaseResource> getResources(Collection<Long> thePids);
122
123        /**
124         * Returns accurate hit count
125         */
126        long count(String theResourceName, SearchParameterMap theParams);
127
128        List<IBaseResource> searchForResources(
129                        String theResourceType, SearchParameterMap theParams, RequestDetails theRequestDetails);
130
131        boolean supportsAllOf(SearchParameterMap theParams);
132
133        /**
134         * Given a resource type that is indexed by hibernate search, and a list of objects reprenting the IDs you wish to delete,
135         * this method will delete the resources from the Hibernate Search index. This is useful for situations where a deletion occurred
136         * outside a Hibernate ORM session, leaving dangling documents in the index.
137         *
138         * @param theClazz The class, which must be annotated with {@link  org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed}
139         * @param theGivenIds The list of IDs for the given document type. Note that while this is a List<Object>, the type must match the type of the `@Id` field on the given class.
140         */
141        void deleteIndexedDocumentsByTypeAndId(Class theClazz, List<Object> theGivenIds);
142
143        /**
144         * Given a resource type and a {@link SearchParameterMap}, return true only if all sort terms are supported.
145         *
146         * @param theResourceName The resource type for the query.
147         * @param theParams The {@link SearchParameterMap} being searched with.
148         * @return true if all sort terms are supported, false otherwise.
149         */
150        boolean supportsAllSortTerms(String theResourceName, SearchParameterMap theParams);
151}