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:索引的映射操做

爲何要進行手動的映射?json

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

若是你習慣SQL數據庫,或許知道,在存入數據前,須要建立模式來描述數據(schmal);儘管elasticsearch是一個無模式的搜索引擎,能夠即時算出數據結構;數據結構

可是咱們仍然認爲由本身控制並定義結構是更好的;並且在實際的生產中,咱們也是本身建立映射;app

注意:注意建立mapping的時候,索引必須提早存在elasticsearch

{
"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):建立索引oop

/**
* 建立索引
* */
@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();
      }
  }
}

若是有同窗學習的比較紮實,那麼會記住如下內容:ui

當索引不存在的時候,能夠指定副本數和分片數搜索引擎

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

因此,咱們在構建索引的時候就要指定肯定的分片和副本:

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();
      }
  }
}

相關文章
相關標籤/搜索