Search in sources :

Example 1 with LeafDocLookup

use of org.opensearch.search.lookup.LeafDocLookup in project OpenSearch by opensearch-project.

the class AvgAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    Function<Map<String, Object>, Integer> getInc = vars -> {
        if (vars == null || vars.containsKey("inc") == false) {
            return 0;
        } else {
            return ((Number) vars.get("inc")).intValue();
        }
    };
    BiFunction<Map<String, Object>, String, Object> sum = (vars, fieldname) -> {
        int inc = getInc.apply(vars);
        LeafDocLookup docLookup = (LeafDocLookup) vars.get("doc");
        List<Long> values = new ArrayList<>();
        for (Object v : docLookup.get(fieldname)) {
            values.add(((Number) v).longValue() + inc);
        }
        return values;
    };
    scripts.put(SUM_FIELD_PARAMS_SCRIPT, vars -> {
        String fieldname = (String) vars.get("field");
        return sum.apply(vars, fieldname);
    });
    scripts.put(SUM_VALUES_FIELD_SCRIPT, vars -> sum.apply(vars, "values"));
    scripts.put(VALUE_FIELD_SCRIPT, vars -> sum.apply(vars, "value"));
    scripts.put(VALUE_SCRIPT, vars -> {
        int inc = getInc.apply(vars);
        return ((Number) vars.get("_value")).doubleValue() + inc;
    });
    Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
    nonDeterministicScripts.put(RANDOM_SCRIPT, vars -> AvgAggregatorTests.randomDouble());
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, nonDeterministicScripts, Collections.emptyMap());
    Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericUtils(org.apache.lucene.util.NumericUtils) ScriptType(org.opensearch.script.ScriptType) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) ScriptService(org.opensearch.script.ScriptService) ValueType(org.opensearch.search.aggregations.support.ValueType) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) List(java.util.List) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) IntPoint(org.apache.lucene.document.IntPoint) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Consumer(java.util.function.Consumer) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Collections(java.util.Collections) HashMap(java.util.HashMap) IntPoint(org.apache.lucene.document.IntPoint) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with LeafDocLookup

use of org.opensearch.search.lookup.LeafDocLookup in project OpenSearch by opensearch-project.

the class StatsAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    final Map<String, Function<Map<String, Object>, Object>> scripts = org.opensearch.common.collect.Map.of(VALUE_SCRIPT_NAME, vars -> ((Number) vars.get("_value")).doubleValue() + 1, FIELD_SCRIPT_NAME, vars -> {
        final String fieldName = (String) vars.get("field");
        final LeafDocLookup lookup = (LeafDocLookup) vars.get("doc");
        return lookup.get(fieldName).stream().map(value -> ((Number) value).longValue() + 1).collect(toList());
    });
    final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap());
    final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) IndexableField(org.apache.lucene.index.IndexableField) ScriptEngine(org.opensearch.script.ScriptEngine) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) CheckedConsumer(org.opensearch.common.CheckedConsumer) NumericUtils(org.apache.lucene.util.NumericUtils) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ScriptType(org.opensearch.script.ScriptType) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Collections.singletonMap(java.util.Collections.singletonMap) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) MockScriptEngine(org.opensearch.script.MockScriptEngine) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) AggregationBuilders.stats(org.opensearch.search.aggregations.AggregationBuilders.stats) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) Collectors.toSet(java.util.stream.Collectors.toSet) ScriptService(org.opensearch.script.ScriptService) Collections.emptyMap(java.util.Collections.emptyMap) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Collections.emptySet(java.util.Collections.emptySet) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Script(org.opensearch.script.Script) Collection(java.util.Collection) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) ScriptService(org.opensearch.script.ScriptService) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 3 with LeafDocLookup

use of org.opensearch.search.lookup.LeafDocLookup in project OpenSearch by opensearch-project.

the class MaxAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    Function<Map<String, Object>, Integer> getInc = vars -> {
        if (vars == null || vars.containsKey("inc") == false) {
            return 0;
        } else {
            return ((Number) vars.get("inc")).intValue();
        }
    };
    BiFunction<Map<String, Object>, String, Object> sum = (vars, fieldname) -> {
        int inc = getInc.apply(vars);
        LeafDocLookup docLookup = (LeafDocLookup) vars.get("doc");
        List<Long> values = new ArrayList<>();
        for (Object v : docLookup.get(fieldname)) {
            values.add(((Number) v).longValue() + inc);
        }
        return values;
    };
    scripts.put(SCRIPT_NAME, script -> SCRIPT_VALUE);
    scripts.put(SUM_FIELD_PARAMS_SCRIPT, vars -> {
        String fieldname = (String) vars.get("field");
        return sum.apply(vars, fieldname);
    });
    scripts.put(SUM_VALUES_FIELD_SCRIPT, vars -> sum.apply(vars, "values"));
    scripts.put(VALUE_FIELD_SCRIPT, vars -> sum.apply(vars, "value"));
    scripts.put(VALUE_SCRIPT, vars -> {
        int inc = getInc.apply(vars);
        return ((Number) vars.get("_value")).doubleValue() + inc;
    });
    Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
    nonDeterministicScripts.put(RANDOM_SCRIPT, vars -> MaxAggregatorTests.randomDouble());
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, nonDeterministicScripts, Collections.emptyMap());
    Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) MultiBucketCollector(org.opensearch.search.aggregations.MultiBucketCollector) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) ScriptType(org.opensearch.script.ScriptType) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) BucketCollector(org.opensearch.search.aggregations.BucketCollector) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FutureArrays(org.apache.lucene.util.FutureArrays) MockScriptEngine(org.opensearch.script.MockScriptEngine) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) ScriptService(org.opensearch.script.ScriptService) ValueType(org.opensearch.search.aggregations.support.ValueType) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Collections.emptyList(java.util.Collections.emptyList) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Global(org.opensearch.search.aggregations.bucket.global.Global) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Tuple(org.opensearch.common.collect.Tuple) PointValues(org.apache.lucene.index.PointValues) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) LongPoint(org.apache.lucene.document.LongPoint) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) IntPoint(org.apache.lucene.document.IntPoint) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) Bits(org.apache.lucene.util.Bits) FloatPoint(org.apache.lucene.document.FloatPoint) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Consumer(java.util.function.Consumer) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) Field(org.apache.lucene.document.Field) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) Aggregator(org.opensearch.search.aggregations.Aggregator) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with LeafDocLookup

use of org.opensearch.search.lookup.LeafDocLookup in project OpenSearch by opensearch-project.

the class MinAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    Function<Map<String, Object>, Integer> getInc = vars -> {
        if (vars == null || vars.containsKey("inc") == false) {
            return 0;
        } else {
            return ((Number) vars.get("inc")).intValue();
        }
    };
    BiFunction<Map<String, Object>, String, Object> sum = (vars, fieldname) -> {
        int inc = getInc.apply(vars);
        LeafDocLookup docLookup = (LeafDocLookup) vars.get("doc");
        List<Long> values = new ArrayList<>();
        for (Object v : docLookup.get(fieldname)) {
            values.add(((Number) v).longValue() + inc);
        }
        return values;
    };
    scripts.put(SCRIPT_NAME, script -> SCRIPT_VALUE);
    scripts.put(SUM_FIELD_PARAMS_SCRIPT, vars -> {
        String fieldname = (String) vars.get("field");
        return sum.apply(vars, fieldname);
    });
    scripts.put(SUM_VALUES_FIELD_SCRIPT, vars -> sum.apply(vars, "values"));
    scripts.put(VALUE_FIELD_SCRIPT, vars -> sum.apply(vars, "value"));
    scripts.put(VALUE_SCRIPT, vars -> {
        int inc = getInc.apply(vars);
        return ((Number) vars.get("_value")).doubleValue() + inc;
    });
    scripts.put(INVERT_SCRIPT, vars -> -((Number) vars.get("_value")).doubleValue());
    Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
    nonDeterministicScripts.put(RANDOM_SCRIPT, vars -> AggregatorTestCase.randomDouble());
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, nonDeterministicScripts, Collections.emptyMap());
    Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) InternalHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalHistogram) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) ScriptType(org.opensearch.script.ScriptType) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MapperService(org.opensearch.index.mapper.MapperService) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MockScriptEngine(org.opensearch.script.MockScriptEngine) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) ScriptService(org.opensearch.script.ScriptService) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IpFieldMapper(org.opensearch.index.mapper.IpFieldMapper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Tuple(org.opensearch.common.collect.Tuple) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) BigArrays(org.opensearch.common.util.BigArrays) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) LongPoint(org.apache.lucene.document.LongPoint) HistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) IntPoint(org.apache.lucene.document.IntPoint) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) FloatPoint(org.apache.lucene.document.FloatPoint) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) InternalGlobal(org.opensearch.search.aggregations.bucket.global.InternalGlobal) Consumer(java.util.function.Consumer) Field(org.apache.lucene.document.Field) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) DoublePoint(org.apache.lucene.document.DoublePoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with LeafDocLookup

use of org.opensearch.search.lookup.LeafDocLookup in project OpenSearch by opensearch-project.

the class SumAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    final Map<String, Function<Map<String, Object>, Object>> scripts = org.opensearch.common.collect.Map.of(VALUE_SCRIPT_NAME, vars -> ((Number) vars.get("_value")).doubleValue() + 1, FIELD_SCRIPT_NAME, vars -> {
        final String fieldName = (String) vars.get("field");
        final LeafDocLookup lookup = (LeafDocLookup) vars.get("doc");
        return lookup.get(fieldName).stream().map(value -> ((Number) value).longValue() + 1).collect(toList());
    });
    final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap());
    final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) AggregationBuilders.sum(org.opensearch.search.aggregations.AggregationBuilders.sum) ScriptModule(org.opensearch.script.ScriptModule) IndexableField(org.apache.lucene.index.IndexableField) ScriptEngine(org.opensearch.script.ScriptEngine) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) NumericUtils(org.apache.lucene.util.NumericUtils) ScriptType(org.opensearch.script.ScriptType) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) ScriptService(org.opensearch.script.ScriptService) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) List(java.util.List) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) TriConsumer(org.opensearch.common.TriConsumer) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Collections.singletonMap(java.util.Collections.singletonMap) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) Collections.emptyMap(java.util.Collections.emptyMap) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) Collectors.toList(java.util.stream.Collectors.toList) Field(org.apache.lucene.document.Field) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) ScriptService(org.opensearch.script.ScriptService) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Aggregations

LeafDocLookup (org.opensearch.search.lookup.LeafDocLookup)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Function (java.util.function.Function)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 Directory (org.apache.lucene.store.Directory)6 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)6 Arrays (java.util.Arrays)5 Collections.singleton (java.util.Collections.singleton)5 List (java.util.List)5 Map (java.util.Map)5 Consumer (java.util.function.Consumer)5 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)5 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)5 IndexReader (org.apache.lucene.index.IndexReader)5 MultiReader (org.apache.lucene.index.MultiReader)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 CheckedConsumer (org.opensearch.common.CheckedConsumer)5 Settings (org.opensearch.common.settings.Settings)5