ES16-JAVA API 索引管理

1.建立鏈接

建立鏈接demojava

package com.sean.esapi.client;

import java.net.InetSocketAddress;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ClientDemo {

	public static void main(String[] args) {
		// 設置集羣名稱
		Settings settings = Settings.builder().put("cluster.name", "secms-elk").build();

		// 建立client連接es
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(new InetSocketAddress("192.168.1.112", 9300)));
		//查詢指定文檔數據
		GetResponse response = client.prepareGet("sfpay_log", "waf", "1").get();
		//輸出結果
		System.out.println(response.getSourceAsString());
		//關閉連接
		client.close();

	}

}

2.索引管理

package com.sean.esapi.client;

import java.net.InetAddress;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ESIndexUtil {

	private static final String CLUSTER_NAME = "secms-elk";

	private static final String HOST = "192.168.1.112";

	private static final Integer PORT = 9300;

	private static Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();

	private static volatile TransportClient client;

	/**
	 * 獲取客戶端
	 * 
	 * @return
	 */
	public static TransportClient getClient() {
		if (client == null) {
			synchronized (ESIndexUtil.class) {
				try {
					client = new PreBuiltTransportClient(settings)
							.addTransportAddress(new TransportAddress(InetAddress.getByName(HOST), PORT));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return client;
	}

	/**
	 * 獲取索引管理客戶端
	 * 
	 * @return
	 */
	public static IndicesAdminClient getAdminClient() {
		return getClient().admin().indices();
	}

	/**
	 * 判斷索引是否存在
	 * 
	 * @param indexName
	 * @return
	 */
	public static boolean isExists(String indexName) {
		IndicesExistsResponse response = getAdminClient().prepareExists(indexName).get();
		return response.isExists();
	}

	/**
	 * 建立索引
	 * 
	 * @param indexName
	 * @return
	 */
	public static boolean createIndex(String indexName) {
		CreateIndexResponse response = getAdminClient().prepareCreate(indexName).get();
		return response.isAcknowledged();
	}

	/**
	 * 建立索引
	 * 
	 * @param indexName
	 *            索引名稱
	 * @param shards
	 *            分片數量
	 * @param replicas
	 *            備份分數
	 * @return
	 */
	public static boolean createIndex(String indexName, int shards, int replicas) {
		Settings settings = Settings.builder().put("index.number_of_shards", shards)
				.put("index.number_of_replicas", replicas).build();
		CreateIndexResponse response = getAdminClient().prepareCreate(indexName).setSettings(settings).execute()
				.actionGet();
		return response.isAcknowledged();
	}

	/**
	 * 爲索引設置mapping
	 * 
	 * @param indexName
	 *            索引名稱
	 * @param type
	 * @param mapping
	 */
	public static void setMapping(String indexName, String type, String mapping) {
		getAdminClient().preparePutMapping(indexName).setType(type).setSource(mapping, XContentType.JSON).get();
	}

	/**
	 * 刪除索引
	 * 
	 * @param indexName
	 * @return
	 */
	public static boolean deleteIndex(String indexName) {
		DeleteIndexResponse response = getAdminClient().prepareDelete(indexName).execute().actionGet();
		return response.isAcknowledged();
	}
}

demo測試json

package com.sean.esapi.client;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

public class IndexDemo {

	public static void main(String[] args) throws Exception {
		// 斷定索引是否存在
		boolean isExist = ESIndexUtil.isExists("sfpay_log");
		System.out.println(isExist);
		// 建立索引
		boolean isCreated = ESIndexUtil.createIndex("cailianpress", 5, 2);
		System.out.println(isCreated);
		// 設置mapping
		XContentBuilder builder = XContentFactory.jsonBuilder()
				.startObject()
				.startObject("properties")
				.startObject("title")
				.field("type","text")
				.field("analyzer","ik_max_word")
				.endObject()
				.startObject("content")
				.field("type","text")
				.field("analyzer","ik_max_word")
				.endObject()
				.startObject("pubdate")
				.field("type","date")
				.field("format","yyyy-MM-dd HH:mm")
				.endObject()
				.endObject()
				.endObject();
		System.out.println(builder.string());
		//設置mapping
		ESIndexUtil.setMapping("cailianpress","msg",builder.string());

	}
}
相關文章
相關標籤/搜索