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

相关文章
相关标签/搜索