Search in sources :

Example 61 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.

the class DoubleColumnReferenceTest method testFieldCacheExpression.

@Test
public void testFieldCacheExpression() throws Exception {
    MappedFieldType fieldType = DoubleFieldMapper.Defaults.FIELD_TYPE.clone();
    fieldType.setNames(new MappedFieldType.Names("d"));
    DoubleColumnReference doubleColumn = new DoubleColumnReference(column, fieldType);
    doubleColumn.startCollect(ctx);
    doubleColumn.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10);
    double d = 0.5;
    for (ScoreDoc doc : topDocs.scoreDocs) {
        doubleColumn.setNextDocId(doc.doc);
        assertThat(doubleColumn.value(), is(d));
        d++;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) DoubleColumnReference(io.crate.operation.reference.doc.lucene.DoubleColumnReference) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 62 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.

the class FloatColumnReferenceTest method testFieldCacheExpression.

@Test
public void testFieldCacheExpression() throws Exception {
    MappedFieldType fieldType = FloatFieldMapper.Defaults.FIELD_TYPE.clone();
    fieldType.setNames(new MappedFieldType.Names(column));
    FloatColumnReference floatColumn = new FloatColumnReference(column, fieldType);
    floatColumn.startCollect(ctx);
    floatColumn.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10);
    float f = -0.5f;
    for (ScoreDoc doc : topDocs.scoreDocs) {
        floatColumn.setNextDocId(doc.doc);
        assertThat(floatColumn.value(), is(f));
        f++;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FloatColumnReference(io.crate.operation.reference.doc.lucene.FloatColumnReference) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 63 with MappedFieldType

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

the class TermVectorsService method addGeneratedTermVectors.

private static Fields addGeneratedTermVectors(IndexShard indexShard, Engine.GetResult get, Fields termVectorsByField, TermVectorsRequest request, Set<String> selectedFields) throws IOException {
    /* only keep valid fields */
    Set<String> validFields = new HashSet<>();
    for (String field : selectedFields) {
        MappedFieldType fieldType = indexShard.mapperService().fullName(field);
        if (!isValidField(fieldType)) {
            continue;
        }
        // already retrieved, only if the analyzer hasn't been overridden at the field
        if (fieldType.storeTermVectors() && (request.perFieldAnalyzer() == null || !request.perFieldAnalyzer().containsKey(field))) {
            continue;
        }
        validFields.add(field);
    }
    if (validFields.isEmpty()) {
        return termVectorsByField;
    }
    /* generate term vectors from fetched document fields */
    String[] getFields = validFields.toArray(new String[validFields.size() + 1]);
    getFields[getFields.length - 1] = SourceFieldMapper.NAME;
    GetResult getResult = indexShard.getService().get(get, request.id(), request.type(), getFields, null);
    Fields generatedTermVectors = generateTermVectors(indexShard, getResult.sourceAsMap(), getResult.getFields().values(), request.offsets(), request.perFieldAnalyzer(), validFields);
    /* merge with existing Fields */
    if (termVectorsByField == null) {
        return generatedTermVectors;
    } else {
        return mergeFields(termVectorsByField, generatedTermVectors);
    }
}
Also used : Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) GetResult(org.elasticsearch.index.get.GetResult) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Example 64 with MappedFieldType

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

the class TermVectorsService method generateTermVectorsFromDoc.

private static Fields generateTermVectorsFromDoc(IndexShard indexShard, TermVectorsRequest request) throws IOException {
    // parse the document, at the moment we do update the mapping, just like percolate
    ParsedDocument parsedDocument = parseDocument(indexShard, indexShard.shardId().getIndexName(), request.type(), request.doc(), request.xContentType());
    // select the right fields and generate term vectors
    ParseContext.Document doc = parsedDocument.rootDoc();
    Set<String> seenFields = new HashSet<>();
    Collection<GetField> getFields = new HashSet<>();
    for (IndexableField field : doc.getFields()) {
        MappedFieldType fieldType = indexShard.mapperService().fullName(field.name());
        if (!isValidField(fieldType)) {
            continue;
        }
        if (request.selectedFields() != null && !request.selectedFields().contains(field.name())) {
            continue;
        }
        if (seenFields.contains(field.name())) {
            continue;
        } else {
            seenFields.add(field.name());
        }
        String[] values = doc.getValues(field.name());
        getFields.add(new GetField(field.name(), Arrays.asList((Object[]) values)));
    }
    return generateTermVectors(indexShard, XContentHelper.convertToMap(parsedDocument.source(), true, request.xContentType()).v2(), getFields, request.offsets(), request.perFieldAnalyzer(), seenFields);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) GetField(org.elasticsearch.index.get.GetField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ParseContext(org.elasticsearch.index.mapper.ParseContext) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Example 65 with MappedFieldType

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

the class ValuesSourceConfig method resolve.

/**
     * Resolve a {@link ValuesSourceConfig} given configuration parameters.
     */
public static <VS extends ValuesSource> ValuesSourceConfig<VS> resolve(QueryShardContext context, ValueType valueType, String field, Script script, Object missing, DateTimeZone timeZone, String format) {
    if (field == null) {
        if (script == null) {
            @SuppressWarnings("unchecked") ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY);
            config.format(resolveFormat(null, valueType));
            return config;
        }
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : ValuesSourceType.ANY;
        if (valuesSourceType == ValuesSourceType.ANY) {
            // the specific value source type is undefined, but for scripts,
            // we need to have a specific value source
            // type to know how to handle the script values, so we fallback
            // on Bytes
            valuesSourceType = ValuesSourceType.BYTES;
        }
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<VS>(valuesSourceType);
        config.missing(missing);
        config.timezone(timeZone);
        config.format(resolveFormat(format, valueType));
        config.script(createScript(script, context));
        config.scriptValueType(valueType);
        return config;
    }
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : ValuesSourceType.ANY;
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
        config.missing(missing);
        config.timezone(timeZone);
        config.format(resolveFormat(format, valueType));
        config.unmapped(true);
        if (valueType != null) {
            // todo do we really need this for unmapped?
            config.scriptValueType(valueType);
        }
        return config;
    }
    IndexFieldData<?> indexFieldData = context.getForField(fieldType);
    ValuesSourceConfig<VS> config;
    if (valueType == null) {
        if (indexFieldData instanceof IndexNumericFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC);
        } else if (indexFieldData instanceof IndexGeoPointFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.GEOPOINT);
        } else {
            config = new ValuesSourceConfig<>(ValuesSourceType.BYTES);
        }
    } else {
        config = new ValuesSourceConfig<>(valueType.getValuesSourceType());
    }
    config.fieldContext(new FieldContext(field, indexFieldData, fieldType));
    config.missing(missing);
    config.timezone(timeZone);
    config.script(createScript(script, context));
    config.format(fieldType.docValueFormat(format, timeZone));
    return config;
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)122 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 IndexReader (org.apache.lucene.index.IndexReader)33 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)29 Directory (org.apache.lucene.store.Directory)29 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 Document (org.apache.lucene.document.Document)23 Query (org.apache.lucene.search.Query)19 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 Term (org.apache.lucene.index.Term)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 TermQuery (org.apache.lucene.search.TermQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8 Analyzer (org.apache.lucene.analysis.Analyzer)7