es之java索引操作


1.7.1: 創建索引

/**
 * 創建索引
 * */
@Test
public void createIndex(){
    // 創建索引
    CreateIndexResponse blog2 = client.admin().indices().prepareCreate("blog2").get();
    System.out.println(blog2.toString());

}

默認創建好索引,mappings為空

1.7.2: 刪除索引

/**
 * 刪除索引
 * */
@Test
public void deleteIndex(){
    // 刪除索引
    client.admin().indices().prepareDelete("blog2").get();
}

1.7.3:索引的映射操作

為什么要進行手動的映射?

在實際生產中經常會出現精度損失的現象,往往就是因為沒有進行正確的索引映射或者壓根就沒進行索引映射
Elasticsearch最開始索引文檔A,其中的一個字段是一個數字,是整數;通過自動類型猜測,並設置類型為整型(integer)或者長整型;
然后在索引另一個文檔B,B文檔在同一個字段中存儲的是浮點型;那么這個時候elasticsearch就會把B文檔中的小數刪除,保留整數部分;
這樣就會導致數據的不准確!

如果你習慣SQL數據庫,或許知道,在存入數據前,需要創建模式來描述數據(schmal);盡管elasticsearch是一個無模式的搜索引擎,可以即時算出數據結構;

但是我們仍然認為由自己控制並定義結構是更好的;而且在實際的生產中,我們也是自己創建映射;

注意:注意創建mapping的時候,索引必須提前存在

{
"settings":{
   "nshards":3,
   "number_of_repli umber_of_cas":1
},
"mappings":{
   "dahan":{
       "dynamic":"strict",
       "properties":{
           "studentNo":{"type": "string", "store": true},
           "name":{"type": "string","store": true,"index" : "analyzed","analyzer": "ik_max_word"},
           "male":{"type": "string","store": true},
           "age":{"type": "integer","store": true},
           "birthday":{"type": "string","store": true},
           "classNo":{"type": "string","store": true},
           "address":{"type": "string","store": true,"index" : "analyzed","analyzer": "ik_max_word"},
           "isLeader": {"type": "boolean", "index": "not_analyzed"}
       }
    }
}

1):創建索引

/**
* 創建索引
* */
@Test
public void createIndex(){
   
   CreateIndexResponse blog2 = client.admin().indices().prepareCreate("sanguo").get();
   System.out.println(blog2.toString());

}

2):通過代碼創建索引配置信息(有錯的情況)

/**
* Created by angel;
*/
public class CreateMappings {
   public static void main(String[] args) throws UnknownHostException {
       TransportClient client = null;
       Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("number_of_shards", 3);
       map.put("number_of_replicas", 1);
       Settings settings = Settings.builder()
              .put("cluster.name", "cluster")
              .build();
       client = new PreBuiltTransportClient(settings)
              .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop01"), 9300));
       System.out.println("========連接成功=============");


       XContentBuilder builder = null;
       try {
           builder = jsonBuilder()
                  .startObject()
                  .startObject("dahan").field("dynamic", "true")
                  .startObject("properties")
                  .startObject("studentNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("name").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()//.field("analyzer", "ik")
                  .startObject("male").field("type", "string").field("store", "yes").endObject()//.field("analyzer", "ik")
                  .startObject("age").field("type", "integer").field("store", "yes").endObject()
                  .startObject("birthday").field("type", "string").field("store", "yes").endObject()
                  .startObject("classNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("address").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()
                  .startObject("isLeader").field("type", "boolean").field("store", "yes").field("index", "not_analyzed").endObject()
                  .endObject()
                  .endObject()
                  .endObject();
           PutMappingRequest mapping = Requests.putMappingRequest("sanguo")
                  .type("dahan")
                  .source(builder);
           UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest();
           updateSettingsRequest.settings(map);
           client.admin().indices().updateSettings(updateSettingsRequest).actionGet();
           client.admin().indices().putMapping(mapping).get();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
}

如果有同學學習的比較扎實,那么會記住以下內容:

當索引不存在的時候,可以指定副本數和分片數

當索引存在的時候,只能指定副本數

所以,我們在構建索引的時候就要指定確定的分片和副本:

1):創建索引的時候直接指定索引的shard數和副本數

/**
* 創建索引
* */
@Test
public void createIndex(){
   // 創建索引
   Map<String, Integer> map = new HashMap<String, Integer>();
   map.put("number_of_shards", 3);
   map.put("number_of_replicas", 1);
   CreateIndexResponse blog2 = client.admin().indices().prepareCreate("sanguo").setSettings(map).get();
   System.out.println(blog2.toString());

}

2):然后在創建映射信息的時候,就可以忽略掉分片數和副本數了。直接進行索引的映射:

/**
* Created by angel;
*/
public class CreateMappings {
   public static void main(String[] args) throws UnknownHostException {
       TransportClient client = null;
       Settings settings = Settings.builder()
              .put("cluster.name", "cluster")
              .build();
       client = new PreBuiltTransportClient(settings)
              .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop01"), 9300));
       System.out.println("========連接成功=============");


       XContentBuilder builder = null;
       try {
           builder = jsonBuilder()
                  .startObject()
                  .startObject("dahan").field("dynamic", "true")
                  .startObject("properties")
                  .startObject("studentNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("name").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()//.field("analyzer", "ik")
                  .startObject("male").field("type", "string").field("store", "yes").endObject()//.field("analyzer", "ik")
                  .startObject("age").field("type", "integer").field("store", "yes").endObject()
                  .startObject("birthday").field("type", "string").field("store", "yes").endObject()
                  .startObject("classNo").field("type", "string").field("store", "yes").endObject()
                  .startObject("address").field("type", "string").field("store", "yes").field("analyzer", "ik_max_word").endObject()
                  .startObject("isLeader").field("type", "boolean").field("store", "yes").field("index", "not_analyzed").endObject()
                  .endObject()
                  .endObject()
                  .endObject();
           PutMappingRequest mapping = Requests.putMappingRequest("sanguo")
                  .type("dahan")
                  .source(builder);
           client.admin().indices().putMapping(mapping).get();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
}

 

 


免責聲明!

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



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