Search in sources :

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

Example 14 with MatchNoDocsQuery

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

use of org.apache.lucene.search.MatchNoDocsQuery in project lucene-solr by apache.

the class BinaryPoint method newSetQuery.

/**
   * Create a query matching any of the specified 1D values.  This is the points equivalent of {@code TermsQuery}.
   * 
   * @param field field name. must not be {@code null}.
   * @param values all values to match
   */
public static Query newSetQuery(String field, byte[]... values) {
    // Make sure all byte[] have the same length
    int bytesPerDim = -1;
    for (byte[] value : values) {
        if (bytesPerDim == -1) {
            bytesPerDim = value.length;
        } else if (value.length != bytesPerDim) {
            throw new IllegalArgumentException("all byte[] must be the same length, but saw " + bytesPerDim + " and " + value.length);
        }
    }
    if (bytesPerDim == -1) {
        // There are no points, and we cannot guess the bytesPerDim here, so we return an equivalent query:
        return new MatchNoDocsQuery("empty BinaryPoint.newSetQuery");
    }
    // Don't unexpectedly change the user's incoming values array:
    byte[][] sortedValues = values.clone();
    Arrays.sort(sortedValues, new Comparator<byte[]>() {

        @Override
        public int compare(byte[] a, byte[] b) {
            return StringHelper.compare(a.length, a, 0, b, 0);
        }
    });
    final BytesRef encoded = new BytesRef(new byte[bytesPerDim]);
    return new PointInSetQuery(field, 1, bytesPerDim, new PointInSetQuery.Stream() {

        int upto;

        @Override
        public BytesRef next() {
            if (upto == sortedValues.length) {
                return null;
            } else {
                encoded.bytes = sortedValues[upto];
                upto++;
                return encoded;
            }
        }
    }) {

        @Override
        protected String toString(byte[] value) {
            return new BytesRef(value).toString();
        }
    };
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)42 Query (org.apache.lucene.search.Query)25 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 TermQuery (org.apache.lucene.search.TermQuery)11 Term (org.apache.lucene.index.Term)9 ArrayList (java.util.ArrayList)7 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)7 Directory (org.apache.lucene.store.Directory)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)7 Document (org.apache.lucene.document.Document)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 List (java.util.List)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 BytesRef (org.apache.lucene.util.BytesRef)4 LongPoint (org.apache.lucene.document.LongPoint)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3