use of org.apache.lucene.sandbox.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();
}
use of org.apache.lucene.sandbox.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));
}
Aggregations