Search in sources :

Example 6 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) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) 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) 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 7 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) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) 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) 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 8 with LeafDocLookup

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

the class DateScriptMocksPlugin method pluginScripts.

@Override
public Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    scripts.put(EXTRACT_FIELD, params -> {
        LeafDocLookup docLookup = (LeafDocLookup) params.get("doc");
        String fieldname = (String) params.get("fieldname");
        return docLookup.get(fieldname);
    });
    scripts.put(DOUBLE_PLUS_ONE_MONTH, params -> new DateTime(Double.valueOf((double) params.get("_value")).longValue(), DateTimeZone.UTC).plusMonths(1).getMillis());
    scripts.put(LONG_PLUS_ONE_MONTH, params -> new DateTime((long) params.get("_value"), DateTimeZone.UTC).plusMonths(1).getMillis());
    return scripts;
}
Also used : Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) HashMap(java.util.HashMap) DateTime(org.joda.time.DateTime)

Aggregations

LeafDocLookup (org.opensearch.search.lookup.LeafDocLookup)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Function (java.util.function.Function)7 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)7 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)7 List (java.util.List)6 Map (java.util.Map)6 Consumer (java.util.function.Consumer)6 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)6 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 Directory (org.apache.lucene.store.Directory)6 CheckedConsumer (org.opensearch.common.CheckedConsumer)6 Settings (org.opensearch.common.settings.Settings)6 NumberFieldMapper (org.opensearch.index.mapper.NumberFieldMapper)6 MockScriptEngine (org.opensearch.script.MockScriptEngine)6 Script (org.opensearch.script.Script)6 ScriptEngine (org.opensearch.script.ScriptEngine)6