Search in sources :

Example 1 with IndexSortSortedNumericDocValuesRangeQuery

use of org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testRangeQueryWithIndexSort.

public void testRangeQueryWithIndexSort() {
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put("index.sort.field", "field").build();
    IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
    IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
    QueryShardContext context = new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> 0L, null, null, () -> true, null);
    MappedFieldType ft = new DateFieldType("field");
    String date1 = "2015-10-12T14:10:55";
    String date2 = "2016-04-28T11:33:52";
    long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli();
    long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999;
    Query pointQuery = LongPoint.newRangeQuery("field", instant1, instant2);
    Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2);
    Query expected = new IndexSortSortedNumericDocValuesRangeQuery("field", instant1, instant2, new IndexOrDocValuesQuery(pointQuery, dvQuery));
    assertEquals(expected, ft.rangeQuery(date1, date2, true, true, null, null, null, context));
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) DateRangeIncludingNowQuery(org.opensearch.index.query.DateRangeIncludingNowQuery) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)

Example 2 with IndexSortSortedNumericDocValuesRangeQuery

use of org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery in project OpenSearch by opensearch-project.

the class NumberFieldTypeTests method doTestIndexSortRangeQueries.

public void doTestIndexSortRangeQueries(NumberType type, Supplier<Number> valueSupplier) throws IOException {
    // Create index settings with an index sort.
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put("index.sort.field", "field").build();
    IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
    IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
    // Create an index writer configured with the same index sort.
    NumberFieldType fieldType = new NumberFieldType("field", type);
    IndexNumericFieldData fielddata = (IndexNumericFieldData) fieldType.fielddataBuilder("index", () -> {
        throw new UnsupportedOperationException();
    }).build(null, null);
    SortField sortField = fielddata.sortField(null, MultiValueMode.MIN, null, randomBoolean());
    IndexWriterConfig writerConfig = new IndexWriterConfig();
    writerConfig.setIndexSort(new Sort(sortField));
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, writerConfig);
    final int numDocs = TestUtil.nextInt(random(), 100, 500);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(type.createFields("field", valueSupplier.get(), true, true, false));
    }
    // Ensure that the optimized index sort query gives the same results as a points query.
    DirectoryReader reader = DirectoryReader.open(w);
    IndexSearcher searcher = newSearcher(reader);
    QueryShardContext context = new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> 0L, null, null, () -> true, null);
    final int iters = 10;
    for (int iter = 0; iter < iters; ++iter) {
        Query query = type.rangeQuery("field", random().nextBoolean() ? null : valueSupplier.get(), random().nextBoolean() ? null : valueSupplier.get(), randomBoolean(), randomBoolean(), true, context);
        assertThat(query, instanceOf(IndexSortSortedNumericDocValuesRangeQuery.class));
        Query fallbackQuery = ((IndexSortSortedNumericDocValuesRangeQuery) query).getFallbackQuery();
        assertThat(fallbackQuery, instanceOf(IndexOrDocValuesQuery.class));
        IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) fallbackQuery;
        assertEquals(searcher.count(query), searcher.count(indexOrDvQuery.getIndexQuery()));
    }
    reader.close();
    w.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) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IndexSettings(org.opensearch.index.IndexSettings) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortField(org.apache.lucene.search.SortField) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) IndexWriter(org.apache.lucene.index.IndexWriter) Sort(org.apache.lucene.search.Sort) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Aggregations

IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)2 IndexSortSortedNumericDocValuesRangeQuery (org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)2 Query (org.apache.lucene.search.Query)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 QueryShardContext (org.opensearch.index.query.QueryShardContext)2 DoublePoint (org.apache.lucene.document.DoublePoint)1 FloatPoint (org.apache.lucene.document.FloatPoint)1 HalfFloatPoint (org.apache.lucene.document.HalfFloatPoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Sort (org.apache.lucene.search.Sort)1 SortField (org.apache.lucene.search.SortField)1 Directory (org.apache.lucene.store.Directory)1