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、效果圖

 


免責聲明!

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



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