1. 程式人生 > 實用技巧 >ES之Mapping對映建立

ES之Mapping對映建立

1、AdminAPI.java

package es;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Before; import org.junit.Test;
import java.io.IOException; import java.net.InetAddress; import java.util.HashMap; public class AdminAPI { private TransportClient client = null; //在所有的測試方法之前執行 @Before public void init() throws Exception { //設定叢集名稱 Settings settings = Settings.builder().put("cluster.name", "bigdata").build();
//建立client client = new PreBuiltTransportClient(settings).addTransportAddresses( new InetSocketTransportAddress(InetAddress.getByName("192.168.33.100"), 9300), new InetSocketTransportAddress(InetAddress.getByName("192.168.33.101"), 9300), new InetSocketTransportAddress(InetAddress.getByName("192.168.33.102"), 9300)); } //建立索引,並配置一些引數 @Test public void createIndexWithSettings() { //獲取Admin的API AdminClient admin = client.admin(); //使用Admin API對索引進行操作 IndicesAdminClient indices = admin.indices(); //準備建立索引 indices.prepareCreate("gamelog") //配置索引引數 .setSettings( //引數配置器 Settings.builder()//指定索引分割槽的數量 .put("index.number_of_shards", 4) //指定索引副本的數量(注意:不包括本身,如果設定資料儲存副本為2,實際上資料儲存了3份) .put("index.number_of_replicas", 2) ) //真正執行 .get(); } //跟索引新增mapping資訊(給表新增schema資訊)------這種已不使用 @Test public void putMapping() { //建立索引 client.admin().indices().prepareCreate("twitter") //建立一個type,並指定type中屬性的名字和型別 .addMapping("tweet", "{\n" + " \"tweet\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " }\n" + " }") .get(); } /** * 你可以通過dynamic設定來控制這一行為,它能夠接受以下的選項: * true:預設值。動態新增欄位 * false:忽略新欄位 * strict:如果碰到陌生欄位,丟擲異常 * @throws IOException */ @Test public void testSettingsMappings() throws IOException { //1:settings HashMap<String, Object> settings_map = new HashMap<String, Object>(2); settings_map.put("number_of_shards", 3); settings_map.put("number_of_replicas", 2); //2:mappings(對映、schema) XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .field("dynamic", "true") //設定type中的屬性 .startObject("properties") //id屬性 .startObject("num") //型別是integer .field("type", "integer") //不分詞,但是建索引 .field("index", "not_analyzed") //在文件中儲存 .field("store", "yes") .endObject() //name屬性 .startObject("name") //string型別 .field("type", "string") //在文件中儲存 .field("store", "yes") //建立索引 .field("index", "analyzed") //使用ik_smart進行分詞 .field("analyzer", "ik_smart") .endObject() .endObject() .endObject(); CreateIndexRequestBuilder prepareCreate = client.admin().indices().prepareCreate("user_info"); //管理索引(user_info)然後關聯type(user) prepareCreate.setSettings(settings_map).addMapping("user", builder).get(); } /** * XContentBuilder mapping = jsonBuilder() .startObject() .startObject("productIndex") .startObject("properties") .startObject("title").field("type", "string").field("store", "yes").endObject() .startObject("description").field("type", "string").field("index", "not_analyzed").endObject() .startObject("price").field("type", "double").endObject() .startObject("onSale").field("type", "boolean").endObject() .startObject("type").field("type", "integer").endObject() .startObject("createDate").field("type", "date").endObject() .endObject() .endObject() .endObject(); PutMappingRequest mappingRequest = Requests.putMappingRequest("productIndex").type("productIndex").source(mapping); client.admin().indices().putMapping(mappingRequest).actionGet(); */ /** * index這個屬性,no代表不建索引 * not_analyzed,建索引不分詞 * analyzed 即分詞,又建立索引 * expected [no], [not_analyzed] or [analyzed] * @throws IOException */ @Test public void testSettingsPlayerMappings() throws IOException { //1:settings HashMap<String, Object> settings_map = new HashMap<String, Object>(2); settings_map.put("number_of_shards", 3); settings_map.put("number_of_replicas", 1); //2:mappings XContentBuilder builder = XContentFactory.jsonBuilder() .startObject()// .field("dynamic", "true") .startObject("properties") //資料欄位 .startObject("id") //欄位型別 .field("type", "integer") .field("store", "yes") .endObject() .startObject("name") .field("type", "string") .field("index", "not_analyzed") .endObject() .startObject("age") .field("type", "integer") .endObject() .startObject("salary") .field("type", "integer") .endObject() .startObject("team") .field("type", "string") .field("index", "not_analyzed") .endObject() .startObject("position") .field("type", "string") .field("index", "not_analyzed") .endObject() .startObject("description") .field("type", "string") .field("store", "no") .field("index", "analyzed") .field("analyzer", "ik_smart") .endObject() .startObject("addr") .field("type", "string") .field("store", "yes") .field("index", "analyzed") .field("analyzer", "ik_smart") .endObject() .endObject() .endObject(); //index CreateIndexRequestBuilder prepareCreate = client.admin().indices().prepareCreate("player_info"); //type prepareCreate.setSettings(settings_map).addMapping("player", builder).get(); } }

2、效果圖