Search in sources :

Example 16 with FieldMapper

use of org.elasticsearch.index.mapper.FieldMapper 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)

Example 17 with FieldMapper

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

the class GeoContextMappingTests method testIndexingWithNoContexts.

public void testIndexingWithNoContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "geo").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion1", "suggestion2").field("weight", 3).endObject().startObject().array("input", "suggestion3", "suggestion4").field("weight", 4).endObject().startObject().array("input", "suggestion5", "suggestion6", "suggestion7").field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 7);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 18 with FieldMapper

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

the class GeoContextMappingTests method testIndexingWithSimpleContexts.

public void testIndexingWithSimpleContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "geo").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").startObject("contexts").startObject("ctx").field("lat", 43.6624803).field("lon", -79.3863353).endObject().endObject().field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 19 with FieldMapper

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

the class GeoContextMappingTests method testIndexingWithMultipleContexts.

public void testIndexingWithMultipleContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "loc1").field("type", "geo").endObject().startObject().field("name", "loc2").field("type", "geo").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    XContentBuilder builder = jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").field("weight", 5).startObject("contexts").array("loc1", "ezs42e44yx96").array("loc2", "wh0n9447fwrc").endObject().endObject().endArray().endObject();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", builder.bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 20 with FieldMapper

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

the class CategoryContextMappingTests method testIndexingWithSimpleContexts.

public void testIndexingWithSimpleContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "category").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").startObject("contexts").field("ctx", "ctx1").endObject().field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Aggregations

FieldMapper (org.elasticsearch.index.mapper.FieldMapper)23 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)15 IndexableField (org.apache.lucene.index.IndexableField)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)12 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)8 IOException (java.io.IOException)4 Analyzer (org.apache.lucene.analysis.Analyzer)4 Encoder (org.apache.lucene.search.highlight.Encoder)4 BytesRef (org.apache.lucene.util.BytesRef)4 BytesArray (org.elasticsearch.common.bytes.BytesArray)4 IndexService (org.elasticsearch.index.IndexService)4 Document (org.elasticsearch.index.mapper.ParseContext.Document)4 FetchPhaseExecutionException (org.elasticsearch.search.fetch.FetchPhaseExecutionException)4 FetchSubPhase (org.elasticsearch.search.fetch.FetchSubPhase)4 SearchContext (org.elasticsearch.search.internal.SearchContext)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2