Search in sources :

Example 1 with SourceFieldMapper

use of org.opensearch.index.mapper.SourceFieldMapper in project OpenSearch by opensearch-project.

the class HighlightPhase method contextBuilders.

private Map<String, Function<HitContext, FieldHighlightContext>> contextBuilders(FetchContext context, SearchHighlightContext highlightContext, Query query, Map<String, Object> sharedCache) {
    Map<String, Function<HitContext, FieldHighlightContext>> builders = new LinkedHashMap<>();
    for (SearchHighlightContext.Field field : highlightContext.fields()) {
        Highlighter highlighter = getHighlighter(field);
        Collection<String> fieldNamesToHighlight;
        if (Regex.isSimpleMatchPattern(field.field())) {
            fieldNamesToHighlight = context.mapperService().simpleMatchToFullName(field.field());
        } else {
            fieldNamesToHighlight = Collections.singletonList(field.field());
        }
        if (highlightContext.forceSource(field)) {
            SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper().sourceMapper();
            if (sourceFieldMapper.enabled() == false) {
                throw new IllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but _source is disabled");
            }
        }
        boolean fieldNameContainsWildcards = field.field().contains("*");
        for (String fieldName : fieldNamesToHighlight) {
            MappedFieldType fieldType = context.mapperService().fieldType(fieldName);
            if (fieldType == null) {
                continue;
            }
            // what they were doing and try to highlight anyway.
            if (fieldNameContainsWildcards) {
                if (fieldType.typeName().equals(TextFieldMapper.CONTENT_TYPE) == false && fieldType.typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false) {
                    continue;
                }
                if (highlighter.canHighlight(fieldType) == false) {
                    continue;
                }
            }
            Query highlightQuery = field.fieldOptions().highlightQuery();
            boolean forceSource = highlightContext.forceSource(field);
            builders.put(fieldName, hc -> new FieldHighlightContext(fieldType.name(), field, fieldType, context, hc, highlightQuery == null ? query : highlightQuery, forceSource, sharedCache));
        }
    }
    return builders;
}
Also used : Query(org.apache.lucene.search.Query) SourceFieldMapper(org.opensearch.index.mapper.SourceFieldMapper) LinkedHashMap(java.util.LinkedHashMap) Function(java.util.function.Function) MappedFieldType(org.opensearch.index.mapper.MappedFieldType)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Function (java.util.function.Function)1 Query (org.apache.lucene.search.Query)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1 SourceFieldMapper (org.opensearch.index.mapper.SourceFieldMapper)1