【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】
步骤4:创建索引库
创建索引库代码如下:
试试
先来一个常量类,存放json 语句
package cn.itcast.hotel.constants;
/**
* ClassName: HotelConstants
* date: 2022/11/1 10:16
*
* @author DingJiaxiong
*/
public class HotelConstants {
public static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\":\"keyword\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
OK,编写测试方法
//创建索引库
@Test
void createHotelIndex() throws IOException {
//1. 创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//2. 准备请求的参数:DSL 语句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3. 发送请求
client.indices().create(request , RequestOptions.DEFAULT);
}
运行结果
我超,绿了
到kibana 查一下
GET /hotel
运行结果
没毛病,索引库这就创建成功了