/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.elasticsearch.script;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.function.DoubleSupplier;

/**
 * A script used for adjusting the score on a per document basis.
 */
public abstract class ScoreScript {
    
    public static final String[] PARAMETERS = new String[]{};
    
    /** The generic runtime parameters for the script. */
    private final Map<String, Object> params;
    
    /** A leaf lookup for the bound segment this script will operate on. */
    private final LeafSearchLookup leafLookup;
    
    private DoubleSupplier scoreSupplier = () -> 0.0;
    
    public ScoreScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) {
        this.params = params;
        this.leafLookup = lookup.getLeafSearchLookup(leafContext);
    }
    
    public abstract double execute();
    
    /** Return the parameters for this script. */
    public Map<String, Object> getParams() {
        return params;
    }
    
    /** The doc lookup for the Lucene segment this script was created for. */
    public final Map<String, ScriptDocValues<?>> getDoc() {
        return leafLookup.doc();
    }
    
    /** Set the current document to run the script on next. */
    public void setDocument(int docid) {
        leafLookup.setDocument(docid);
    }
    
    public void setScorer(Scorer scorer) {
        this.scoreSupplier = () -> {
            try {
                return scorer.score();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        };
    }
    
    public double get_score() {
        return scoreSupplier.getAsDouble();
    }
    
    /** A factory to construct {@link ScoreScript} instances. */
    public interface LeafFactory {
    
        /**
         * Return {@code true} if the script needs {@code _score} calculated, or {@code false} otherwise.
         */
        boolean needs_score();
        
        ScoreScript newInstance(LeafReaderContext ctx) throws IOException;
    }
    
    /** A factory to construct stateful {@link ScoreScript} factories for a specific index. */
    public interface Factory {
        
        ScoreScript.LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup);
        
    }
    
    public static final ScriptContext<ScoreScript.Factory> CONTEXT = new ScriptContext<>("score", ScoreScript.Factory.class);
}

@@ -1133,32 +1133,88 @@
 kedIOException;%0A
+import java.util.Collections;%0Aimport java.util.HashMap;%0A
 import java.util
@@ -1376,577 +1376,1598 @@
 t %7B%0A
+%0A
     
-%0A    public static final String%5B%5D PARAMETERS = new String%5B%5D%7B%7D;%0A    %0A    /** The generic runtime parameters for the script. */%0A    private final Map%3CString, Object%3E params;%0A    %0A    /** A leaf lookup for the bound segment this script will operate on. */%0A    private final LeafSearchLookup leafLookup;%0A    %0A    private DoubleSupplier scoreSupplier = () -%3E 0.0;%0A    %0A    public ScoreScript(Map%3CString, Object%3E params, SearchLookup lookup, LeafReaderContext leafContext) %7B%0A        this.params = params;%0A        this.leafLookup = lookup.getLeafSearchLookup(leafContext);
+private static final Map%3CString, String%3E DEPRECATIONS;%0A    static %7B%0A        Map%3CString, String%3E deprecations = new HashMap%3C%3E();%0A        deprecations.put(%0A            %22doc%22,%0A            %22Accessing variable %5Bdoc%5D via %5Bparams.doc%5D from within a score script %22 +%0A                %22is deprecated in favor of directly accessing %5Bdoc%5D.%22%0A        );%0A        deprecations.put(%0A            %22_doc%22,%0A            %22Accessing variable %5Bdoc%5D via %5Bparams._doc%5D from within a score script %22 +%0A                %22is deprecated in favor of directly accessing %5Bdoc%5D.%22%0A        );%0A        DEPRECATIONS = Collections.unmodifiableMap(deprecations);%0A    %7D%0A%0A    public static final String%5B%5D PARAMETERS = new String%5B%5D%7B%7D;%0A    %0A    /** The generic runtime parameters for the script. */%0A    private final Map%3CString, Object%3E params;%0A    %0A    /** A leaf lookup for the bound segment this script will operate on. */%0A    private final LeafSearchLookup leafLookup;%0A    %0A    private DoubleSupplier scoreSupplier = () -%3E 0.0;%0A    %0A    public ScoreScript(Map%3CString, Object%3E params, SearchLookup lookup, LeafReaderContext leafContext) %7B%0A        // null check needed b/c of expression engine subclass%0A        if (lookup == null) %7B%0A            assert params == null;%0A            assert leafContext == null;%0A            this.params = null;%0A            this.leafLookup = null;%0A        %7D else %7B%0A            this.leafLookup = lookup.getLeafSearchLookup(leafContext);%0A            params = new HashMap%3C%3E(params);%0A            params.putAll(leafLookup.asMap());%0A            this.params = new ParameterMap(params, DEPRECATIONS);%0A        %7D
 %0A   

Add back lookup vars in score script (#34833)