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);
}
}
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());
}
}
}
}
}
}
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());
}
}
}
}
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());
}
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 })));
}
}
}
}
Aggregations