Search in sources :

Example 16 with MappedFieldType

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

the class IndexSortConfig method buildIndexSort.

/**
 * Builds the {@link Sort} order from the settings for this index
 * or returns null if this index has no sort.
 */
public Sort buildIndexSort(Function<String, MappedFieldType> fieldTypeLookup, BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> fieldDataLookup) {
    if (hasIndexSort() == false) {
        return null;
    }
    final SortField[] sortFields = new SortField[sortSpecs.length];
    for (int i = 0; i < sortSpecs.length; i++) {
        FieldSortSpec sortSpec = sortSpecs[i];
        final MappedFieldType ft = fieldTypeLookup.apply(sortSpec.field);
        if (ft == null) {
            throw new IllegalArgumentException("unknown index sort field:[" + sortSpec.field + "]");
        }
        boolean reverse = sortSpec.order == null ? false : (sortSpec.order == SortOrder.DESC);
        MultiValueMode mode = sortSpec.mode;
        if (mode == null) {
            mode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
        }
        IndexFieldData<?> fieldData;
        try {
            fieldData = fieldDataLookup.apply(ft, () -> {
                throw new UnsupportedOperationException("index sorting not supported on runtime field [" + ft.name() + "]");
            });
        } catch (Exception e) {
            throw new IllegalArgumentException("docvalues not found for index sort field:[" + sortSpec.field + "]", e);
        }
        if (fieldData == null) {
            throw new IllegalArgumentException("docvalues not found for index sort field:[" + sortSpec.field + "]");
        }
        sortFields[i] = fieldData.sortField(sortSpec.missingValue, mode, null, reverse);
        validateIndexSortField(sortFields[i]);
    }
    return new Sort(sortFields);
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Sort(org.apache.lucene.search.Sort) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) MultiValueMode(org.opensearch.search.MultiValueMode)

Example 17 with MappedFieldType

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

the class FieldsVisitor method postProcess.

public final void postProcess(Function<String, MappedFieldType> fieldTypeLookup) {
    for (Map.Entry<String, List<Object>> entry : fields().entrySet()) {
        MappedFieldType fieldType = fieldTypeLookup.apply(entry.getKey());
        if (fieldType == null) {
            throw new IllegalStateException("Field [" + entry.getKey() + "] exists in the index but not in mappings");
        }
        List<Object> fieldValues = entry.getValue();
        for (int i = 0; i < fieldValues.size(); i++) {
            fieldValues.set(i, fieldType.valueForDisplay(fieldValues.get(i)));
        }
    }
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ArrayList(java.util.ArrayList) List(java.util.List) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with MappedFieldType

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

the class TermVectorsService method getAnalyzerAtField.

private static Analyzer getAnalyzerAtField(IndexShard indexShard, String field, @Nullable Map<String, String> perFieldAnalyzer) {
    MapperService mapperService = indexShard.mapperService();
    Analyzer analyzer;
    if (perFieldAnalyzer != null && perFieldAnalyzer.containsKey(field)) {
        analyzer = mapperService.getIndexAnalyzers().get(perFieldAnalyzer.get(field));
    } else {
        MappedFieldType fieldType = mapperService.fieldType(field);
        analyzer = fieldType.indexAnalyzer();
    }
    if (analyzer == null) {
        analyzer = mapperService.getIndexAnalyzers().getDefaultIndexAnalyzer();
    }
    return analyzer;
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Analyzer(org.apache.lucene.analysis.Analyzer) MapperService(org.opensearch.index.mapper.MapperService)

Example 19 with MappedFieldType

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

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().fieldType(field);
        if (!isValidField(fieldType)) {
            continue;
        }
        // already retrieved, only if the analyzer hasn't been overridden at the field
        if (fieldType.getTextSearchInfo().termVectors() != TextSearchInfo.TermVector.NONE && (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(), 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) GetResult(org.opensearch.index.get.GetResult) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Example 20 with MappedFieldType

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

the class ValuesSourceConfig method internalResolve.

private static ValuesSourceConfig internalResolve(QueryShardContext context, ValueType userValueTypeHint, String field, Script script, Object missing, ZoneId timeZone, String format, ValuesSourceType defaultValueSourceType, FieldResolver fieldResolver) {
    ValuesSourceConfig config;
    MappedFieldType fieldType = null;
    ValuesSourceType valuesSourceType = null;
    ValueType scriptValueType = userValueTypeHint;
    FieldContext fieldContext = null;
    // returns null if script is null
    AggregationScript.LeafFactory aggregationScript = createScript(script, context);
    boolean unmapped = false;
    if (userValueTypeHint != null) {
        // If the user gave us a type hint, respect that.
        valuesSourceType = userValueTypeHint.getValuesSourceType();
    }
    if (field == null) {
        if (script == null) {
            throw new IllegalStateException("value source config is invalid; must have either a field or a script");
        }
    } else {
        // Field case
        fieldType = context.fieldMapper(field);
        if (fieldType == null) {
            /* Unmapped Field Case
                 * We got here because the user specified a field, but it doesn't exist on this index, possibly because of a wildcard index
                 * pattern.  In this case, we're going to end up using the EMPTY variant of the ValuesSource, and possibly applying a user
                 * specified missing value.
                 */
            unmapped = true;
            // Value scripts are not allowed on unmapped fields. What would that do, anyway?
            aggregationScript = null;
        } else {
            fieldContext = new FieldContext(fieldType.name(), context.getForField(fieldType), fieldType);
            if (valuesSourceType == null) {
                // We have a field, and the user didn't specify a type, so get the type from the field
                valuesSourceType = fieldResolver.getValuesSourceType(fieldContext, userValueTypeHint, defaultValueSourceType);
            }
        }
    }
    if (valuesSourceType == null) {
        valuesSourceType = defaultValueSourceType;
    }
    DocValueFormat docValueFormat = resolveFormat(format, valuesSourceType, timeZone, fieldType);
    config = new ValuesSourceConfig(valuesSourceType, fieldContext, unmapped, aggregationScript, scriptValueType, missing, timeZone, docValueFormat, context::nowInMillis);
    return config;
}
Also used : DocValueFormat(org.opensearch.search.DocValueFormat) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AggregationScript(org.opensearch.script.AggregationScript)

Aggregations

MappedFieldType (org.opensearch.index.mapper.MappedFieldType)321 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)150 IndexReader (org.apache.lucene.index.IndexReader)146 Directory (org.apache.lucene.store.Directory)136 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)130 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)127 IndexSearcher (org.apache.lucene.search.IndexSearcher)124 Document (org.apache.lucene.document.Document)96 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)83 ArrayList (java.util.ArrayList)76 BytesRef (org.apache.lucene.util.BytesRef)64 Query (org.apache.lucene.search.Query)57 Script (org.opensearch.script.Script)51 IntPoint (org.apache.lucene.document.IntPoint)49 IOException (java.io.IOException)48 List (java.util.List)48 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)48 LongPoint (org.apache.lucene.document.LongPoint)47 NumberFieldMapper (org.opensearch.index.mapper.NumberFieldMapper)46 KeywordFieldMapper (org.opensearch.index.mapper.KeywordFieldMapper)42