001package ca.uhn.fhir.jpa.model.search;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Model
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.rest.param.TokenParam;
025import com.google.common.collect.HashMultimap;
026import com.google.common.collect.SetMultimap;
027import org.hibernate.search.engine.backend.document.DocumentElement;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import java.util.function.BiConsumer;
032
033/**
034 * Collects our lucene extended indexing data.
035 *
036 */
037public class ExtendedLuceneIndexData {
038        private static final Logger ourLog = LoggerFactory.getLogger(ExtendedLuceneIndexData.class);
039
040        final FhirContext myFhirContext;
041        final SetMultimap<String, String> mySearchParamStrings = HashMultimap.create();
042        final SetMultimap<String, TokenParam> mySearchParamTokens = HashMultimap.create();
043        final SetMultimap<String, String> mySearchParamLinks = HashMultimap.create();
044
045        public ExtendedLuceneIndexData(FhirContext theFhirContext) {
046                this.myFhirContext = theFhirContext;
047        }
048
049        private <V> BiConsumer<String, V> ifNotContained(BiConsumer<String, V> theIndexWriter) {
050                return (s,v) -> {
051                        // Ignore contained resources for now.
052                        if (!s.contains(".")) {
053                                theIndexWriter.accept(s,v);
054                        }
055                };
056        }
057        public void writeIndexElements(DocumentElement theDocument) {
058                HibernateSearchIndexWriter indexWriter = HibernateSearchIndexWriter.forRoot(myFhirContext, theDocument);
059
060                ourLog.debug("Writing JPA index to Hibernate Search");
061
062                mySearchParamStrings.forEach(ifNotContained(indexWriter::writeStringIndex));
063                mySearchParamTokens.forEach(ifNotContained(indexWriter::writeTokenIndex));
064                mySearchParamLinks.forEach(ifNotContained(indexWriter::writeReferenceIndex));
065        }
066
067        public void addStringIndexData(String theSpName, String theText) {
068                mySearchParamStrings.put(theSpName, theText);
069        }
070
071        public void addTokenIndexData(String theSpName, String theSystem,  String theValue) {
072                mySearchParamTokens.put(theSpName, new TokenParam(theSystem, theValue));
073        }
074
075        public void addResourceLinkIndexData(String theSpName, String theTargetResourceId){
076                mySearchParamLinks.put(theSpName, theTargetResourceId);
077        }
078}