Search in sources :

Example 16 with MappedFieldType

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

the class GeoDistanceQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext shardContext) throws IOException {
    MappedFieldType fieldType = shardContext.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(shardContext, "failed to find geo_point field [" + fieldName + "]");
        }
    }
    if (!(fieldType instanceof GeoPointFieldType)) {
        throw new QueryShardException(shardContext, "field [" + fieldName + "] is not a geo_point field");
    }
    final Version indexVersionCreated = shardContext.indexVersionCreated();
    QueryValidationException exception = checkLatLon(shardContext.indexVersionCreated().before(Version.V_2_0_0));
    if (exception != null) {
        throw new QueryShardException(shardContext, "couldn't validate latitude/ longitude values", exception);
    }
    if (indexVersionCreated.onOrAfter(Version.V_2_2_0) || GeoValidationMethod.isCoerce(validationMethod)) {
        GeoUtils.normalizePoint(center, true, true);
    }
    Query query = LatLonPoint.newDistanceQuery(fieldType.name(), center.lat(), center.lon(), this.distance);
    if (fieldType.hasDocValues()) {
        Query dvQuery = LatLonDocValuesField.newDistanceQuery(fieldType.name(), center.lat(), center.lon(), this.distance);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
Also used : 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)

Example 17 with MappedFieldType

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

the class GeoPolygonQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    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");
    }
    List<GeoPoint> shell = new ArrayList<GeoPoint>();
    for (GeoPoint geoPoint : this.shell) {
        shell.add(new GeoPoint(geoPoint));
    }
    final int shellSize = shell.size();
    // percolation queries we only ignore_malformed on 2.x created indexes
    if (!GeoValidationMethod.isIgnoreMalformed(validationMethod)) {
        for (GeoPoint point : shell) {
            if (!GeoUtils.isValidLatitude(point.lat())) {
                throw new QueryShardException(context, "illegal latitude value [{}] for [{}]", point.lat(), GeoPolygonQueryBuilder.NAME);
            }
            if (!GeoUtils.isValidLongitude(point.lat())) {
                throw new QueryShardException(context, "illegal longitude value [{}] for [{}]", point.lon(), GeoPolygonQueryBuilder.NAME);
            }
        }
    }
    if (GeoValidationMethod.isCoerce(validationMethod)) {
        for (GeoPoint point : shell) {
            GeoUtils.normalizePoint(point, true, true);
        }
    }
    double[] lats = new double[shellSize];
    double[] lons = new double[shellSize];
    GeoPoint p;
    for (int i = 0; i < shellSize; ++i) {
        p = shell.get(i);
        lats[i] = p.lat();
        lons[i] = p.lon();
    }
    return LatLonPoint.newPolygonQuery(fieldType.name(), new Polygon(lats, lons));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ArrayList(java.util.ArrayList) GeoPointFieldType(org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType) Polygon(org.apache.lucene.geo.Polygon) LatLonPoint(org.apache.lucene.document.LatLonPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 18 with MappedFieldType

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

the class SpanTermQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType mapper = context.fieldMapper(fieldName);
    Term term;
    if (mapper == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = mapper.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    return new SpanTermQuery(term);
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 19 with MappedFieldType

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

the class IndicesService method getFieldStats.

/**
     * Fetch {@linkplain FieldStats} for a field. These stats are cached until the shard changes.
     * @param shard the shard to use with the cache key
     * @param searcher searcher to use to lookup the field stats
     * @param field the actual field
     * @param useCache should this request use the cache?
     */
public FieldStats<?> getFieldStats(IndexShard shard, Engine.Searcher searcher, String field, boolean useCache) throws Exception {
    MappedFieldType fieldType = shard.mapperService().fullName(field);
    if (fieldType == null) {
        return null;
    }
    if (useCache == false) {
        return fieldType.stats(searcher.reader());
    }
    BytesReference cacheKey = new BytesArray("fieldstats:" + field);
    BytesReference statsRef = cacheShardLevelResult(shard, searcher.getDirectoryReader(), cacheKey, out -> {
        try {
            out.writeOptionalWriteable(fieldType.stats(searcher.reader()));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to write field stats output", e);
        }
    });
    try (StreamInput in = statsRef.streamInput()) {
        return in.readOptionalWriteable(FieldStats::readFrom);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) IOException(java.io.IOException) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 20 with MappedFieldType

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

the class CategoryContextMappingTests 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", "ctx").field("type", "category").endObject().startObject().field("name", "type").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();
    XContentBuilder builder = jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").field("weight", 5).startObject("contexts").array("ctx", "ctx1", "ctx2", "ctx3").array("type", "typr3", "ftg").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)

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