Search in sources :

Example 1 with SearchSortValuesAndFormats

use of org.opensearch.search.SearchSortValuesAndFormats in project OpenSearch by opensearch-project.

the class BottomSortValuesCollector method consumeTopDocs.

synchronized void consumeTopDocs(TopFieldDocs topDocs, DocValueFormat[] sortValuesFormat) {
    totalHits += topDocs.totalHits.value;
    if (validateShardSortFields(topDocs.fields) == false) {
        return;
    }
    FieldDoc shardBottomDoc = extractBottom(topDocs);
    if (shardBottomDoc == null) {
        return;
    }
    if (bottomSortValues == null || compareValues(shardBottomDoc.fields, bottomSortValues.getRawSortValues()) < 0) {
        bottomSortValues = new SearchSortValuesAndFormats(shardBottomDoc.fields, sortValuesFormat);
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) SearchSortValuesAndFormats(org.opensearch.search.SearchSortValuesAndFormats)

Example 2 with SearchSortValuesAndFormats

use of org.opensearch.search.SearchSortValuesAndFormats in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testIsBottomSortShardDisjoint.

public void testIsBottomSortShardDisjoint() throws Exception {
    try (Directory dir = newDirectory()) {
        int numDocs = randomIntBetween(5, 10);
        long maxValue = -1;
        long minValue = Integer.MAX_VALUE;
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new KeywordAnalyzer())) {
            FieldSortBuilder fieldSort = SortBuilders.fieldSort("custom-date");
            try (DirectoryReader reader = writer.getReader()) {
                QueryShardContext context = createMockShardContext(new IndexSearcher(reader));
                assertTrue(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { 0L }, new DocValueFormat[] { DocValueFormat.RAW })));
            }
            for (int i = 0; i < numDocs; i++) {
                Document doc = new Document();
                long value = randomLongBetween(1, Integer.MAX_VALUE);
                doc.add(new LongPoint("custom-date", value));
                doc.add(new SortedNumericDocValuesField("custom-date", value));
                writer.addDocument(doc);
                maxValue = Math.max(maxValue, value);
                minValue = Math.min(minValue, value);
            }
            try (DirectoryReader reader = writer.getReader()) {
                QueryShardContext context = createMockShardContext(new IndexSearcher(reader));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, null));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { minValue }, new DocValueFormat[] { DocValueFormat.RAW })));
                assertTrue(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { minValue - 1 }, new DocValueFormat[] { DocValueFormat.RAW })));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { minValue + 1 }, new DocValueFormat[] { DocValueFormat.RAW })));
                fieldSort.order(SortOrder.DESC);
                assertTrue(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { maxValue + 1 }, new DocValueFormat[] { DocValueFormat.RAW })));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { maxValue }, new DocValueFormat[] { DocValueFormat.RAW })));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { minValue }, new DocValueFormat[] { DocValueFormat.RAW })));
                fieldSort.setNestedSort(new NestedSortBuilder("empty"));
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { minValue - 1 }, new DocValueFormat[] { DocValueFormat.RAW })));
                fieldSort.setNestedSort(null);
                fieldSort.missing("100");
                assertFalse(fieldSort.isBottomSortShardDisjoint(context, new SearchSortValuesAndFormats(new Object[] { maxValue + 1 }, new DocValueFormat[] { DocValueFormat.RAW })));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SearchSortValuesAndFormats(org.opensearch.search.SearchSortValuesAndFormats) QueryShardContext(org.opensearch.index.query.QueryShardContext) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

SearchSortValuesAndFormats (org.opensearch.search.SearchSortValuesAndFormats)2 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)1 Document (org.apache.lucene.document.Document)1 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 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 AssertingIndexSearcher (org.apache.lucene.search.AssertingIndexSearcher)1 FieldDoc (org.apache.lucene.search.FieldDoc)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Directory (org.apache.lucene.store.Directory)1 QueryShardContext (org.opensearch.index.query.QueryShardContext)1