Search in sources :

Example 16 with ExecutableScript

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

the class QueryShardContext method getLazyExecutableScript.

/**
     * Returns a lazily created {@link ExecutableScript} that is compiled immediately but can be pulled later once all
     * parameters are available.
     */
public final Function<Map<String, Object>, ExecutableScript> getLazyExecutableScript(Script script, ScriptContext context) {
    failIfFrozen();
    CompiledScript executable = scriptService.compile(script, context);
    return (p) -> scriptService.executable(executable, p);
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) Query(org.apache.lucene.search.Query) BitsetFilterCache(org.elasticsearch.index.cache.bitset.BitsetFilterCache) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) ParsingException(org.elasticsearch.common.ParsingException) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) IndexAnalyzers(org.elasticsearch.index.analysis.IndexAnalyzers) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) MapperQueryParser(org.apache.lucene.queryparser.classic.MapperQueryParser) Function(java.util.function.Function) ScriptContext(org.elasticsearch.script.ScriptContext) Strings(org.elasticsearch.common.Strings) NestedScope(org.elasticsearch.index.query.support.NestedScope) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Similarity(org.apache.lucene.search.similarities.Similarity) Map(java.util.Map) IndexSettings(org.elasticsearch.index.IndexSettings) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SearchScript(org.elasticsearch.script.SearchScript) SearchLookup(org.elasticsearch.search.lookup.SearchLookup) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper) Script(org.elasticsearch.script.Script) SetOnce(org.apache.lucene.util.SetOnce) Mapper(org.elasticsearch.index.mapper.Mapper) Analyzer(org.apache.lucene.analysis.Analyzer) Client(org.elasticsearch.client.Client) Collection(java.util.Collection) IOException(java.io.IOException) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) BytesReference(org.elasticsearch.common.bytes.BytesReference) SimilarityService(org.elasticsearch.index.similarity.SimilarityService) CheckedFunction(org.elasticsearch.common.CheckedFunction) MapperService(org.elasticsearch.index.mapper.MapperService) Version(org.elasticsearch.Version) CompiledScript(org.elasticsearch.script.CompiledScript) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) Queries(org.elasticsearch.common.lucene.search.Queries) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) ExecutableScript(org.elasticsearch.script.ExecutableScript) ContentPath(org.elasticsearch.index.mapper.ContentPath) ScriptService(org.elasticsearch.script.ScriptService) IndexReader(org.apache.lucene.index.IndexReader)

Example 17 with ExecutableScript

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

the class PhraseSuggester method innerExecute.

/*
     * More Ideas:
     *   - add ability to find whitespace problems -> we can build a poor mans decompounder with our index based on a automaton?
     *   - add ability to build different error models maybe based on a confusion matrix?
     *   - try to combine a token with its subsequent token to find / detect word splits (optional)
     *      - for this to work we need some way to defined the position length of a candidate
     *   - phonetic filters could be interesting here too for candidate selection
     */
@Override
public Suggestion<? extends Entry<? extends Option>> innerExecute(String name, PhraseSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
    double realWordErrorLikelihood = suggestion.realworldErrorLikelyhood();
    final PhraseSuggestion response = new PhraseSuggestion(name, suggestion.getSize());
    final IndexReader indexReader = searcher.getIndexReader();
    List<PhraseSuggestionContext.DirectCandidateGenerator> generators = suggestion.generators();
    final int numGenerators = generators.size();
    final List<CandidateGenerator> gens = new ArrayList<>(generators.size());
    for (int i = 0; i < numGenerators; i++) {
        PhraseSuggestionContext.DirectCandidateGenerator generator = generators.get(i);
        DirectSpellChecker directSpellChecker = generator.createDirectSpellChecker();
        Terms terms = MultiFields.getTerms(indexReader, generator.field());
        if (terms != null) {
            gens.add(new DirectCandidateGenerator(directSpellChecker, generator.field(), generator.suggestMode(), indexReader, realWordErrorLikelihood, generator.size(), generator.preFilter(), generator.postFilter(), terms));
        }
    }
    final String suggestField = suggestion.getField();
    final Terms suggestTerms = MultiFields.getTerms(indexReader, suggestField);
    if (gens.size() > 0 && suggestTerms != null) {
        final NoisyChannelSpellChecker checker = new NoisyChannelSpellChecker(realWordErrorLikelihood, suggestion.getRequireUnigram(), suggestion.getTokenLimit());
        final BytesRef separator = suggestion.separator();
        WordScorer wordScorer = suggestion.model().newScorer(indexReader, suggestTerms, suggestField, realWordErrorLikelihood, separator);
        Result checkerResult;
        try (TokenStream stream = checker.tokenStream(suggestion.getAnalyzer(), suggestion.getText(), spare, suggestion.getField())) {
            checkerResult = checker.getCorrections(stream, new MultiCandidateGeneratorWrapper(suggestion.getShardSize(), gens.toArray(new CandidateGenerator[gens.size()])), suggestion.maxErrors(), suggestion.getShardSize(), wordScorer, suggestion.confidence(), suggestion.gramSize());
        }
        PhraseSuggestion.Entry resultEntry = buildResultEntry(suggestion, spare, checkerResult.cutoffScore);
        response.addTerm(resultEntry);
        final BytesRefBuilder byteSpare = new BytesRefBuilder();
        final Function<Map<String, Object>, ExecutableScript> collateScript = suggestion.getCollateQueryScript();
        final boolean collatePrune = (collateScript != null) && suggestion.collatePrune();
        for (int i = 0; i < checkerResult.corrections.length; i++) {
            Correction correction = checkerResult.corrections[i];
            spare.copyUTF8Bytes(correction.join(SEPARATOR, byteSpare, null, null));
            boolean collateMatch = true;
            if (collateScript != null) {
                // Checks if the template query collateScript yields any documents
                // from the index for a correction, collateMatch is updated
                final Map<String, Object> vars = suggestion.getCollateScriptParams();
                vars.put(SUGGESTION_TEMPLATE_VAR_NAME, spare.toString());
                QueryShardContext shardContext = suggestion.getShardContext();
                final ExecutableScript executable = collateScript.apply(vars);
                final BytesReference querySource = (BytesReference) executable.run();
                try (XContentParser parser = XContentFactory.xContent(querySource).createParser(shardContext.getXContentRegistry(), querySource)) {
                    QueryBuilder innerQueryBuilder = shardContext.newParseContext(parser).parseInnerQueryBuilder();
                    final ParsedQuery parsedQuery = shardContext.toQuery(innerQueryBuilder);
                    collateMatch = Lucene.exists(searcher, parsedQuery.query());
                }
            }
            if (!collateMatch && !collatePrune) {
                continue;
            }
            Text phrase = new Text(spare.toString());
            Text highlighted = null;
            if (suggestion.getPreTag() != null) {
                spare.copyUTF8Bytes(correction.join(SEPARATOR, byteSpare, suggestion.getPreTag(), suggestion.getPostTag()));
                highlighted = new Text(spare.toString());
            }
            if (collatePrune) {
                resultEntry.addOption(new Suggestion.Entry.Option(phrase, highlighted, (float) (correction.score), collateMatch));
            } else {
                resultEntry.addOption(new Suggestion.Entry.Option(phrase, highlighted, (float) (correction.score)));
            }
        }
    } else {
        response.addTerm(buildResultEntry(suggestion, spare, Double.MIN_VALUE));
    }
    return response;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Result(org.elasticsearch.search.suggest.phrase.NoisyChannelSpellChecker.Result) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) ExecutableScript(org.elasticsearch.script.ExecutableScript) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker) BytesRef(org.apache.lucene.util.BytesRef) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Terms(org.apache.lucene.index.Terms) Text(org.elasticsearch.common.text.Text) IndexReader(org.apache.lucene.index.IndexReader) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 18 with ExecutableScript

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

the class PhraseSuggestionBuilder method build.

@Override
public SuggestionContext build(QueryShardContext context) throws IOException {
    PhraseSuggestionContext suggestionContext = new PhraseSuggestionContext(context);
    MapperService mapperService = context.getMapperService();
    // copy over common settings to each suggestion builder
    populateCommonFields(mapperService, suggestionContext);
    suggestionContext.setSeparator(BytesRefs.toBytesRef(this.separator));
    suggestionContext.setRealWordErrorLikelihood(this.realWordErrorLikelihood);
    suggestionContext.setConfidence(this.confidence);
    suggestionContext.setMaxErrors(this.maxErrors);
    suggestionContext.setSeparator(BytesRefs.toBytesRef(this.separator));
    suggestionContext.setRequireUnigram(this.forceUnigrams);
    suggestionContext.setTokenLimit(this.tokenLimit);
    suggestionContext.setPreTag(BytesRefs.toBytesRef(this.preTag));
    suggestionContext.setPostTag(BytesRefs.toBytesRef(this.postTag));
    if (this.gramSize != null) {
        suggestionContext.setGramSize(this.gramSize);
    }
    for (List<CandidateGenerator> candidateGenerators : this.generators.values()) {
        for (CandidateGenerator candidateGenerator : candidateGenerators) {
            suggestionContext.addGenerator(candidateGenerator.build(mapperService));
        }
    }
    if (this.model != null) {
        suggestionContext.setModel(this.model.buildWordScorerFactory());
    }
    if (this.collateQuery != null) {
        Function<Map<String, Object>, ExecutableScript> compiledScript = context.getLazyExecutableScript(this.collateQuery, ScriptContext.Standard.SEARCH);
        suggestionContext.setCollateQueryScript(compiledScript);
        if (this.collateParams != null) {
            suggestionContext.setCollateScriptParams(this.collateParams);
        }
        suggestionContext.setCollatePrune(this.collatePrune);
    }
    if (this.gramSize == null || suggestionContext.generators().isEmpty()) {
        final ShingleTokenFilterFactory.Factory shingleFilterFactory = getShingleFilterFactory(suggestionContext.getAnalyzer());
        if (this.gramSize == null) {
            // try to detect the shingle size
            if (shingleFilterFactory != null) {
                suggestionContext.setGramSize(shingleFilterFactory.getMaxShingleSize());
                if (suggestionContext.getAnalyzer() == null && shingleFilterFactory.getMinShingleSize() > 1 && !shingleFilterFactory.getOutputUnigrams()) {
                    throw new IllegalArgumentException("The default analyzer for field: [" + suggestionContext.getField() + "] doesn't emit unigrams. If this is intentional try to set the analyzer explicitly");
                }
            }
        }
        if (suggestionContext.generators().isEmpty()) {
            if (shingleFilterFactory != null && shingleFilterFactory.getMinShingleSize() > 1 && !shingleFilterFactory.getOutputUnigrams() && suggestionContext.getRequireUnigram()) {
                throw new IllegalArgumentException("The default candidate generator for phrase suggest can't operate on field: [" + suggestionContext.getField() + "] since it doesn't emit unigrams. " + "If this is intentional try to set the candidate generator field explicitly");
            }
            // use a default generator on the same field
            DirectCandidateGenerator generator = new DirectCandidateGenerator();
            generator.setField(suggestionContext.getField());
            suggestionContext.addGenerator(generator);
        }
    }
    return suggestionContext;
}
Also used : DirectCandidateGenerator(org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCandidateGenerator) ShingleTokenFilterFactory(org.elasticsearch.index.analysis.ShingleTokenFilterFactory) ExecutableScript(org.elasticsearch.script.ExecutableScript) DirectCandidateGenerator(org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCandidateGenerator) HashMap(java.util.HashMap) Map(java.util.Map) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 19 with ExecutableScript

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

the class ScriptEngineTests method testChangingVarsCrossExecution1.

public void testChangingVarsCrossExecution1() {
    Map<String, Object> vars = new HashMap<>();
    Map<String, Object> ctx = new HashMap<>();
    vars.put("ctx", ctx);
    Object compiledScript = scriptEngine.compile(null, "return ctx.value;", Collections.emptyMap());
    ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptType.INLINE, "testChangingVarsCrossExecution1", "painless", compiledScript), vars);
    ctx.put("value", 1);
    Object o = script.run();
    assertEquals(1, ((Number) o).intValue());
    ctx.put("value", 2);
    o = script.run();
    assertEquals(2, ((Number) o).intValue());
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 20 with ExecutableScript

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

the class ScriptEngineTests method testChangingVarsCrossExecution2.

public void testChangingVarsCrossExecution2() {
    Map<String, Object> vars = new HashMap<>();
    Object compiledScript = scriptEngine.compile(null, "return params['value'];", Collections.emptyMap());
    ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptType.INLINE, "testChangingVarsCrossExecution2", "painless", compiledScript), vars);
    script.setNextVar("value", 1);
    Object value = script.run();
    assertEquals(1, ((Number) value).intValue());
    script.setNextVar("value", 2);
    value = script.run();
    assertEquals(2, ((Number) value).intValue());
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Aggregations

ExecutableScript (org.elasticsearch.script.ExecutableScript)24 CompiledScript (org.elasticsearch.script.CompiledScript)15 HashMap (java.util.HashMap)9 Script (org.elasticsearch.script.Script)9 Map (java.util.Map)8 BytesReference (org.elasticsearch.common.bytes.BytesReference)7 Mustache (com.github.mustachejava.Mustache)4 ArrayList (java.util.ArrayList)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 IOException (java.io.IOException)3 ScriptEngineService (org.elasticsearch.script.ScriptEngineService)3 SearchScript (org.elasticsearch.script.SearchScript)3 List (java.util.List)2 IndexReader (org.apache.lucene.index.IndexReader)2 MapperService (org.elasticsearch.index.mapper.MapperService)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1