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 org.hibernate.search.engine.backend.document.DocumentElement;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029public class HibernateSearchIndexWriter {
030        private static final Logger ourLog = LoggerFactory.getLogger(HibernateSearchIndexWriter.class);
031        public static final String IDX_STRING_NORMALIZED = "norm";
032        public static final String IDX_STRING_EXACT = "exact";
033        public static final String IDX_STRING_TEXT = "text";
034        final HibernateSearchElementCache myNodeCache;
035        final FhirContext myFhirContext;
036
037        HibernateSearchIndexWriter(FhirContext theFhirContext, DocumentElement theRoot) {
038                myFhirContext = theFhirContext;
039                myNodeCache = new HibernateSearchElementCache(theRoot);
040        }
041
042        public DocumentElement getSearchParamIndexNode(String theSearchParamName, String theIndexType) {
043                return myNodeCache.getObjectElement("sp", theSearchParamName, theIndexType);
044
045        }
046
047        public static HibernateSearchIndexWriter forRoot(FhirContext theFhirContext, DocumentElement theDocument) {
048                return new HibernateSearchIndexWriter(theFhirContext, theDocument);
049        }
050
051        public void writeStringIndex(String theSearchParam, String theValue) {
052                DocumentElement stringIndexNode = getSearchParamIndexNode(theSearchParam, "string");
053
054                stringIndexNode.addValue(IDX_STRING_NORMALIZED, theValue);// for default search
055                stringIndexNode.addValue(IDX_STRING_EXACT, theValue);
056                stringIndexNode.addValue(IDX_STRING_TEXT, theValue);
057                ourLog.debug("Adding Search Param Text: {} -- {}", theSearchParam, theValue);
058        }
059
060        public void writeTokenIndex(String theSearchParam, TokenParam theValue) {
061                DocumentElement tokenIndexNode = getSearchParamIndexNode(theSearchParam, "token");
062                // TODO mb we can use a token_filter with pattern_capture to generate all three off a single value.  Do this next, after merge.
063                tokenIndexNode.addValue("code", theValue.getValue());
064                tokenIndexNode.addValue("system", theValue.getSystem());
065                //This next one returns as system|value
066                tokenIndexNode.addValue("code-system", theValue.getValueAsQueryToken(myFhirContext));
067                ourLog.debug("Adding Search Param Token: {} -- {}", theSearchParam, theValue);
068        }
069
070    public void writeReferenceIndex(String theSearchParam, String theValue) {
071                 DocumentElement referenceIndexNode = getSearchParamIndexNode(theSearchParam, "reference");
072                 referenceIndexNode.addValue("value", theValue);
073                 ourLog.trace("Adding Search Param Reference: {} -- {}", theSearchParam, theValue);
074    }
075}