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
码农致富
- 粉丝: 3704
- 资源: 112
最新资源
- Matlab实现GWO-TCN-Multihead-Attention灰狼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测(含完整的程序,GUI设计和代码详解)
- C# 压缩辅助类实例源码
- Arduino IDE esp32开发板 3.1.0 离线安装包 再也不怕网络慢
- Matlab实现GRO-CNN-BiLSTM-Attention淘金算法优化卷积神经网络-双向长短期记忆网络结合注意力机制多变量时间序列预测(含完整的程序,GUI设计和代码详解)
- Matlab实现KPCA-EBWO-SVM核主成分分析和改进的白鲸优化算法优化支持向量机分类预测(含完整的程序,GUI设计和代码详解)
- Matlab实现RIME-HKELM霜冰算法优化混合核极限学习机多变量回归预测(含完整的程序,GUI设计和代码详解)
- Matlab实现CPO-LSSVM冠豪猪算法优化最小二乘支持向量机多变量回归预测(含完整的程序,GUI设计和代码详解)
- Matlab实现ZOA-CNN-LSTM-Attention斑马优化卷积长短期记忆神经网络注意力机制的数据分类预测(含完整的程序,GUI设计和代码详解)
- Matlab实现基于RIME-DBSCAN的数据聚类可视化(含完整的程序,GUI设计和代码详解)
- C# 链接数据库ODBC
- Matlab实现改进黑猩猩优化算法SLWCHOA与多个基准函数对比与秩和检验(含完整的程序,GUI设计和代码详解)
- 冒泡排序模版(c++)
- ArcGIS教程008:三维地形+雨水淹没分析教程数据
- C# 操作Access数据库
- 大一C语言项目实践-小游戏集成开发系统
- 选择排序模版(c++)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页