//手动的创建映射 根据需求来确定
/**
* blog系统 按照文章的ID 搜 和标题搜 内容搜
* "mappings" : {
* "properties" : {
* "id" : { "type" : "long","store":"true" },
* "title" : { "type" : "text","analyzer":"ik_smart","index":"true","store":"true" },
* "content" : { "type" : "text","analyzer":"ik_smart","index":"true","store":"true" }
* }
* }
*
*/
@Test
public void putMapping() throws Exception{
PutMappingRequest putMappingRequest = new PutMappingRequest("twitter");
XContentBuilder xContentbuidler = XContentFactory.jsonBuilder()
.startObject()//{
.startObject("properties")//"properties" : {
.startObject("id") // "id" : {
.field("type","long") //"type" : "long"
.field("store","true")
.endObject()//}
.startObject("title") // "title" : {
.field("type","text")
.field("store","true")
.field("index","true")
.field("analyzer","ik_smart")
.endObject()//}
.startObject("content") // "title" : {
.field("type","text")
.field("store","true")
.field("index","true")
.field("analyzer","ik_smart")
.endObject()//}
.endObject()// }
.endObject();//}
putMappingRequest.source(xContentbuidler);
highLevelClient.indices().putMapping(putMappingRequest,RequestOptions.DEFAULT);
}
//文档的操作
@Autowired
private ObjectMapper objectMapper;
//添加文档 更新文档
@Test
public void add() throws Exception {
//批量的导入100条 bulk
long l = System.currentTimeMillis();
BulkRequest request = new BulkRequest();//list
request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);//刷新的策略 默认的情况下需要每隔1S刷新 采用立刻刷新的策略
for (long i = 0; i < 100; i++) {
IndexRequest indexrequest = new IndexRequest("twitter");
Article article = new Article(i, "es数据 elasticsearch"+i, "es数据插入"+i);
String jsonstr = objectMapper.writeValueAsString(article);
indexrequest.source(jsonstr, XContentType.JSON)
//设置文档的唯一标识
.id(""+i);
//highLevelClient.index(indexrequest, RequestOptions.DEFAULT);//添加索引和类型和文档
request.add(indexrequest);
}
//执行批量操作
highLevelClient.bulk(request,RequestOptions.DEFAULT);
long l1 = System.currentTimeMillis();
System.out.println(l1-l);
}