Search in sources :

Example 1 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery in project elasticsearch by elastic.

the class SliceBuilder method toFilter.

public Query toFilter(QueryShardContext context, int shardId, int numShards) {
    final MappedFieldType type = context.fieldMapper(field);
    if (type == null) {
        throw new IllegalArgumentException("field " + field + " not found");
    }
    boolean useTermQuery = false;
    if (UidFieldMapper.NAME.equals(field)) {
        useTermQuery = true;
    } else if (type.hasDocValues() == false) {
        throw new IllegalArgumentException("cannot load numeric doc values on " + field);
    } else {
        IndexFieldData ifm = context.getForField(type);
        if (ifm instanceof IndexNumericFieldData == false) {
            throw new IllegalArgumentException("cannot load numeric doc values on " + field);
        }
    }
    if (numShards == 1) {
        return useTermQuery ? new TermsSliceQuery(field, id, max) : new DocValuesSliceQuery(field, id, max);
    }
    if (max >= numShards) {
        // the number of slices is greater than the number of shards
        // in such case we can reduce the number of requested shards by slice
        // first we check if the slice is responsible of this shard
        int targetShard = id % numShards;
        if (targetShard != shardId) {
            // the shard is not part of this slice, we can skip it.
            return new MatchNoDocsQuery("this shard is not part of the slice");
        }
        // compute the number of slices where this shard appears
        int numSlicesInShard = max / numShards;
        int rest = max % numShards;
        if (rest > targetShard) {
            numSlicesInShard++;
        }
        if (numSlicesInShard == 1) {
            // this shard has only one slice so we must check all the documents
            return new MatchAllDocsQuery();
        }
        // get the new slice id for this shard
        int shardSlice = id / numShards;
        return useTermQuery ? new TermsSliceQuery(field, shardSlice, numSlicesInShard) : new DocValuesSliceQuery(field, shardSlice, numSlicesInShard);
    }
    // the number of shards is greater than the number of slices
    // check if the shard is assigned to the slice
    int targetSlice = shardId % max;
    if (id != targetSlice) {
        // the shard is not part of this slice, we can skip it.
        return new MatchNoDocsQuery("this shard is not part of the slice");
    }
    return new MatchAllDocsQuery();
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 2 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery in project elasticsearch by elastic.

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new ESToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) NestedHelper(org.elasticsearch.index.search.NestedHelper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 3 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery in project elasticsearch by elastic.

the class ParentIdQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    DocumentMapper childDocMapper = context.getMapperService().documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type());
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST);
    // Need to take child type into account, otherwise a child doc of different type with the same id could match
    query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) DocValuesTermsQuery(org.apache.lucene.search.DocValuesTermsQuery) Term(org.apache.lucene.index.Term)

Example 4 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery 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)

Example 5 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery 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)

Aggregations

MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)47 Query (org.apache.lucene.search.Query)29 BooleanQuery (org.apache.lucene.search.BooleanQuery)16 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)15 TermQuery (org.apache.lucene.search.TermQuery)14 Term (org.apache.lucene.index.Term)11 ArrayList (java.util.ArrayList)8 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)8 Directory (org.apache.lucene.store.Directory)8 Document (org.apache.lucene.document.Document)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)7 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 BytesRef (org.apache.lucene.util.BytesRef)5 List (java.util.List)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)4