Search in sources :

Example 46 with MappedFieldType

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

the class LeafDocLookup method get.

@Override
public ScriptDocValues<?> get(Object key) {
    // assume its a string...
    String fieldName = key.toString();
    ScriptDocValues<?> scriptValues = localCacheFieldData.get(fieldName);
    if (scriptValues == null) {
        final MappedFieldType fieldType = mapperService.fullName(fieldName);
        if (fieldType == null) {
            throw new IllegalArgumentException("No field found for [" + fieldName + "] in mapping with types " + Arrays.toString(types) + "");
        }
        // load fielddata on behalf of the script: otherwise it would need additional permissions
        // to deal with pagedbytes/ramusagestimator/etc
        scriptValues = AccessController.doPrivileged(new PrivilegedAction<ScriptDocValues<?>>() {

            @Override
            public ScriptDocValues<?> run() {
                return fieldDataService.getForField(fieldType).load(reader).getScriptValues();
            }
        });
        localCacheFieldData.put(fieldName, scriptValues);
    }
    scriptValues.setNextDocId(docId);
    return scriptValues;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

Example 47 with MappedFieldType

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

the class TransportAnalyzeAction method shardOperation.

@Override
protected AnalyzeResponse shardOperation(AnalyzeRequest request, ShardId shardId) {
    try {
        final IndexService indexService;
        if (shardId != null) {
            indexService = indicesService.indexServiceSafe(shardId.getIndex());
        } else {
            indexService = null;
        }
        String field = null;
        Analyzer analyzer = null;
        if (request.field() != null) {
            if (indexService == null) {
                throw new IllegalArgumentException("No index provided, and trying to analyzer based on a specific field which requires the index parameter");
            }
            MappedFieldType fieldType = indexService.mapperService().fullName(request.field());
            if (fieldType != null) {
                if (fieldType.tokenized()) {
                    analyzer = fieldType.indexAnalyzer();
                } else if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
                    analyzer = ((KeywordFieldMapper.KeywordFieldType) fieldType).normalizer();
                    if (analyzer == null) {
                        // this will be KeywordAnalyzer
                        analyzer = fieldType.indexAnalyzer();
                    }
                } else {
                    throw new IllegalArgumentException("Can't process field [" + request.field() + "], Analysis requests are only supported on tokenized fields");
                }
                field = fieldType.name();
            }
        }
        if (field == null) {
            if (indexService != null) {
                field = indexService.getIndexSettings().getDefaultField();
            } else {
                field = AllFieldMapper.NAME;
            }
        }
        final AnalysisRegistry analysisRegistry = indicesService.getAnalysis();
        return analyze(request, field, analyzer, indexService != null ? indexService.getIndexAnalyzers() : null, analysisRegistry, environment);
    } catch (IOException e) {
        throw new ElasticsearchException("analysis failed", e);
    }
}
Also used : AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) IndexService(org.elasticsearch.index.IndexService) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) CustomAnalyzer(org.elasticsearch.index.analysis.CustomAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 48 with MappedFieldType

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

the class PrefixQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null);
    Query query = null;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.prefixQuery(value, method, context);
    }
    if (query == null) {
        PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, BytesRefs.toBytesRef(value)));
        if (method != null) {
            prefixQuery.setRewriteMethod(method);
        }
        query = prefixQuery;
    }
    return query;
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 49 with MappedFieldType

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

the class MoreLikeThisQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Item[] likeItems = new Item[this.likeItems.length];
    for (int i = 0; i < likeItems.length; i++) {
        likeItems[i] = new Item(this.likeItems[i]);
    }
    Item[] unlikeItems = new Item[this.unlikeItems.length];
    for (int i = 0; i < unlikeItems.length; i++) {
        unlikeItems[i] = new Item(this.unlikeItems[i]);
    }
    MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
    // set similarity
    mltQuery.setSimilarity(context.getSearchSimilarity());
    // set query parameters
    mltQuery.setMaxQueryTerms(maxQueryTerms);
    mltQuery.setMinTermFrequency(minTermFreq);
    mltQuery.setMinDocFreq(minDocFreq);
    mltQuery.setMaxDocFreq(maxDocFreq);
    mltQuery.setMinWordLen(minWordLength);
    mltQuery.setMaxWordLen(maxWordLength);
    mltQuery.setMinimumShouldMatch(minimumShouldMatch);
    if (stopWords != null) {
        mltQuery.setStopWords(new HashSet<>(Arrays.asList(stopWords)));
    }
    // sets boost terms
    if (boostTerms != 0) {
        mltQuery.setBoostTerms(true);
        mltQuery.setBoostTermsFactor(boostTerms);
    }
    // set analyzer
    Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
    if (analyzerObj == null) {
        analyzerObj = context.getMapperService().searchAnalyzer();
    }
    mltQuery.setAnalyzer(analyzerObj);
    // set like text fields
    boolean useDefaultField = (fields == null);
    List<String> moreLikeFields = new ArrayList<>();
    if (useDefaultField) {
        moreLikeFields = Collections.singletonList(context.defaultField());
    } else {
        for (String field : fields) {
            MappedFieldType fieldType = context.fieldMapper(field);
            if (fieldType != null && SUPPORTED_FIELD_TYPES.contains(fieldType.getClass()) == false) {
                if (failOnUnsupportedField) {
                    throw new IllegalArgumentException("more_like_this only supports text/keyword fields: [" + field + "]");
                } else {
                    // skip
                    continue;
                }
            }
            moreLikeFields.add(fieldType == null ? field : fieldType.name());
        }
    }
    if (moreLikeFields.isEmpty()) {
        return null;
    }
    mltQuery.setMoreLikeFields(moreLikeFields.toArray(new String[moreLikeFields.size()]));
    // handle like texts
    if (likeTexts.length > 0) {
        mltQuery.setLikeText(likeTexts);
    }
    if (unlikeTexts.length > 0) {
        mltQuery.setUnlikeText(unlikeTexts);
    }
    // handle items
    if (likeItems.length > 0) {
        return handleItems(context, mltQuery, likeItems, unlikeItems, include, moreLikeFields, useDefaultField);
    } else {
        return mltQuery;
    }
}
Also used : MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery) ArrayList(java.util.ArrayList) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 50 with MappedFieldType

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

the class DecayFunctionBuilder method parseVariable.

private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryShardContext context, MultiValueMode mode) throws IOException {
    //the field must exist, else we cannot read the value for the doc later
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new ParsingException(parser.getTokenLocation(), "unknown field [{}]", fieldName);
    }
    // dates and time and geo need special handling
    parser.nextToken();
    if (fieldType instanceof DateFieldMapper.DateFieldType) {
        return parseDateVariable(parser, context, fieldType, mode);
    } else if (fieldType instanceof GeoPointFieldType) {
        return parseGeoVariable(parser, context, fieldType, mode);
    } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
        return parseNumberVariable(parser, context, fieldType, mode);
    } else {
        throw new ParsingException(parser.getTokenLocation(), "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) GeoPointFieldType(org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType)

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