Search in sources :

Example 1 with LeafSearchScript

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

the class ScriptScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
    final LeafSearchScript leafScript = script.getLeafSearchScript(ctx);
    final CannedScorer scorer = new CannedScorer();
    leafScript.setScorer(scorer);
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) {
            leafScript.setDocument(docId);
            scorer.docid = docId;
            scorer.score = subQueryScore;
            double result = leafScript.runAsDouble();
            if (Double.isNaN(result)) {
                throw new GeneralScriptException("script_score returned NaN");
            }
            return result;
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
            Explanation exp;
            if (leafScript instanceof ExplainableSearchScript) {
                leafScript.setDocument(docId);
                scorer.docid = docId;
                scorer.score = subQueryScore.getValue();
                exp = ((ExplainableSearchScript) leafScript).explain(subQueryScore);
            } else {
                double score = score(docId, subQueryScore.getValue());
                String explanation = "script score function, computed with script:\"" + sScript + "\"";
                if (sScript.getParams() != null) {
                    explanation += " and parameters: \n" + sScript.getParams().toString();
                }
                Explanation scoreExp = Explanation.match(subQueryScore.getValue(), "_score: ", subQueryScore);
                return Explanation.match(CombineFunction.toFloat(score), explanation, scoreExp);
            }
            return exp;
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Explanation(org.apache.lucene.search.Explanation) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript)

Example 2 with LeafSearchScript

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

the class ScriptSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final SearchScript searchScript = context.getSearchScript(script, ScriptContext.Standard.SEARCH);
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    boolean reverse = (order == SortOrder.DESC);
    if (valueMode == null) {
        valueMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    final IndexFieldData.XFieldComparatorSource fieldComparatorSource;
    switch(type) {
        case STRING:
            fieldComparatorSource = new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final BinaryDocValues values = new BinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public BytesRef get(int docID) {
                            leafScript.setDocument(docID);
                            spare.copyChars(leafScript.run().toString());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        case NUMBER:
            fieldComparatorSource = new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public double get(int docID) {
                            leafScript.setDocument(docID);
                            return leafScript.runAsDouble();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
    return new SortFieldAndFormat(new SortField("_script", fieldComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) Scorer(org.apache.lucene.search.Scorer) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException) MultiValueMode(org.elasticsearch.search.MultiValueMode) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with LeafSearchScript

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

the class ScriptFieldsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.hasScriptFields() == false) {
        return;
    }
    for (ScriptFieldsContext.ScriptField scriptField : context.scriptFields().fields()) {
        /* Because this is called once per document we end up creating new ScriptDocValues for every document which is important because
             * the values inside ScriptDocValues might be reused for different documents (Dates do this). */
        LeafSearchScript leafScript;
        try {
            leafScript = scriptField.script().getLeafSearchScript(hitContext.readerContext());
        } catch (IOException e1) {
            throw new IllegalStateException("Failed to load script", e1);
        }
        leafScript.setDocument(hitContext.docId());
        final Object value;
        try {
            value = leafScript.unwrap(leafScript.run());
        } catch (RuntimeException e) {
            if (scriptField.ignoreException()) {
                continue;
            }
            throw e;
        }
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<>(2));
        }
        SearchHitField hitField = hitContext.hit().getFields().get(scriptField.name());
        if (hitField == null) {
            final List<Object> values;
            if (value instanceof Collection) {
                // TODO: use diamond operator once JI-9019884 is fixed
                values = new ArrayList<>((Collection<?>) value);
            } else {
                values = Collections.singletonList(value);
            }
            hitField = new SearchHitField(scriptField.name(), values);
            hitContext.hit().getFields().put(scriptField.name(), hitField);
        }
    }
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Collection(java.util.Collection) SearchHitField(org.elasticsearch.search.SearchHitField) IOException(java.io.IOException)

Example 4 with LeafSearchScript

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

the class ScriptedMetricAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    final LeafSearchScript leafMapScript = mapScript.getLeafSearchScript(ctx);
    return new LeafBucketCollectorBase(sub, mapScript) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0 : bucket;
            leafMapScript.setDocument(doc);
            leafMapScript.run();
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase)

Example 5 with LeafSearchScript

use of org.elasticsearch.script.LeafSearchScript 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)

Aggregations

LeafSearchScript (org.elasticsearch.script.LeafSearchScript)5 IOException (java.io.IOException)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 GeneralScriptException (org.elasticsearch.script.GeneralScriptException)2 SearchScript (org.elasticsearch.script.SearchScript)2 Collection (java.util.Collection)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 Explanation (org.apache.lucene.search.Explanation)1 Scorer (org.apache.lucene.search.Scorer)1 SortField (org.apache.lucene.search.SortField)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)1 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)1 SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)1 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)1 BytesRefFieldComparatorSource (org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource)1 DoubleValuesComparatorSource (org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource)1 QueryShardException (org.elasticsearch.index.query.QueryShardException)1