24-RestHighLevelClient----索引&映射操作


與我們kibana語法幾乎無縫銜接

@SpringBootTest
public class RestHighLevelClientTest {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public RestHighLevelClientTest(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    //添加索引&映射
    @Test
    public void testIndexAndMapping() throws IOException {
        //參數1:創建索引請求對象 參數2:請求配置對象
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("products");

        //一般在kibana中寫好后再復制過來
        createIndexRequest.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"title\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"price\":{\n" +
                "        \"type\": \"double\"\n" +
                "      },\n" +
                "      \"created_at\":{\n" +
                "        \"type\": \"date\"\n" +
                "      },\n" +
                "      \"description\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }\n" +
                "  }", XContentType.JSON); //參數1:指定映射json結構   參數2:指定數據結構

        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println("創建狀態:" + createIndexResponse.isAcknowledged());

        restHighLevelClient.close();//關閉資源
    }

    //刪除索引
    @Test
    public void testDeleteIndex() throws IOException {
        //參數1:刪除索引對象  參數2:請求配置對象
        AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest("products"), RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse.isAcknowledged());
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM