ElasticSearch(6)--使用Java客戶端創建索引和映射

手動創建映射(包含創建映射和文檔):

 

package com.es.querydemo;
 
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
 
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import com.es.bean.Product;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * 查詢
 * 
 * @author Beck
 * @date 2018年2月6日
 */
public class TestESQuery {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 9300;
    
    private static final String INDEX = "eshop";
    private static final String TYPE = "product";
    
    private static final ObjectMapper MAPPER = new ObjectMapper();
    
    private TransportClient client = null;
    
    // 創建索引
    private void createIndex(){
        this.client.admin().indices().prepareCreate(INDEX).get();
    }
    
    // 刪除索引
    private void deleteIndex(){
        this.client.admin().indices().prepareDelete(INDEX).get();
    }
    
    // 新增一個文檔
    private void createDocument() throws JsonProcessingException{
        Product product = new Product();
        product.setId(5);
        product.setTitle("三星(SAMSUNG)UA78KU6900JXXZ 78英寸 曲面 4K 超高清 智能電視 黑色");
        
        String source = MAPPER.writeValueAsString(product);
        // 新增
        this.client.prepareIndex(INDEX, TYPE).setSource(source).get();
    }
    
    // 創建映射
    private void createMapping() throws Exception{
        // 配置映射關係
        Map<String,Object> mappings = new HashMap<>();
        
        Map<String,Object> type = new HashMap<>();
        mappings.put(TYPE, type);
        type.put("dynamic", false);
        
        Map<String,Object> properties = new HashMap<>();
        type.put("properties", properties);
        
        // 文檔的id映射
        Map<String,Object> idProperties = new HashMap<>();
        idProperties.put("type", "integer");
        idProperties.put("store", "yes");
        properties.put("id", idProperties);
        
        // 文檔的title映射
        Map<String,Object> titleProperties = new HashMap<>();
        titleProperties.put("type", "string");
        titleProperties.put("store", "yes");
        titleProperties.put("analyzer", "ik");
        properties.put("title", titleProperties);
        
        String json = MAPPER.writeValueAsString(mappings);
        System.out.println(json);
        
        PutMappingRequest request = Requests.putMappingRequest(INDEX).type(TYPE).source(json);
        this.client.admin().indices().putMapping(request).get();
    }
    @Test
    public void execute(){
        try {
            createDocument();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 獲取客戶端
    @Before
    public void getClient() throws Exception{
        client = TransportClient.builder()
        .build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
    }
    
    // 關閉客戶端
    @After
    public void closeClient(){
        if (this.client != null){
            this.client.close();
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章