package org.carrot2.elasticsearch.plugin;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.network.NetworkUtils;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

/**
 * 
 */
public class SimpleClusteringTest {
    private static Node node;

    @BeforeClass
    public static void setup() throws IOException {
        node = nodeBuilder().settings(settingsBuilder()
                .put("path.data", "target/data")
                .put("cluster.name", "test-cluster-" + NetworkUtils.getLocalAddress()))
                .local(true)
                .node();

        // Wait for the node/ cluster to come alive.
        node.client().admin().cluster().prepareHealth()
                .setWaitForEvents(Priority.LANGUID)
                .setWaitForYellowStatus().execute().actionGet();

        // Delete any previous documents.
        final String indexName = "test";
        node.client().deleteByQuery(
                Requests.deleteByQueryRequest(indexName).query(
                        QueryBuilders.matchAllQuery())).actionGet();

        // Index some sample "documents".
        BulkRequestBuilder bulk = node.client().prepareBulk();
        for (String[] data : SampleDocumentData.SAMPLE_DATA) {
            bulk.add(new IndexRequestBuilder(node.client())
            .setIndex(indexName)
            .setType("test")
            .setSource(XContentFactory.jsonBuilder()
                    .startObject()
                        .field("url",     data[0])
                        .field("title",   data[1])
                        .field("content", data[2])
                    .endObject()));
        }
        bulk.setRefresh(true).execute().actionGet();

        SearchResponse search = 
                node.client()
                    .prepareSearch("test")
                    .setTypes("test")
                    .setQuery(QueryBuilders.termQuery("_all", "data"))
                    .addHighlightedField("content")
                    .addHighlightedField("title")
                    .execute().actionGet();

        System.out.println(search);
    }

    @AfterClass
    public void tearDown() {
        node.close();
    }

    @Test
    public void testSimpleClustering() throws Exception {
    }
}

@@ -32,24 +32,114 @@
 ch.plugin;%0A%0A
+import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;%0A
 import org.e
@@ -1584,16 +1584,144 @@
 %22test%22;%0A
+        if (new IndicesExistsRequestBuilder(node.client().admin().indices(), indexName).execute().actionGet().isExists()) %7B%0A    
         
@@ -1745,24 +1745,28 @@
 eteByQuery(%0A
+    
             
@@ -1841,16 +1841,20 @@
         
+  
+  
 QueryBui
@@ -1881,32 +1881,42 @@
 ))).actionGet();
+%0A        %7D
 %0A%0A        // Ind
@@ -2137,24 +2137,28 @@
             
+    
 .setIndex(in
@@ -2170,32 +2170,36 @@
 me)%0A            
+    
 .setType(%22test%22)
@@ -2207,24 +2207,28 @@
             
+  
+  
 .setSource(X
@@ -2248,32 +2248,36 @@
 y.jsonBuilder()%0A
+    
                 
@@ -2311,32 +2311,36 @@
                 
+    
 .field(%22url%22,   
@@ -2366,32 +2366,36 @@
                 
+ 
+   
 .field(%22title%22, 
@@ -2405,16 +2405,20 @@
 ata%5B1%5D)%0A
+    
         
@@ -2460,16 +2460,20 @@
 ata%5B2%5D)%0A
+    
         

Add content.