Search in sources :

Example 6 with IndexOrDocValuesQuery

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

the class NumberFieldTypeTests method doTestDocValueRangeQueries.

public void doTestDocValueRangeQueries(NumberType type, Supplier<Number> valueSupplier) throws Exception {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
    final int numDocs = TestUtil.nextInt(random(), 100, 500);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(type.createFields("foo", valueSupplier.get(), true, true, false));
    }
    DirectoryReader reader = DirectoryReader.open(w);
    IndexSearcher searcher = newSearcher(reader);
    w.close();
    final int iters = 10;
    for (int iter = 0; iter < iters; ++iter) {
        Query query = type.rangeQuery("foo", random().nextBoolean() ? null : valueSupplier.get(), random().nextBoolean() ? null : valueSupplier.get(), randomBoolean(), randomBoolean(), true);
        assertThat(query, Matchers.instanceOf(IndexOrDocValuesQuery.class));
        IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) query;
        assertEquals(searcher.count(indexOrDvQuery.getIndexQuery()), searcher.count(indexOrDvQuery.getRandomAccessQuery()));
    }
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) Directory(org.apache.lucene.store.Directory)

Example 7 with IndexOrDocValuesQuery

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

the class RangeQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false && queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false)) {
        assertThat(query, instanceOf(TermRangeQuery.class));
        TermRangeQuery termRangeQuery = (TermRangeQuery) query;
        assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
        assertThat(termRangeQuery.getLowerTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.from())));
        assertThat(termRangeQuery.getUpperTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.to())));
        assertThat(termRangeQuery.includesLower(), equalTo(queryBuilder.includeLower()));
        assertThat(termRangeQuery.includesUpper(), equalTo(queryBuilder.includeUpper()));
    } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        MapperService mapperService = context.getQueryShardContext().getMapperService();
        MappedFieldType mappedFieldType = mapperService.fullName(DATE_FIELD_NAME);
        final Long fromInMillis;
        final Long toInMillis;
        // we have to normalize the incoming value into milliseconds since it could be literally anything
        if (mappedFieldType instanceof DateFieldMapper.DateFieldType) {
            fromInMillis = queryBuilder.from() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.from(), queryBuilder.includeLower(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
            toInMillis = queryBuilder.to() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.to(), queryBuilder.includeUpper(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
        } else {
            fromInMillis = toInMillis = null;
            fail("unexpected mapped field type: [" + mappedFieldType.getClass() + "] " + mappedFieldType.toString());
        }
        Long min = fromInMillis;
        Long max = toInMillis;
        long minLong, maxLong;
        if (min == null) {
            minLong = Long.MIN_VALUE;
        } else {
            minLong = min.longValue();
            if (queryBuilder.includeLower() == false && minLong != Long.MAX_VALUE) {
                minLong++;
            }
        }
        if (max == null) {
            maxLong = Long.MAX_VALUE;
        } else {
            maxLong = max.longValue();
            if (queryBuilder.includeUpper() == false && maxLong != Long.MIN_VALUE) {
                maxLong--;
            }
        }
        assertEquals(LongPoint.newRangeQuery(DATE_FIELD_NAME, minLong, maxLong), query);
    } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        Integer min = (Integer) queryBuilder.from();
        Integer max = (Integer) queryBuilder.to();
        int minInt, maxInt;
        if (min == null) {
            minInt = Integer.MIN_VALUE;
        } else {
            minInt = min.intValue();
            if (queryBuilder.includeLower() == false && minInt != Integer.MAX_VALUE) {
                minInt++;
            }
        }
        if (max == null) {
            maxInt = Integer.MAX_VALUE;
        } else {
            maxInt = max.intValue();
            if (queryBuilder.includeUpper() == false && maxInt != Integer.MIN_VALUE) {
                maxInt--;
            }
        }
    } else if (queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) || queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME)) {
    // todo can't check RangeFieldQuery because its currently package private (this will change)
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MapperService(org.elasticsearch.index.mapper.MapperService) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint)

Example 8 with IndexOrDocValuesQuery

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

the class RangeQueryBuilderTests method testDateRangeQueryTimezone.

public void testDateRangeQueryTimezone() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String query = "{\n" + "    \"range\" : {\n" + "        \"" + DATE_FIELD_NAME + "\" : {\n" + "            \"gte\": \"2012-01-01\",\n" + "            \"lte\": \"now\",\n" + "            \"time_zone\": \"+01:00\"\n" + "        }\n" + "    }\n" + "}";
    QueryShardContext context = createShardContext();
    Query parsedQuery = parseQuery(query).toQuery(context);
    assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class));
    parsedQuery = ((IndexOrDocValuesQuery) parsedQuery).getIndexQuery();
    assertThat(parsedQuery, instanceOf(PointRangeQuery.class));
    // TODO what else can we assert
    query = "{\n" + "    \"range\" : {\n" + "        \"" + INT_FIELD_NAME + "\" : {\n" + "            \"gte\": \"0\",\n" + "            \"lte\": \"100\",\n" + "            \"time_zone\": \"-01:00\"\n" + "        }\n" + "    }\n" + "}";
    QueryBuilder queryBuilder = parseQuery(query);
    expectThrows(QueryShardException.class, () -> queryBuilder.toQuery(createShardContext()));
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.rangeQuery(org.elasticsearch.index.query.QueryBuilders.rangeQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 9 with IndexOrDocValuesQuery

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

the class RangeQueryBuilderTests method testDateRangeQueryFormat.

public void testDateRangeQueryFormat() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    // We test 01/01/2012 from gte and 2030 for lt
    String query = "{\n" + "    \"range\" : {\n" + "        \"" + DATE_FIELD_NAME + "\" : {\n" + "            \"gte\": \"01/01/2012\",\n" + "            \"lt\": \"2030\",\n" + "            \"format\": \"dd/MM/yyyy||yyyy\"\n" + "        }\n" + "    }\n" + "}";
    Query parsedQuery = parseQuery(query).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class));
    parsedQuery = ((IndexOrDocValuesQuery) parsedQuery).getIndexQuery();
    assertThat(parsedQuery, instanceOf(PointRangeQuery.class));
    assertEquals(LongPoint.newRangeQuery(DATE_FIELD_NAME, DateTime.parse("2012-01-01T00:00:00.000+00").getMillis(), DateTime.parse("2030-01-01T00:00:00.000+00").getMillis() - 1), parsedQuery);
    // Test Invalid format
    final String invalidQuery = "{\n" + "    \"range\" : {\n" + "        \"" + DATE_FIELD_NAME + "\" : {\n" + "            \"gte\": \"01/01/2012\",\n" + "            \"lt\": \"2030\",\n" + "            \"format\": \"yyyy\"\n" + "        }\n" + "    }\n" + "}";
    expectThrows(ElasticsearchParseException.class, () -> parseQuery(invalidQuery).toQuery(createShardContext()));
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.rangeQuery(org.elasticsearch.index.query.QueryBuilders.rangeQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 10 with IndexOrDocValuesQuery

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

the class TestPointFields method testIndexOrDocValuesQuery.

@Test
public void testIndexOrDocValuesQuery() throws Exception {
    String[] fieldTypeNames = new String[] { "_p_i", "_p_l", "_p_d", "_p_f" };
    FieldType[] fieldTypes = new FieldType[] { new IntPointField(), new LongPointField(), new DoublePointField(), new FloatPointField() };
    assert fieldTypeNames.length == fieldTypes.length;
    for (int i = 0; i < fieldTypeNames.length; i++) {
        SchemaField fieldIndexed = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i]);
        SchemaField fieldIndexedAndDv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_dv");
        SchemaField fieldIndexedMv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_mv");
        SchemaField fieldIndexedAndDvMv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_mv_dv");
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexed, "0", "10", true, true) instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedAndDv, "0", "10", true, true) instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedMv, "0", "10", true, true) instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedAndDvMv, "0", "10", true, true) instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexed, "0") instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedAndDv, "0") instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedMv, "0") instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedAndDvMv, "0") instanceof IndexOrDocValuesQuery);
    }
}
Also used : PointRangeQuery(org.apache.lucene.search.PointRangeQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) Test(org.junit.Test)

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