CreateIndexRequest request = new CreateIndexRequest(“ysx_course”);
request.settings(Settings.builder().put(“number_of_shards”, “1”).put(“number_of_replicas”, “0”));
// 创建映射
request.mapping(“doc”, “{\n” +
" “properties”: {\n" +
" “description”: {\n" +
" “type”: “text”,\n" +
" “analyzer”: “ik_max_word”,\n" +
" “search_analyzer”: “ik_smart”\n" +
" },\n" +
" “name”: {\n" +
" “type”: “text”,\n" +
" “analyzer”: “ik_max_word”,\n" +
" “search_analyzer”: “ik_smart”\n" +
" },\n" +
““pic”:{ \n” +
““type”:“text”, \n” +
““index”:false \n” +
“}, \n” +
" “price”: {\n" +
" “type”: “float”\n" +
" },\n" +
" “studymodel”: {\n" +
" “type”: “keyword”\n" +
" },\n" +
" “timestamp”: {\n" +
" “type”: “date”,\n" +
" “format”: “yyyy-MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis”\n" +
" }\n" +
" }\n" +
" }", XContentType.JSON);
// 执行创建操作
CreateIndexResponse response = indices.create(request);
// 得到响应
boolean b = response.isAcknowledged();
System.out.println(b);
}
Java API操作ES
准备数据环境
创建索引:ysx_course
创建映射:
PUT http://localhost:9200/ysx_course/doc/_mapping
{
“properties”: {
“description”: { // 课程描述
“type”: “text”, // String text 类型
“analyzer”: “ik_max_word”, // 存入的分词模式:细粒度
“search_analyzer”: “ik_smart” // 查询的分词模式:粗粒度
},
“name”: { // 课程名称
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_smart”
},
“pic”:{ // 图片地址
“type”:“text”,
“index”:false // 地址不用来搜索,因此不为它构建索引
},
“price”: { // 价格
“type”: “scaled_float”, // 有比例浮点
“scaling_factor”: 100 // 比例因子 100
},
“studymodel”: {
“type”: “keyword” // 不分词,全关键字匹配
},
“timestamp”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”
}
}
}
加入原始数据:
POST http://localhost:9200/ysx_course/doc/1
{
“name”: “Bootstrap开发”,
“description”: “Bootstrap是由Twitter推出的一个前台页面开发框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松的实现一个不受浏览器限制的精美界面效果。”,
“studymodel”: “201002”,
“price”:38.6,
“timestamp”:“2018-04-25 19:11:35”,
“pic”:“group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg”
}
DSL搜索
DSL(Domain S