Search in sources :

Example 11 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 12 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)

Example 13 with MappedFieldType

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

the class FieldValueFactorFunctionBuilder method doToFunction.

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    MappedFieldType fieldType = context.getMapperService().fullName(field);
    IndexNumericFieldData fieldData = null;
    if (fieldType == null) {
        if (missing == null) {
            throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined.");
        }
    } else {
        fieldData = context.getForField(fieldType);
    }
    return new FieldValueFactorFunction(field, factor, modifier, missing, fieldData);
}
Also used : FieldValueFactorFunction(org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 14 with MappedFieldType

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

the class FuzzyQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    String rewrite = this.rewrite;
    if (rewrite == null && context.isFilter()) {
        rewrite = QueryParsers.CONSTANT_SCORE.getPreferredName();
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions);
    }
    if (query == null) {
        int maxEdits = fuzziness.asDistance(BytesRefs.toString(value));
        query = new FuzzyQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), maxEdits, prefixLength, maxExpansions, transpositions);
    }
    if (query instanceof MultiTermQuery) {
        MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null);
        QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 15 with MappedFieldType

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

the class GeoBoundingBoxQueryBuilder method doToQuery.

@Override
public Query doToQuery(QueryShardContext context) {
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_point field [" + fieldName + "]");
        }
    }
    if (!(fieldType instanceof GeoPointFieldType)) {
        throw new QueryShardException(context, "field [" + fieldName + "] is not a geo_point field");
    }
    QueryValidationException exception = checkLatLon(context.indexVersionCreated().before(Version.V_2_0_0));
    if (exception != null) {
        throw new QueryShardException(context, "couldn't validate latitude/ longitude values", exception);
    }
    GeoPoint luceneTopLeft = new GeoPoint(topLeft);
    GeoPoint luceneBottomRight = new GeoPoint(bottomRight);
    final Version indexVersionCreated = context.indexVersionCreated();
    if (indexVersionCreated.onOrAfter(Version.V_2_2_0) || GeoValidationMethod.isCoerce(validationMethod)) {
        // Special case: if the difference between the left and right is 360 and the right is greater than the left, we are asking for
        // the complete longitude range so need to set longitude to the complete longitude range
        double right = luceneBottomRight.getLon();
        double left = luceneTopLeft.getLon();
        boolean completeLonRange = ((right - left) % 360 == 0 && right > left);
        GeoUtils.normalizePoint(luceneTopLeft, true, !completeLonRange);
        GeoUtils.normalizePoint(luceneBottomRight, true, !completeLonRange);
        if (completeLonRange) {
            luceneTopLeft.resetLon(-180);
            luceneBottomRight.resetLon(180);
        }
    }
    Query query = LatLonPoint.newBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
    if (fieldType.hasDocValues()) {
        Query dvQuery = LatLonDocValuesField.newBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Version(org.elasticsearch.Version) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) GeoPointFieldType(org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)130 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)21 Term (org.apache.lucene.index.Term)18 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 TermQuery (org.apache.lucene.search.TermQuery)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 Analyzer (org.apache.lucene.analysis.Analyzer)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8