Search in sources :

Example 1 with IndexOrDocValuesQuery

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

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

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

the class DateFieldTypeTests method testTermQuery.

public void testTermQuery() {
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    String date = "2015-10-12T14:10:55";
    long instant = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date).getMillis();
    ft.setIndexOptions(IndexOptions.DOCS);
    Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant, instant + 999), SortedNumericDocValuesField.newRangeQuery("field", instant, instant + 999));
    assertEquals(expected, ft.termQuery(date, context));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.termQuery(date, context));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexSettings(org.elasticsearch.index.IndexSettings) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 4 with IndexOrDocValuesQuery

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

the class DateFieldTypeTests method testRangeQuery.

public void testRangeQuery() throws IOException {
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    String date1 = "2015-10-12T14:10:55";
    String date2 = "2016-04-28T11:33:52";
    long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date1).getMillis();
    long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date2).getMillis() + 999;
    ft.setIndexOptions(IndexOptions.DOCS);
    Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newRangeQuery("field", instant1, instant2));
    assertEquals(expected, ft.rangeQuery(date1, date2, true, true, context).rewrite(new MultiReader()));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(date1, date2, true, true, context));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MultiReader(org.apache.lucene.index.MultiReader) IndexSettings(org.elasticsearch.index.IndexSettings) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 5 with IndexOrDocValuesQuery

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

the class NumberFieldTypeTests method testRangeQuery.

public void testRangeQuery() {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", 1, 3), SortedNumericDocValuesField.newRangeQuery("field", 1, 3));
    assertEquals(expected, ft.rangeQuery("1", "3", true, true, null));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery("1", "3", true, true, null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Aggregations

IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)16 Query (org.apache.lucene.search.Query)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)6 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)6 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)5 IntPoint (org.apache.lucene.document.IntPoint)4 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 QueryBuilders.rangeQuery (org.elasticsearch.index.query.QueryBuilders.rangeQuery)4 LongPoint (org.apache.lucene.document.LongPoint)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 FloatPoint (org.apache.lucene.document.FloatPoint)2 Version (org.elasticsearch.Version)2 Settings (org.elasticsearch.common.settings.Settings)2 IndexSettings (org.elasticsearch.index.IndexSettings)2 GeoPointFieldType (org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 DoublePoint (org.apache.lucene.document.DoublePoint)1 HalfFloatPoint (org.apache.lucene.document.HalfFloatPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1