Search in sources :

Example 1 with SourceFieldMapper

use of org.elasticsearch.index.mapper.SourceFieldMapper in project elasticsearch by elastic.

the class HighlightPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.highlight() == null) {
        return;
    }
    Map<String, HighlightField> highlightFields = new HashMap<>();
    for (SearchContextHighlight.Field field : context.highlight().fields()) {
        Collection<String> fieldNamesToHighlight;
        if (Regex.isSimpleMatchPattern(field.field())) {
            DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().getType());
            fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field());
        } else {
            fieldNamesToHighlight = Collections.singletonList(field.field());
        }
        if (context.highlight().forceSource(field)) {
            SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper(hitContext.hit().getType()).sourceMapper();
            if (!sourceFieldMapper.enabled()) {
                throw new IllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but type [" + hitContext.hit().getType() + "] has disabled _source");
            }
        }
        boolean fieldNameContainsWildcards = field.field().contains("*");
        for (String fieldName : fieldNamesToHighlight) {
            FieldMapper fieldMapper = getMapperForField(fieldName, context, hitContext);
            if (fieldMapper == null) {
                continue;
            }
            // what they were doing and try to highlight anyway.
            if (fieldNameContainsWildcards) {
                if (fieldMapper.fieldType().typeName().equals(TextFieldMapper.CONTENT_TYPE) == false && fieldMapper.fieldType().typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false) {
                    continue;
                }
            }
            String highlighterType = field.fieldOptions().highlighterType();
            if (highlighterType == null) {
                for (String highlighterCandidate : STANDARD_HIGHLIGHTERS_BY_PRECEDENCE) {
                    if (highlighters.get(highlighterCandidate).canHighlight(fieldMapper)) {
                        highlighterType = highlighterCandidate;
                        break;
                    }
                }
                assert highlighterType != null;
            }
            Highlighter highlighter = highlighters.get(highlighterType);
            if (highlighter == null) {
                throw new IllegalArgumentException("unknown highlighter type [" + highlighterType + "] for the field [" + fieldName + "]");
            }
            Query highlightQuery = field.fieldOptions().highlightQuery();
            if (highlightQuery == null) {
                highlightQuery = context.parsedQuery().query();
            }
            HighlighterContext highlighterContext = new HighlighterContext(fieldName, field, fieldMapper, context, hitContext, highlightQuery);
            if ((highlighter.canHighlight(fieldMapper) == false) && fieldNameContainsWildcards) {
                // if several fieldnames matched the wildcard then we want to skip those that we cannot highlight
                continue;
            }
            HighlightField highlightField = highlighter.highlight(highlighterContext);
            if (highlightField != null) {
                highlightFields.put(highlightField.name(), highlightField);
            }
        }
    }
    hitContext.hit().highlightFields(highlightFields);
}
Also used : Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) SourceFieldMapper(org.elasticsearch.index.mapper.SourceFieldMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) SourceFieldMapper(org.elasticsearch.index.mapper.SourceFieldMapper) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper)

Aggregations

HashMap (java.util.HashMap)1 Query (org.apache.lucene.search.Query)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 KeywordFieldMapper (org.elasticsearch.index.mapper.KeywordFieldMapper)1 SourceFieldMapper (org.elasticsearch.index.mapper.SourceFieldMapper)1 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)1