package io.searchbox.core;


import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;

import java.io.IOException;

/**
 * @author Dogukan Sonmez
 */
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
public class PercolateIntegrationTest extends AbstractIntegrationTest {

    @Test
    public void percolateWithValidParameters() throws IOException {
        String index = "cvbank";
        String type = "candidate";
        createIndex(index);
        ensureSearchable(index);

        PutMappingResponse putMappingResponse = client().admin().indices().putMapping(
                new PutMappingRequest(index).type(type).source(
                        "{ \"properties\" : { \"language\" : {\"type\" : \"string\", \"store\" : \"yes\"} } }"
                )
        ).actionGet();
        assertTrue(putMappingResponse.isAcknowledged());

        String query = "{\n" +
                "    \"query\" : {\n" +
                "        \"term\" : {\n" +
                "            \"language\" : \"java\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        // register percolator query on our index
        JestResult result = client.execute(new Index.Builder(query).index(index).type(".percolator").id("1").build());
        assertTrue(result.getErrorMessage(), result.isSucceeded());

        // try to match a document against the registered percolator
        Percolate percolate = new Percolate.Builder(index, type, "{\"doc\" : {\"language\":\"java\"}}").build();
        result = client.execute(percolate);
        assertTrue(result.getErrorMessage(), result.isSucceeded());
        assertEquals(1, result.getJsonObject().getAsJsonPrimitive("total").getAsInt());
    }
}
@@ -368,16 +368,100 @@
 ption;%0A%0A
+import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;%0A%0A
 /**%0A * @
@@ -864,48 +864,20 @@
     
-PutMappingResponse putMappingResponse = 
+assertAcked(
 clie
@@ -900,16 +900,23 @@
 ices().p
+repareP
 utMappin
@@ -917,16 +917,22 @@
 Mapping(
+index)
 %0A       
@@ -944,38 +944,13 @@
     
-new PutMappingRequest(index).t
+.setT
 ype(
@@ -958,16 +958,8 @@
 ype)
-.source(
 %0A   
@@ -975,19 +975,21 @@
     
-        
+.setSource(
 %22%7B
- 
 %5C%22pr
@@ -1002,13 +1002,10 @@
 es%5C%22
- : %7B 
+:%7B
 %5C%22la
@@ -1012,19 +1012,17 @@
 nguage%5C%22
- : 
+:
 %7B%5C%22type%5C
@@ -1026,23 +1026,21 @@
 pe%5C%22
- : %5C%22string
+:%5C%22keyword
 %5C%22,
- 
 %5C%22st
@@ -1048,19 +1048,17 @@
 re%5C%22
- : 
+:
 %5C%22yes%5C%22%7D
  %7D %7D
@@ -1057,13 +1057,12 @@
 s%5C%22%7D
- %7D 
+%7D
 %7D%22
+)
 %0A   
@@ -1074,17 +1074,24 @@
         
-)
+.get());
 %0A       
@@ -1095,75 +1095,213 @@
     
-).actionGet();%0A        as
+assertAcked(client().admin().indices().preparePutMapping(index)%0A                .
 se
-r
 tT
-rue(putMappingResponse.isAcknowledged
+ype(%22queries%22)%0A                .setSource(%22%7B%5C%22properties%5C%22:%7B%5C%22query%5C%22:%7B%5C%22type%5C%22:%5C%22percolator%5C%22%7D%7D%7D%22)%0A                .get
 ());
@@ -1697,28 +1697,38 @@
 pe(%22
-.percolator%22).id(%221%22
+queries%22).id(%221%22).refresh(true
 ).bu

Fix PercolateIntegrationTest