use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class BucketScriptAggregatorTests method testScript.
public void testScript() throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number_field", NumberFieldMapper.NumberType.INTEGER);
MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("the_field");
FiltersAggregationBuilder filters = new FiltersAggregationBuilder("placeholder", new MatchAllQueryBuilder()).subAggregation(new TermsAggregationBuilder("the_terms").userValueTypeHint(ValueType.STRING).field("the_field").subAggregation(new AvgAggregationBuilder("the_avg").field("number_field"))).subAggregation(new BucketScriptPipelineAggregationBuilder("bucket_script", Collections.singletonMap("the_avg", "the_terms['test1']>the_avg.value"), new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap())));
testCase(filters, new MatchAllDocsQuery(), iw -> {
Document doc = new Document();
doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test1")));
doc.add(new SortedNumericDocValuesField("number_field", 19));
iw.addDocument(doc);
doc = new Document();
doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test2")));
doc.add(new SortedNumericDocValuesField("number_field", 55));
iw.addDocument(doc);
}, f -> {
assertThat(((InternalSimpleValue) (f.getBuckets().get(0).getAggregations().get("bucket_script"))).value, equalTo(19.0));
}, fieldType, fieldType1);
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class StatsAggregatorTests method randomLongsTestCase.
private void randomLongsTestCase(int valuesPerField, StatsAggregationBuilder builder, BiConsumer<SimpleStatsAggregator, InternalStats> verify) throws IOException {
final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG);
final int numDocs = randomIntBetween(10, 50);
final List<Set<IndexableField>> docs = new ArrayList<>(numDocs);
final SimpleStatsAggregator expected = new SimpleStatsAggregator();
for (int iDoc = 0; iDoc < numDocs; iDoc++) {
List<Long> values = randomList(valuesPerField, valuesPerField, () -> randomLongBetween(-100, 100));
docs.add(values.stream().map(value -> new SortedNumericDocValuesField(ft.name(), value)).collect(toSet()));
values.forEach(expected::add);
}
testCase(builder, iw -> iw.addDocuments(docs), stats -> verify.accept(expected, stats), singleton(ft));
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class StatsAggregatorTests method testEmpty.
// TODO: Script tests, should fail with defaultValuesSourceType disabled.
public void testEmpty() throws IOException {
final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG);
testCase(stats("_name").field(ft.name()), iw -> {
}, stats -> {
assertEquals(0d, stats.getCount(), 0);
assertEquals(0d, stats.getSum(), 0);
assertEquals(Float.NaN, stats.getAvg(), 0);
assertEquals(Double.POSITIVE_INFINITY, stats.getMin(), 0);
assertEquals(Double.NEGATIVE_INFINITY, stats.getMax(), 0);
assertFalse(AggregationInspectionHelper.hasValue(stats));
}, singleton(ft));
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class StatsAggregatorTests method testCase.
private void testCase(StatsAggregationBuilder builder, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalStats> verify, Collection<MappedFieldType> fieldTypes) throws IOException {
try (Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
buildIndex.accept(indexWriter);
try (IndexReader reader = indexWriter.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
final MappedFieldType[] fieldTypesArray = fieldTypes.toArray(new MappedFieldType[0]);
final InternalStats stats = searchAndReduce(searcher, new MatchAllDocsQuery(), builder, fieldTypesArray);
verify.accept(stats);
}
}
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class SumAggregatorTests method testPartiallyUnmapped.
public void testPartiallyUnmapped() throws IOException {
final MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberType.LONG);
final SumAggregationBuilder builder = sum("_name").field(fieldType.name());
final int numDocs = randomIntBetween(10, 100);
final List<Set<IndexableField>> docs = new ArrayList<>(numDocs);
int sum = 0;
for (int i = 0; i < numDocs; i++) {
final long value = randomLongBetween(0, 1000);
sum += (int) value;
docs.add(singleton(new NumericDocValuesField(fieldType.name(), value)));
}
try (Directory mappedDirectory = newDirectory();
Directory unmappedDirectory = newDirectory()) {
try (RandomIndexWriter mappedWriter = new RandomIndexWriter(random(), mappedDirectory)) {
mappedWriter.addDocuments(docs);
}
new RandomIndexWriter(random(), unmappedDirectory).close();
try (IndexReader mappedReader = DirectoryReader.open(mappedDirectory);
IndexReader unmappedReader = DirectoryReader.open(unmappedDirectory);
MultiReader multiReader = new MultiReader(mappedReader, unmappedReader)) {
final IndexSearcher searcher = newSearcher(multiReader, true, true);
final InternalSum internalSum = searchAndReduce(searcher, new MatchAllDocsQuery(), builder, fieldType);
assertEquals(sum, internalSum.getValue(), 0d);
assertTrue(AggregationInspectionHelper.hasValue(internalSum));
}
}
}
Aggregations