IDEA连接ElasticSearch创建索引(一)

IDEA连接ElasticSearch创建索引(一)

1.添加pom.xml 加入dependency并更新导入包,elasticsearch 选择7.17.9


<!-- elasticsearch -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.9</version>
</dependency>

2. 建立和销毁 elasticsearch连接


//建立连接
@BeforeEach
void setup(){
    this.client = new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://10.11.242.196:9200")
    ));
}

//销毁连接
@AfterEach
void tearDown() throws IOException {
    this.client.close();
}

//初始化
@Test
void testInit(){
    System.out.println(client);
}

3.操作索引(创建/判断是否存在/删除)



//创建索引库
@Test
void createHotelIndex() throws IOException {
    //1 创建Request 对象
    final CreateIndexRequest request = new CreateIndexRequest("hotel");
    //2 准备请求的参数;DSL语句
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    //3 发送请求
    client.indices().create(request, RequestOptions.DEFAULT);

}

//判定是否存在
@Test
void testExitsHotelIndex() throws IOException {
    //1 创建Request 对象
    GetIndexRequest request = new GetIndexRequest("hotel");

    //3 发送请求
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

    System.out.println(exists ? "索引库已经存在!":"索引库不存在!");
}

//删除
@Test
void testDeleteHotelIndex() throws IOException {
    //1 创建Request 对象
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("hotel");

    //3 发送请求
    client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
}

 

3.1 索引文件json

MAPPING_TEMPLATE


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" +
            "        \"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" +
            "      },      \n" +
            "      \"starName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },      \n" +
            "      \"business\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\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" +
            "  }\n" +
            "}";
}

 

本期结束,精彩继续!