/** * 建立索引 * */ @Test public void createIndex(){ // 建立索引 CreateIndexResponse blog2 = client.admin().indices().prepareCreate("blog2").get(); System.out.println(blog2.toString()); }
默認建立好索引,mappings爲空數據庫
/** * 刪除索引 * */ @Test public void deleteIndex(){ // 刪除索引 client.admin().indices().prepareDelete("blog2").get(); }
爲何要進行手動的映射?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"} } } }
/**
* 建立索引
* */
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();
}
}
}