Search in sources :

Example 96 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class AbstractSortTestCase method testBuildSortField.

/**
 * test that build() outputs a {@link SortField} that is similar to the one
 * we would get when parsing the xContent the sort builder is rendering out
 */
public void testBuildSortField() throws IOException {
    QueryShardContext mockShardContext = createMockShardContext();
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        T sortBuilder = createTestItem();
        SortFieldAndFormat sortField = Rewriteable.rewrite(sortBuilder, mockShardContext).build(mockShardContext);
        sortFieldAssertions(sortBuilder, sortField.field, sortField.format);
    }
}
Also used : QueryShardContext(org.opensearch.index.query.QueryShardContext)

Example 97 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testGetMaxNumericSortValue.

public void testGetMaxNumericSortValue() throws IOException {
    QueryShardContext context = createMockShardContext();
    for (NumberFieldMapper.NumberType numberType : NumberFieldMapper.NumberType.values()) {
        String fieldName = "custom-" + numberType.numericType();
        assertNull(getMinMaxOrNull(context, SortBuilders.fieldSort(fieldName)));
        assertNull(getMinMaxOrNull(context, SortBuilders.fieldSort(fieldName + "-ni")));
        try (Directory dir = newDirectory()) {
            int numDocs = randomIntBetween(10, 30);
            final Comparable[] values = new Comparable[numDocs];
            try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
                for (int i = 0; i < numDocs; i++) {
                    Document doc = new Document();
                    switch(numberType) {
                        case LONG:
                            long v1 = randomLong();
                            values[i] = v1;
                            doc.add(new LongPoint(fieldName, v1));
                            break;
                        case INTEGER:
                            int v2 = randomInt();
                            values[i] = (long) v2;
                            doc.add(new IntPoint(fieldName, v2));
                            break;
                        case DOUBLE:
                            double v3 = randomDouble();
                            values[i] = v3;
                            doc.add(new DoublePoint(fieldName, v3));
                            break;
                        case FLOAT:
                            float v4 = randomFloat();
                            values[i] = v4;
                            doc.add(new FloatPoint(fieldName, v4));
                            break;
                        case HALF_FLOAT:
                            float v5 = randomFloat();
                            values[i] = (double) v5;
                            doc.add(new HalfFloatPoint(fieldName, v5));
                            break;
                        case BYTE:
                            byte v6 = randomByte();
                            values[i] = (long) v6;
                            doc.add(new IntPoint(fieldName, v6));
                            break;
                        case SHORT:
                            short v7 = randomShort();
                            values[i] = (long) v7;
                            doc.add(new IntPoint(fieldName, v7));
                            break;
                        default:
                            throw new AssertionError("unknown type " + numberType);
                    }
                    writer.addDocument(doc);
                }
                Arrays.sort(values);
                try (DirectoryReader reader = writer.getReader()) {
                    QueryShardContext newContext = createMockShardContext(new AssertingIndexSearcher(random(), reader));
                    if (numberType == NumberFieldMapper.NumberType.HALF_FLOAT) {
                        assertNull(getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName + "-ni")));
                        assertNull(getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)));
                    } else {
                        assertNull(getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName + "-ni")));
                        assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).getMax());
                        assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).getMin());
                    }
                }
            }
        }
    }
}
Also used : NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) 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) AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) QueryShardContext(org.opensearch.index.query.QueryShardContext) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 98 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testGetMaxKeywordValue.

public void testGetMaxKeywordValue() throws IOException {
    QueryShardContext context = createMockShardContext();
    String fieldName = "custom-keyword";
    assertNull(getMinMaxOrNull(context, SortBuilders.fieldSort(fieldName)));
    assertNull(getMinMaxOrNull(context, SortBuilders.fieldSort(fieldName + "-ni")));
    try (Directory dir = newDirectory()) {
        int numDocs = randomIntBetween(10, 30);
        final BytesRef[] values = new BytesRef[numDocs];
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new KeywordAnalyzer())) {
            for (int i = 0; i < numDocs; i++) {
                Document doc = new Document();
                values[i] = new BytesRef(randomAlphaOfLengthBetween(5, 10));
                doc.add(new TextField(fieldName, values[i].utf8ToString(), Field.Store.NO));
                writer.addDocument(doc);
            }
            Arrays.sort(values);
            try (DirectoryReader reader = writer.getReader()) {
                QueryShardContext newContext = createMockShardContext(new AssertingIndexSearcher(random(), reader));
                assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).getMax());
                assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).getMin());
            }
        }
    }
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) DirectoryReader(org.apache.lucene.index.DirectoryReader) QueryShardContext(org.opensearch.index.query.QueryShardContext) TextField(org.apache.lucene.document.TextField) 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) BytesRef(org.apache.lucene.util.BytesRef) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher) Directory(org.apache.lucene.store.Directory)

Example 99 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testModeNonNumericField.

/**
 * Test that MIN, MAX mode work on non-numeric fields, but other modes throw exception
 */
public void testModeNonNumericField() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    FieldSortBuilder sortBuilder = new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MIN);
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedSetSortField.class));
    assertEquals(SortedSetSelector.Type.MIN, ((SortedSetSortField) sortField).getSelector());
    sortBuilder = new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MAX);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedSetSortField.class));
    assertEquals(SortedSetSelector.Type.MAX, ((SortedSetSortField) sortField).getSelector());
    String expectedError = "we only support AVG, MEDIAN and SUM on number based fields";
    QueryShardException e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.AVG).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
    e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.SUM).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
    e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MEDIAN).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) QueryShardContext(org.opensearch.index.query.QueryShardContext) QueryShardException(org.opensearch.index.query.QueryShardException) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 100 with QueryShardContext

use of org.opensearch.index.query.QueryShardContext 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

QueryShardContext (org.opensearch.index.query.QueryShardContext)131 Query (org.apache.lucene.search.Query)46 QueryBuilder (org.opensearch.index.query.QueryBuilder)29 TermQuery (org.apache.lucene.search.TermQuery)27 IndexSettings (org.opensearch.index.IndexSettings)25 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)23 Settings (org.opensearch.common.settings.Settings)22 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 IndexService (org.opensearch.index.IndexService)20 Term (org.apache.lucene.index.Term)17 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 Directory (org.apache.lucene.store.Directory)17 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)17 Engine (org.opensearch.index.engine.Engine)17 BytesRef (org.apache.lucene.util.BytesRef)16 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)16 SortField (org.apache.lucene.search.SortField)15 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 IndexReader (org.apache.lucene.index.IndexReader)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13