INDEX - TuPengXiong/TuPengXiong.github.io GitHub Wiki


package tpx.elasticsearch;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;

public class Index {

	TransportClient client;
	
	public static final String IP = "tpxsoft.wicp.net";
	public static final int PORT = 9300;
	
	
	public static void main(String[] args) {
		Index test = new Index();
		test.createTransportClient();
		IndexResponse indexResponse = test.createIndexJson();
		printResponse(indexResponse);
		indexResponse = test.createIndexMap();
		printResponse(indexResponse);
		indexResponse = test.createIndexBuilder();
		printResponse(indexResponse);
		test.closeTransportClient();
	}
	
	/**
	 * 创建TransportClient
	 */
	public void createTransportClient() {
		Settings settings = Settings.settingsBuilder()
				.put("cluster_tpx", "node_tpx")
				.put("client.transport.ignore_cluster_name", true)
				.put("client.transport.ping_timeout","10s")
				.put("client.transport.nodes_sampler_interval", "10s").build();
		client = TransportClient.builder().settings(settings).build();
		try {
			client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭
	 */
	public void closeTransportClient(){
		if(client!= null){
			client.close();
		}
	}

	/**
	 * @return
	 */
	public IndexResponse createIndexJson() {
		String json = "{" + "\"user\":\"kimchy\","
				+ "\"postDate\":\"2013-01-30\","
				+ "\"message\":\"trying out Elasticsearch\"" + "}";

		IndexResponse response = client.prepareIndex("tpx", "testJson")
				.setSource(json).get();
		return response;
	}

	/**
	 * 
	 * @return
	 */
	public IndexResponse createIndexMap() {
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("user", "kimchy");
		json.put("postDate", new Date());
		json.put("message", "trying out Elasticsearch");
		IndexResponse response = client.prepareIndex("tpx", "testMap")
				.setSource(json).get();
		return response;
	}
	
	public IndexResponse createIndexBuilder(){
		IndexResponse response = null;
		try {
			response = client.prepareIndex("tpx", "testBuilder", "1")
			        .setSource(XContentFactory.jsonBuilder()
			                    .startObject()
			                        .field("user", "kimchy")
			                        .field("postDate", new Date())
			                        .field("message", "trying out Elasticsearch -- testBuilder")
			                        .field("tpx", "tpx")
			                    .endObject()
			                  )
			        .get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 
	 * @param response
	 */
	public static void printResponse(IndexResponse response){
		// Index name
		String _index = response.getIndex();
		System.out.println("_index:"+_index);
		// Type name
		String _type = response.getType();
		System.out.println("_type:"+_type);
		// Document ID (generated or not)
		String _id = response.getId();
		System.out.println("_id:"+_id);
		// Version (if it's the first time you index this document, you will get: 1)
		long _version = response.getVersion();
		System.out.println("_version:"+_version);
		// isCreated() is true if the document is a new one, false if it has been updated
		boolean created = response.isCreated();
		System.out.println("created:"+created);
		
		System.out.println("===============================================");
	}
}