/*
 * 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.search.functionscore;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.test.ElasticsearchBackwardsCompatIntegrationTest;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.client.Requests.searchRequest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;

/**
 */
public class FunctionScoreBackwardCompatibilityTests extends ElasticsearchBackwardsCompatIntegrationTest {

    /**
     * Simple upgrade test for function score
     */
    @Test
    public void testSimpleFunctionScoreParsingWorks() throws IOException, ExecutionException, InterruptedException {

        assertAcked(prepareCreate("test").addMapping(
                "type1",
                jsonBuilder().startObject()
                        .startObject("type1")
                        .startObject("properties")
                        .startObject("text")
                        .field("type", "string")
                        .endObject()
                        .startObject("loc")
                        .field("type", "geo_point")
                        .endObject()
                        .startObject("popularity")
                        .field("type", "float")
                        .endObject()
                        .endObject()
                        .endObject()
                        .endObject()));
        ensureYellow();

        int numDocs = 10;
        String[] ids = new String[numDocs];
        List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
        for (int i = 0; i < numDocs; i++) {
            String id = Integer.toString(i);
            indexBuilders.add(client().prepareIndex()
                    .setType("type1").setId(id).setIndex("test")
                    .setSource(
                            jsonBuilder().startObject()
                                    .field("text", "value")
                                    .startObject("loc")
                                    .field("lat", 10 + i)
                                    .field("lon", 20)
                                    .endObject()
                                    .field("popularity", 2.71828)
                                    .endObject()));
            ids[i] = id;
        }
        indexRandom(true, indexBuilders);
        checkFunctionScoreStillWorks(ids);
        logClusterState();
        boolean upgraded;
        int upgradedNodesCounter = 1;
        do {
            logger.debug("function_score bwc: upgrading {}st node", upgradedNodesCounter++);
            upgraded = backwardsCluster().upgradeOneNode();
            ensureYellow();
            logClusterState();
            checkFunctionScoreStillWorks(ids);
        } while (upgraded);
        logger.debug("done function_score while upgrading");
    }

    private void checkFunctionScoreStillWorks(String... ids) throws ExecutionException, InterruptedException, IOException {
        SearchResponse response = client().search(
                searchRequest().source(
                        searchSource().query(
                                functionScoreQuery(termFilter("text", "value"))
                                        .add(gaussDecayFunction("loc", new GeoPoint(10, 20), "1000km"))
                                        .add(fieldValueFactorFunction("popularity").modifier(FieldValueFactorFunction.Modifier.LN))
                                        .add(scriptFunction("_index['text']['value'].tf()"))
                        ))).actionGet();
        assertSearchResponse(response);
        assertOrderedSearchHits(response, ids);
    }
}

@@ -2620,144 +2620,8 @@
 t()%0A
-                        .startObject(%22popularity%22)%0A                        .field(%22type%22, %22float%22)%0A                        .endObject()%0A
     
@@ -2645,32 +2645,32 @@
    .endObject()%0A
+
                 
@@ -3247,17 +3247,45 @@
 , %22value
-%22
+ %22 + (i %3C 5 ? %22boosted%22 : %22%22)
 )%0A      
@@ -3499,74 +3499,8 @@
 t()%0A
-                                    .field(%22popularity%22, 2.71828)%0A
     
@@ -4620,92 +4620,53 @@
 add(
-fieldValueFactorFunction(%22popularity%22).modifier(FieldValueFactorFunction.Modifier.LN
+scriptFunction(%22_index%5B'text'%5D%5B'value'%5D.tf()%22
 ))%0A 
@@ -4713,53 +4713,55 @@
 add(
-scriptFunction(%22_index%5B'text'%5D%5B'value'%5D.tf()%22
+termFilter(%22text%22, %22boosted%22), factorFunction(5
 ))%0A 

remove field_value_factor , was only added 1.2