Search in sources :

Example 6 with SearchScript

use of org.elasticsearch.script.SearchScript in project elasticsearch by elastic.

the class ScriptedMetricAggregationBuilder method doBuild.

@Override
protected ScriptedMetricAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder) throws IOException {
    QueryShardContext queryShardContext = context.getQueryShardContext();
    Function<Map<String, Object>, ExecutableScript> executableInitScript;
    if (initScript != null) {
        executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.Standard.AGGS);
    } else {
        executableInitScript = (p) -> null;
    }
    Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript, ScriptContext.Standard.AGGS);
    Function<Map<String, Object>, ExecutableScript> executableCombineScript;
    if (combineScript != null) {
        executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.Standard.AGGS);
    } else {
        executableCombineScript = (p) -> null;
    }
    return new ScriptedMetricAggregatorFactory(name, searchMapScript, executableInitScript, executableCombineScript, reduceScript, params, context, parent, subfactoriesBuilder, metaData);
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Map(java.util.Map)

Example 7 with SearchScript

use of org.elasticsearch.script.SearchScript in project elasticsearch by elastic.

the class ScriptedMetricAggregatorFactory method createInternal.

@Override
public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
    if (collectsFromSingleBucket == false) {
        return asMultiBucketAggregator(this, context, parent);
    }
    Map<String, Object> params = this.params;
    if (params != null) {
        params = deepCopyParams(params, context);
    } else {
        params = new HashMap<>();
        params.put("_agg", new HashMap<String, Object>());
    }
    final ExecutableScript initScript = this.initScript.apply(params);
    final SearchScript mapScript = this.mapScript.apply(params);
    final ExecutableScript combineScript = this.combineScript.apply(params);
    final Script reduceScript = deepCopyScript(this.reduceScript, context);
    if (initScript != null) {
        initScript.run();
    }
    return new ScriptedMetricAggregator(name, mapScript, combineScript, reduceScript, params, context, parent, pipelineAggregators, metaData);
}
Also used : Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchScript(org.elasticsearch.script.SearchScript) SearchScript(org.elasticsearch.script.SearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 8 with SearchScript

use of org.elasticsearch.script.SearchScript in project elasticsearch by elastic.

the class ScriptScoreFunctionTests method testScriptScoresReturnsNaN.

/**
     * Tests https://github.com/elastic/elasticsearch/issues/2426
     */
public void testScriptScoresReturnsNaN() throws IOException {
    // script that always returns NaN
    ScoreFunction scoreFunction = new ScriptScoreFunction(new Script("Double.NaN"), new SearchScript() {

        @Override
        public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
            return new AbstractDoubleSearchScript() {

                @Override
                public double runAsDouble() {
                    return Double.NaN;
                }

                @Override
                public void setDocument(int doc) {
                // do nothing: we are a fake with no lookup
                }
            };
        }

        @Override
        public boolean needsScores() {
            return false;
        }
    });
    LeafScoreFunction leafScoreFunction = scoreFunction.getLeafScoreFunction(null);
    GeneralScriptException expected = expectThrows(GeneralScriptException.class, () -> {
        leafScoreFunction.score(randomInt(), randomFloat());
    });
    assertTrue(expected.getMessage().contains("returned NaN"));
}
Also used : Script(org.elasticsearch.script.Script) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) IOException(java.io.IOException)

Example 9 with SearchScript

use of org.elasticsearch.script.SearchScript in project elasticsearch by elastic.

the class SearchService method parseSource.

private void parseSource(DefaultSearchContext context, SearchSourceBuilder source) throws SearchContextException {
    // nothing to parse...
    if (source == null) {
        return;
    }
    QueryShardContext queryShardContext = context.getQueryShardContext();
    context.from(source.from());
    context.size(source.size());
    Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>();
    if (source.query() != null) {
        InnerHitBuilder.extractInnerHits(source.query(), innerHitBuilders);
        context.parsedQuery(queryShardContext.toQuery(source.query()));
    }
    if (source.postFilter() != null) {
        InnerHitBuilder.extractInnerHits(source.postFilter(), innerHitBuilders);
        context.parsedPostFilter(queryShardContext.toQuery(source.postFilter()));
    }
    if (innerHitBuilders.size() > 0) {
        for (Map.Entry<String, InnerHitBuilder> entry : innerHitBuilders.entrySet()) {
            try {
                entry.getValue().build(context, context.innerHits());
            } catch (IOException e) {
                throw new SearchContextException(context, "failed to build inner_hits", e);
            }
        }
    }
    if (source.sorts() != null) {
        try {
            Optional<SortAndFormats> optionalSort = SortBuilder.buildSort(source.sorts(), context.getQueryShardContext());
            if (optionalSort.isPresent()) {
                context.sort(optionalSort.get());
            }
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create sort elements", e);
        }
    }
    context.trackScores(source.trackScores());
    if (source.minScore() != null) {
        context.minimumScore(source.minScore());
    }
    if (source.profile()) {
        context.setProfilers(new Profilers(context.searcher()));
    }
    if (source.timeout() != null) {
        context.timeout(source.timeout());
    }
    context.terminateAfter(source.terminateAfter());
    if (source.aggregations() != null) {
        try {
            AggregatorFactories factories = source.aggregations().build(context, null);
            factories.validate();
            context.aggregations(new SearchContextAggregations(factories));
        } catch (IOException e) {
            throw new AggregationInitializationException("Failed to create aggregators", e);
        }
    }
    if (source.suggest() != null) {
        try {
            context.suggest(source.suggest().build(queryShardContext));
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create SuggestionSearchContext", e);
        }
    }
    if (source.rescores() != null) {
        try {
            for (RescoreBuilder<?> rescore : source.rescores()) {
                context.addRescore(rescore.build(queryShardContext));
            }
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create RescoreSearchContext", e);
        }
    }
    if (source.explain() != null) {
        context.explain(source.explain());
    }
    if (source.fetchSource() != null) {
        context.fetchSourceContext(source.fetchSource());
    }
    if (source.docValueFields() != null) {
        context.docValueFieldsContext(new DocValueFieldsContext(source.docValueFields()));
    }
    if (source.highlighter() != null) {
        HighlightBuilder highlightBuilder = source.highlighter();
        try {
            context.highlight(highlightBuilder.build(queryShardContext));
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create SearchContextHighlighter", e);
        }
    }
    if (source.scriptFields() != null) {
        for (org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField field : source.scriptFields()) {
            SearchScript searchScript = scriptService.search(context.lookup(), field.script(), ScriptContext.Standard.SEARCH);
            context.scriptFields().add(new ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    if (source.ext() != null) {
        for (SearchExtBuilder searchExtBuilder : source.ext()) {
            context.addSearchExt(searchExtBuilder);
        }
    }
    if (source.version() != null) {
        context.version(source.version());
    }
    if (source.stats() != null) {
        context.groupStats(source.stats());
    }
    if (source.searchAfter() != null && source.searchAfter().length > 0) {
        if (context.scrollContext() != null) {
            throw new SearchContextException(context, "`search_after` cannot be used in a scroll context.");
        }
        if (context.from() > 0) {
            throw new SearchContextException(context, "`from` parameter must be set to 0 when `search_after` is used.");
        }
        FieldDoc fieldDoc = SearchAfterBuilder.buildFieldDoc(context.sort(), source.searchAfter());
        context.searchAfter(fieldDoc);
    }
    if (source.slice() != null) {
        if (context.scrollContext() == null) {
            throw new SearchContextException(context, "`slice` cannot be used outside of a scroll context");
        }
        context.sliceBuilder(source.slice());
    }
    if (source.storedFields() != null) {
        if (source.storedFields().fetchFields() == false) {
            if (context.version()) {
                throw new SearchContextException(context, "`stored_fields` cannot be disabled if version is requested");
            }
            if (context.sourceRequested()) {
                throw new SearchContextException(context, "`stored_fields` cannot be disabled if _source is requested");
            }
        }
        context.storedFieldsContext(source.storedFields());
    }
    if (source.collapse() != null) {
        final CollapseContext collapseContext = source.collapse().build(context);
        context.collapse(collapseContext);
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) Profilers(org.elasticsearch.search.profile.Profilers) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScriptField(org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) AggregatorFactories(org.elasticsearch.search.aggregations.AggregatorFactories) DocValueFieldsContext(org.elasticsearch.search.fetch.subphase.DocValueFieldsContext) SearchContextAggregations(org.elasticsearch.search.aggregations.SearchContextAggregations) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats) SearchScript(org.elasticsearch.script.SearchScript) Map(java.util.Map) HashMap(java.util.HashMap) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) CollapseContext(org.elasticsearch.search.collapse.CollapseContext)

Aggregations

SearchScript (org.elasticsearch.script.SearchScript)9 IOException (java.io.IOException)4 Map (java.util.Map)3 SortAndFormats (org.elasticsearch.search.sort.SortAndFormats)3 HashMap (java.util.HashMap)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 CompiledScript (org.elasticsearch.script.CompiledScript)2 ExecutableScript (org.elasticsearch.script.ExecutableScript)2 Script (org.elasticsearch.script.Script)2 ScriptField (org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField)2 SearchLookup (org.elasticsearch.search.lookup.SearchLookup)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Function (java.util.function.Function)1 LongSupplier (java.util.function.LongSupplier)1 Analyzer (org.apache.lucene.analysis.Analyzer)1