package com.fendo.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.action.search.SearchResponse;
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.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.fendo.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 fendo
*
*/
public class Jest {
private static JestClient jestClient;
private static String indexName = "article";
private static String typeName = "content";
@Before
public void getClient() throws Exception{
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost: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) {
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("AAAA");
csdnBlog1.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
csdnBlog1.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
csdnBlog1.setDate(new SimpleDateFormat("yyyy
没有合适的资源?快使用搜索试试~ 我知道了~
ES5.5.1 JestClient示例
共16个文件
class:4个
prefs:3个
java:3个
5星 · 超过95%的资源 需积分: 50 123 下载量 14 浏览量
2017-09-05
16:22:31
上传
评论 3
收藏 28KB ZIP 举报
温馨提示
Elasticsearch5.5.1使用JAVA客户端Jest操作的一些示例代码,详细介绍请参考文章:http://blog.csdn.net/u011781521/article/details/77852861
资源推荐
资源详情
资源评论
收起资源包目录
JestClient.zip (16个子文件)
JestClient
.project 562B
src
test
java
com
fendo
JestClient
AppTest.java 686B
main
java
com
fendo
JestClient
Jest.java 29KB
entity
CsdnBlog.java 1KB
target
classes
com
fendo
JestClient
AppTest.class 627B
Jest.class 25KB
entity
CsdnBlog.class 2KB
META-INF
MANIFEST.MF 107B
maven
com.fendo
JestClient
pom.properties 244B
pom.xml 2KB
test-classes
com
fendo
JestClient
AppTest.class 627B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 736B
org.eclipse.core.resources.prefs 119B
pom.xml 2KB
.classpath 971B
共 16 条
- 1
码农致富
- 粉丝: 3703
- 资源: 112
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页