定义创建索引库的Json参数
:::tips 新建一个常量类,用来存储创建索引库的Json参数常量 :::
public class XxxConstants {
//mapping映射的字段串常量
public static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"copy_to\": \"all\"\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" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\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" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
创建索引库
@SpringBootTest
public class MyTest{
//注入RestHighLevelClient对象
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void test() throws IOException {
//创建CreateIndexRequest请求对象,并指定索引库名
CreateIndexRequest createIndexRequest = new CreateIndexRequest(索引库名);
//在CreateIndexRequest请求对象中放入创建索引库的Json参数,并指定参数类型为Json
createIndexRequest.source(MAPPING_TEMPLATE, XContentType.JSON);
//发送请求
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}
}