<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [TOC] ## 2. java操作ElaticSearch ### 2.1 Java鏈接ES ~~~Xml 1、創建Maven工程 導入依賴 # 4個依賴 1、1 elasticsearch <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.5.4</version> </dependency> 1、2 elasticsearch的高級API <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.5.4</version> </dependency> 1、3 junit <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> 1、4 lombok <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> ~~~ #### 2.1.2 創建測試類,連接ES ~~~java // 先創建連接,工具類 public class ESClient { public static RestHighLevelClient getClient(){ // 創建HttpHost對象 HttpHost httpHost = new HttpHost("127.0.0.1",9200); // 創建RestClientBuilder RestClientBuilder builder = RestClient.builder(httpHost); // 創建RestHighLevelClien對象 RestHighLevelClient client = new RestHighLevelClient(builder); return client; } } ~~~ ### 2.2 java創建索引 ~~~java import com.dengzhou.utils.ESClient; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.junit.jupiter.api.Test; import java.io.IOException; public class Create_ES_Index { String index = "person"; String type = "man"; @Test public void createIndex() throws IOException { //1、 準備關于索引的settings Settings.Builder settings = Settings.builder() .put("number_of_shards", 3) .put("number_of_replicas", 1); //2、 準備關于索引的結構mappings XContentBuilder mappings = JsonXContent.contentBuilder() .startObject() .startObject("properties") .startObject("name") .field("type","text") .endObject() .startObject("age") .field("type","integer") .endObject() .startObject("birthday") .field("type","date") .field("format","yyyy-MM-dd") .endObject() .endObject() .endObject(); //2 將settings 和 mappings封裝成一個request對象 CreateIndexRequest request = new CreateIndexRequest(index) .settings(settings) .mapping(type,mappings); //3 通過client對象去鏈接es并執行創建索引 RestHighLevelClient client = ESClient.getClient(); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); //測試 System.out.println("response"+response.toString()); } ~~~ ### 2.3 檢查索引是否存在,刪除索引 ~~~java //檢查索引是否存在 @Test public void exists() throws IOException { //1 準備request對象 GetIndexRequest request = new GetIndexRequest(); request.indices(index); // 2 通過client去檢查 RestHighLevelClient client = ESClient.getClient(); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } ~~~ ### 2.4 修改文檔 - <u>添加文檔操作</u> ~~~java @Test public void createDoc() throws IOException { ObjectMapper mapper = new ObjectMapper(); // 1. 準備json數據 Person person = new Person(1, "張三", 23, new Date()); String json = mapper.writeValueAsString(person); System.out.println(json); // 2. 準備一個request對象(手動指定id創建) IndexRequest indexRequest = new IndexRequest(index,type,person.getId().toString()); indexRequest.source(json, XContentType.JSON); // 3、通過client對象執行添加操作 RestHighLevelClient client = ESClient.getClient(); IndexResponse resp = client.index(indexRequest, RequestOptions.DEFAULT); // 4、 輸出返回 System.out.println(resp.getResult().toString()); } ~~~ - <u>修改文檔</u> ~~~java // 修改文檔,通過doc方式 @Test public void updateDoc() throws IOException { // 創建map,指定需要修改的內容 Map<String,Object> map = new HashMap<String, Object>(); map.put("name","李四"); String docId = "1"; // 創建一個request對象,封裝數據 UpdateRequest updateRequest = new UpdateRequest(index,type,docId); updateRequest.doc(map); // 通過client對象執行 RestHighLevelClient client = ESClient.getClient(); UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT); // 返回輸出結果 System.out.println(update.getResult().toString()); } ~~~ ### 2.5 刪除文檔 ### 2.6 java批量操作文檔 - ~~~json ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看