package cn.xgs.JestClient;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.Suggest;
import io.searchbox.core.SuggestResult;
import io.searchbox.core.Update;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.xgs.entity.CsdnBlog;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.cluster.Health;
import io.searchbox.cluster.NodesInfo;
import io.searchbox.cluster.NodesStats;
import io.searchbox.core.Bulk;
import io.searchbox.core.Count;
import io.searchbox.core.CountResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.indices.ClearCache;
import io.searchbox.indices.CloseIndex;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.Flush;
import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.Optimize;
import io.searchbox.indices.mapping.PutMapping;
/**
* Elasticserach jestClient示例
* @author zshuai
*
*/
public class Jest {
private static JestClient jestClient;
private static String indexName = "article";
private static String typeName = "content";
@Before
public void getClient() throws Exception{
System.out.println("连接");
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://192.168.189.138:9200")
.gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())
.connTimeout(1500)
.readTimeout(3000)
.multiThreaded(true)
.build());
jestClient=factory.getObject();
}
@After
public void tearDown() throws Exception {
closeJestClient(jestClient);
}
/**
* 关闭JestClient客户端
* @param jestClient
* @throws Exception
*/
public void closeJestClient(JestClient jestClient) throws Exception {
if (jestClient != null) {
System.out.println("关闭");
jestClient.shutdownClient();
}
}
/**
* ---------------------------测试--------------------------
*/
/**
* 创建索引
* @throws Exception
*/
@Test
public void createIndex() throws Exception {
JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());
System.out.println(jr.isSucceeded());
}
/**
* Put映射
* @throws Exception
*/
@Test
public void createIndexMapping() throws Exception {
String source = "{\"" + typeName + "\":{\"properties\":{"
+ "\"author\":{\"type\":\"string\",\"index\":\"not_analyzed\"}"
+ ",\"title\":{\"type\":\"string\"}"
+ ",\"content\":{\"type\":\"string\"}"
+ ",\"price\":{\"type\":\"string\"}"
+ ",\"view\":{\"type\":\"string\"}"
+ ",\"tag\":{\"type\":\"string\"}"
+ ",\"date\":{\"type\":\"date\",\"format\":\"yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"}"
+ "}}}";
System.out.println(source);
PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();
JestResult jr = jestClient.execute(putMapping);
System.out.println(jr.isSucceeded());
}
/**
* 插入多个
* @throws IOException
*/
@Test
public void insertMultiple() throws IOException{
CsdnBlog csdnBlog1=new CsdnBlog();
csdnBlog1.setAuthor("AAAA");
csdnBlog1.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog1.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog1.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
csdnBlog1.setView("100");
csdnBlog1.setTag("JAVA,ANDROID,C++,LINUX");
CsdnBlog csdnBlog2=new CsdnBlog();
csdnBlog2.setAuthor("BBBB");
csdnBlog2.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog2.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog2.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
csdnBlog2.setView("200");
csdnBlog2.setTag("JAVA,ANDROID,C++,LINUX");
CsdnBlog csdnBlog3=new CsdnBlog();
csdnBlog3.setAuthor("CCCC");
csdnBlog3.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog3.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog3.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
csdnBlog3.setView("300");
csdnBlog3.setTag("JAVA,ANDROID,C++,LINUX");
CsdnBlog csdnBlog4=new CsdnBlog();
csdnBlog4.setAuthor("CCCC");
csdnBlog4.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog4.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog4.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
csdnBlog4.setView("400");
csdnBlog4.setTag("JAVA,ANDROID,C++,LINUX");
Index index1 = new Index.Builder(csdnBlog1).index("article").type("article").build();
Index index2 = new Index.Builder(csdnBlog2).index("article").type("article").build();
Index index3 = new Index.Builder(csdnBlog3).index("article").type("article").build();
Index index4 = new Index.Builder(csdnBlog4).index("article").type("article").build();
JestResult jestResult1 = jestClient.execute(index1);
System.out.println(jestResult1.getJsonString());
JestResult jestResult2 = jestClient.execute(index2);
System.out.println(jestResult2.getJsonString());
JestResult jestResult3 = jestClient.execute(index3);
System.out.println(jestResult3.getJsonString());
JestResult jestResult4 = jestClient.execute(index3);
System.out.println(jestResult4.getJsonString());
}
/**
* Bulk插入数据
* @throws IOException
*/
@Test
public void bulkIndex() throws IOException{
CsdnBlog csdnBlog1=new CsdnBlog();
csdnBlog1.setAuthor("1111");
csdnBlog1.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog1.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog1.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));