// Path: src/test/java/org/elasticsearch/api/demo/XPackBaseDemo.java
// public class XPackBaseDemo {
//     protected TransportClient client;
//     protected RestClient restClient ;
// 
//     @Before
//     public void setUp() throws Exception {
//         /**
//          * 如果es集群安装了x-pack插件则以此种方式连接集群
//          * 1. java客户端的方式是以tcp协议在9300端口上进行通信
//          * 2. http客户端的方式是以http协议在9200端口上进行通信
//          */
//         Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build();
//         client = new PreBuiltXPackTransportClient(settings)
//                 .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9200));
//         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//         credentialsProvider.setCredentials(AuthScope.ANY,
//                 new UsernamePasswordCredentials("elastic", "changeme"));
//         restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
//                 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//                     @Override
//                     public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                         return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                     }
//                 }).build();
//     }
// 
//     @After
//     public void tearDown() throws Exception {
//         if (client != null ){
//             client.close();
//         }
// 
//         if (restClient != null){
//             restClient.close();
//         }
//     }
// 
//     @Test
//     public void testClientConnection() throws Exception {
//         AnalyzeRequest analyzeRequest = new AnalyzeRequest();
//         analyzeRequest.text("美女");
//         ActionFuture<AnalyzeResponse> analyzeResponseActionFuture = client.admin().indices().analyze(analyzeRequest);
//         List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
//         for (AnalyzeResponse.AnalyzeToken analyzeToken  : analyzeTokens){
//             System.out.println(analyzeToken.getTerm());
//         }
//     }
// 
// 
//     @Test
//     public void testRestClientConnection() throws Exception {
//         String method = "GET";
//         String endpoint = "/_analyze";
//         Map<String, String> params = new HashMap<>();
//         params.put("analyzer","standard");
//         params.put("text","中华人民共和国");
//         Response response = restClient.performRequest(method,endpoint,params);
//         System.out.println(JSON.toJSONString(JSONObject.parse(EntityUtils.toString(response.getEntity())),SerializerFeature.PrettyFormat));
//     }
// 
//     protected void println(SearchResponse searchResponse){
//         SearchHit[]  searchHits = searchResponse.getHits().getHits();
//         for (SearchHit searchHit : searchHits){
//             System.out.println(JSON.toJSONString(searchHit.getSourceAsString(),SerializerFeature.PrettyFormat));
//         }
//     }
// }


import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.api.demo.XPackBaseDemo;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.rescore.QueryRescoreMode;
import org.elasticsearch.search.rescore.QueryRescorerBuilder;
import org.junit.Test;
package org.elasticsearch.api.demo.script;


/**
 * @auhthor lei.fang@shijue.me
 * @since . 2017-07-08
 */
public class ScriptLanguageDemo extends XPackBaseDemo {


    @Test
    public void test() throws Exception {


        SearchResponse searchResponse = client.prepareSearch()
                .setIndices("test")
                .setTypes("test")
                .setQuery(QueryBuilders.matchQuery("name","天津公安"))
                .addRescorer(new QueryRescorerBuilder(QueryBuilders.matchPhraseQuery("name","天津公安")))
                .addRescorer(new QueryRescorerBuilder(
                        QueryBuilders.functionScoreQuery(
                                ScoreFunctionBuilders.scriptFunction("doc['time'].value / 10000")
                        )
// Path: src/test/java/org/elasticsearch/api/demo/XPackBaseDemo.java
// public class XPackBaseDemo {
//     protected TransportClient client;
//     protected RestClient restClient ;
// 
//     @Before
//     public void setUp() throws Exception {
//         /**
//          * 如果es集群安装了x-pack插件则以此种方式连接集群
//          * 1. java客户端的方式是以tcp协议在9300端口上进行通信
//          * 2. http客户端的方式是以http协议在9200端口上进行通信
//          */
//         Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build();
//         client = new PreBuiltXPackTransportClient(settings)
//                 .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9200));
//         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//         credentialsProvider.setCredentials(AuthScope.ANY,
//                 new UsernamePasswordCredentials("elastic", "changeme"));
//         restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
//                 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//                     @Override
//                     public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                         return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                     }
//                 }).build();
//     }
// 
//     @After
//     public void tearDown() throws Exception {
//         if (client != null ){
//             client.close();
//         }
// 
//         if (restClient != null){
//             restClient.close();
//         }
//     }
// 
//     @Test
//     public void testClientConnection() throws Exception {
//         AnalyzeRequest analyzeRequest = new AnalyzeRequest();
//         analyzeRequest.text("美女");
//         ActionFuture<AnalyzeResponse> analyzeResponseActionFuture = client.admin().indices().analyze(analyzeRequest);
//         List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
//         for (AnalyzeResponse.AnalyzeToken analyzeToken  : analyzeTokens){
//             System.out.println(analyzeToken.getTerm());
//         }
//     }
// 
// 
//     @Test
//     public void testRestClientConnection() throws Exception {
//         String method = "GET";
//         String endpoint = "/_analyze";
//         Map<String, String> params = new HashMap<>();
//         params.put("analyzer","standard");
//         params.put("text","中华人民共和国");
//         Response response = restClient.performRequest(method,endpoint,params);
//         System.out.println(JSON.toJSONString(JSONObject.parse(EntityUtils.toString(response.getEntity())),SerializerFeature.PrettyFormat));
//     }
// 
//     protected void println(SearchResponse searchResponse){
//         SearchHit[]  searchHits = searchResponse.getHits().getHits();
//         for (SearchHit searchHit : searchHits){
//             System.out.println(JSON.toJSONString(searchHit.getSourceAsString(),SerializerFeature.PrettyFormat));
//         }
//     }
// }


// Path: src/test/java/org/elasticsearch/api/demo/script/ScriptLanguageDemo.java
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.api.demo.XPackBaseDemo;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.rescore.QueryRescoreMode;
import org.elasticsearch.search.rescore.QueryRescorerBuilder;
import org.junit.Test;

package org.elasticsearch.api.demo.script;


/**
 * @auhthor lei.fang@shijue.me
 * @since . 2017-07-08
 */
public class ScriptLanguageDemo extends XPackBaseDemo {


    @Test
    public void test() throws Exception {


        SearchResponse searchResponse = client.prepareSearch()
                .setIndices("test")
                .setTypes("test")
                .setQuery(QueryBuilders.matchQuery("name","天津公安"))
                .addRescorer(new QueryRescorerBuilder(QueryBuilders.matchPhraseQuery("name","天津公安")))
                .addRescorer(new QueryRescorerBuilder(
                        QueryBuilders.functionScoreQuery(
                                ScoreFunctionBuilders.scriptFunction("doc['time'].value / 10000")
                        )
                ).windowSize(100).setScoreMode(QueryRescoreMode.Multiply))