[AppleScript] 纯文本查看 复制代码
/**创建 索引*/
@Test
public void testCreateIndex_boost() throws Exception{
// 创建索引
client.admin().indices().prepareCreate("blog1").get();
//关闭
client.close();
}
/**
* 创建映射
*/
@Test
public void testCreateIndexMapping_boost() throws Exception{
//构建json的数据格式,创建映射
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type","integer").field("store", "yes")
.endObject()
.startObject("title")
.field("type","string").field("store", "yes").field("analyzer","ik")
.endObject()
.startObject("content")
.field("type","string").field("store", "yes").field("analyzer","ik")
.endObject()
.startObject("comment")
.field("type","string").field("store", "yes").field("analyzer","ik")
.endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest request = Requests.putMappingRequest("blog1")
.type("article")
.source(mappingBuilder);
client.admin().indices().putMapping(request).get();
//关闭
client.close();
}
/**创建文档*/
@Test
public void createDocument_boost() throws Exception{
Article article = new Article();
// article.setId(1);
// article.setTitle("搜索引擎服务器"); // 有搜索
// article.setContent("基于restful的数据风格"); // 无搜索
// article.setComment("我们学习Elasticsearch搜索引擎服务器");// 有搜索
article.setId(2);
article.setTitle("什么是Elasticsearch"); // 无搜索
article.setContent("Elasticsearch搜索引擎服务器"); // 有搜索
article.setComment("Elasticsearch封装了lucene");// 无搜索
ObjectMapper objectMapper = new ObjectMapper();
String source = objectMapper.writeValueAsString(article);
System.out.println("source:"+source);
IndexResponse indexResponse = client.prepareIndex("blog1", "article", article.getId().toString()).setSource(source).get();
// 获取响应的信息
System.out.println("索引名称:"+indexResponse.getIndex());
System.out.println("文档类型:"+indexResponse.getType());
System.out.println("ID:"+indexResponse.getId());
System.out.println("版本:"+indexResponse.getVersion());
System.out.println("是否创建成功:"+indexResponse.isCreated());
client.close();
}