ElasticSearch javaApi 增删改查

 pom.xml文件

    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<elasticsearch.version>5.6.3</elasticsearch.version>
    </properties>

	<dependencies>
		<!-- 测试类 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<!-- elasticsearch -->
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>${elasticsearch.version}</version>
		</dependency>
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>transport</artifactId>
			<version>${elasticsearch.version}</version>
		</dependency>

		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.8.6</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.6</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.8.0</version>
		</dependency>

		<!-- log4j -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.9.1</version>
		</dependency>

	</dependencies>

 ESUtils.java

package com.es.operation;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/**
 * package : com.es.operation
 * type: ESUtils
 * <p>Description: ES工具类</p>
 * @author : zxz
 * @date : 2018年11月21日
 */
public class ESUtils {

	private final static Logger logger = LogManager.getLogger(ESUtils.class.getName());
	private static TransportClient client = null;
	private static BulkRequestBuilder bulkRequest = null;

	public static void main(String[] args) throws Exception {
		String clusterName = "logos-bigdata" ;
		String host = "192.168.6.241" ;
		int port = 9300 ;

		String indexName = "cms" ;
		String type = "article" ;

		client = getEsClient(clusterName, host, port) ;
		
		/*// 批量插入数据
		List<Map<String,String>> list = new ArrayList<Map<String,String>>() ;
		Map<String,String> map = new HashMap<String,String>() ;
		map.put("title", "es权威指南") ;
		map.put("body", "讲解es") ;
		list.add(map) ;
		bulkContents(client, indexName, type, list) ;*/
		
		//查询所有数据
		SearchHits hits = matchAllContent(client,indexName) ;
		for (SearchHit hit : hits) {
			logger.info("id " + hit.getId());
			Map<String, Object> source = hit.getSourceAsMap() ;
			for(final Entry<String, Object> entry : source.entrySet()) {
				logger.info(entry.getKey() + ": " + entry.getValue());
			}
		}
		
		client.close();
	}

	// 定义索引结构
	public static XContentBuilder getMapping(String indexType) throws IOException {
		XContentBuilder mapping = XContentFactory.jsonBuilder()
				.startObject().startObject(indexType).startObject("properties") 
				.startObject("title")
				.field("type", "string").field("store", "yes").field("analyzer","standard")
				.endObject()
				.startObject("body")
				.field("type", "string").field("store","yes").field("analyzer", "standard")
				.endObject()
				.endObject().endObject().endObject() ;
		logger.info(mapping.bytes().utf8ToString());
		return mapping;
	}

	/**
	 * <p>Title: matchAllContent</p>
	 * <p>Description: 查询索引中全部数据数据</p>
	 * @param indexName 索引名称
	 */
	public static SearchHits matchAllContent(TransportClient client, String indexName) {
		MatchAllQueryBuilder qb = QueryBuilders.matchAllQuery() ;
		SearchResponse searchResponse = client.prepareSearch(indexName).setQuery(qb).get() ;
		return searchResponse.getHits() ;
	}

	/**
	 * <p>Title: matchKeywordContent</p>
	 * <p>Description: 基本的关键词查询</p>
	 * @param indexName 索引名称
	 * @param keyword 关键词
	 */
	public static SearchHits matchKeywordContent(TransportClient client, String indexName, String keyword) {
		QueryStringQueryBuilder qb = new QueryStringQueryBuilder(keyword) ;
		SearchResponse searchResponse = client.prepareSearch(indexName).setQuery(qb).execute().actionGet() ;
		return searchResponse.getHits() ;
	}

	/**
	 * <p>Title: selectFields</p>
	 * <p>Description: 限定返回的列</p>
	 * @param indexName
	 * @param field
	 */
	public static SearchHits selectFields(TransportClient client, String indexName, String field) {
		SearchResponse searchResponse = client.prepareSearch(indexName).setFetchSource(field, null).get() ;
		return searchResponse.getHits() ;
	}

	/**
	 * <p>Title: specifiedDoc</p>
	 * <p>Description: 返回指定的文档</p>
	 * @param indexName 索引名称
	 * @param type 类型
	 * @param id 编号
	 */
	public static Map<String, Object> specifiedDoc(TransportClient client, String indexName, String type, String id) {
		GetResponse getResponse = client.prepareGet(indexName, type, id).execute().actionGet() ;
		return getResponse.getSourceAsMap();
	}

	/**
	 * <p>Title: searchByPage</p>
	 * <p>Description: 分页查询</p>
	 * @param indexName 索引名称
	 * @param rows 返回条数
	 * @param offset 开始条数
	 */
	public static SearchHits searchByPage(TransportClient client, String indexName, int rows, int offset) {
		SearchResponse searchResponse = client.prepareSearch(indexName).setFrom(offset).setSize(rows).get() ; // 设置分页查询
		SearchHits searchHits = searchResponse.getHits() ;
		
		long totalHits = searchHits.getTotalHits() ; // 得到结果总数
		logger.info(totalHits);
		
		return searchHits ;
	}

	/**
	 * <p>Title: highLightShow</p>
	 * <p>Description: 高亮显示</p>
	 * @param indexName 索引名称
	 * @param field 高亮列/查询列
	 * @param keyword 查询关键词/参数
	 */
	public static void highLightShow(TransportClient client, String indexName, String field, String keyword) {

		// 高亮标签
		HighlightBuilder highBuilder = new HighlightBuilder() ;
		highBuilder.preTags("<span style=\"color:red\">") ;
		highBuilder.postTags("</span>") ;

		//指定高亮字段
		highBuilder.field(field) ;

		MatchQueryBuilder qb = QueryBuilders.matchQuery(field, keyword) ;
		SearchResponse searchResponse = client.prepareSearch(indexName).setQuery(qb).highlighter(highBuilder).get();
		SearchHits searchHits = searchResponse.getHits() ;
		for (SearchHit hit : searchHits) {
			logger.info("id " + hit.getId());
			Map<String, HighlightField> highlightFields = hit.getHighlightFields();
			for (final Entry<String, HighlightField> entry : highlightFields.entrySet()) {
				logger.info(entry.getKey() + ": " + entry.getValue().getFragments()[0]);
			}
		}
	}

	/**
	 * <p>Title: insertContent</p>
	 * <p>Description: 导入单条数据</p>
	 * @param indexName 索引名称
	 * @param type 类型
	 * @param id 编号
	 * @param source 文档内容
	 */
	public static void insertContent(TransportClient client, String indexName, String type, String id, Map<String,String> source) {

		IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, type, id) ;
		// 添加文档内容
		indexRequestBuilder.setSource(source) ;

		IndexResponse response = indexRequestBuilder.execute().actionGet() ;
		logger.info("执行结果: " + response.status().name());
	}

	/**
	 * <p>Title: bulkContents</p>
	 * <p>Description: 批量插入数据</p>
	 * @param indexName 索引名称
	 * @param type 类型
	 * @param list 数据列表
	 */
	public static void bulkContents(TransportClient client, String indexName, String type, List<Map<String,String>> list) {
		bulkRequest = client.prepareBulk();
		IndexRequestBuilder prepareIndex = client.prepareIndex(indexName, type);
		for (Map<String, String> source : list) {
			if (bulkRequest.numberOfActions() != 0 && (bulkRequest.numberOfActions() % 500 == 0)) {
				bulkRequest(client) ;
			}
			bulkRequest.add(prepareIndex.setSource(source)) ;
		}
		if (0 < bulkRequest.numberOfActions()) {
			bulkRequest(client) ;
		}
	}

	private static void bulkRequest(TransportClient client) {
		BulkResponse bulkResponse = bulkRequest.execute().actionGet() ;
		logger.info("执行结果: " + bulkResponse.status().name());
		if (!bulkResponse.hasFailures()) {
			bulkRequest = client.prepareBulk();
		}
	}

	/**
	 * <p>Title: updateContent</p>
	 * <p>Description: 更新单条数据</p>
	 * @param indexName 索引名称
	 * @param type 类型
	 * @param id 编号
	 * @param source 更新文档内容
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static void updateContent(TransportClient client, String indexName, String type, String id, XContentBuilder source) throws InterruptedException, ExecutionException{

		UpdateRequest updateRequest = new UpdateRequest() ;
		updateRequest.index(indexName) ;
		updateRequest.type(type) ;
		updateRequest.id(id) ;
		updateRequest.doc(source) ;
		UpdateResponse response = client.update(updateRequest).get() ;

		logger.info("执行结果: " + response.status().name());
	}

	/**
	 * <p>Title: deleteContent</p>
	 * <p>Description: 删除单条数据</p>
	 * @param indexName 索引名称
	 * @param type 类型
	 * @param id 编号
	 */
	public static void deleteContent(TransportClient client, String indexName, String type, String id) {

		DeleteResponse response = client.prepareDelete(indexName, type, id).get() ;
		logger.info("执行结果: " + response.status().name());
	}

	/**
	 * <p>Title: deleteIndex</p>
	 * <p>Description: 删除索引 </p>
	 * @param indexName 索引名称
	 */
	public static void deleteIndex(TransportClient client, String indexName) {

		IndicesAdminClient admin = client.admin().indices() ;
		DeleteIndexResponse actionGet = admin.prepareDelete(indexName).execute().actionGet();

		logger.info("DeleteIndexResponse " + actionGet.isAcknowledged());
	}

	/**
	 * <p>Title: createIndex</p>
	 * <p>Description: 创建索引并设置mapping</p>
	 * @param indexName 索引名称
	 * @param type 类型名称
	 * @param mapping 映射
	 */
	public static void createIndex(TransportClient client, String indexName, String type, XContentBuilder mapping) {

		IndicesAdminClient ac = client.admin().indices() ;
		// 设置分片数量
		Builder setting = Settings.builder().put("number_of_shards", 5).put("number_of_replicas", 1) ;
		// 首先创建索引库
		CreateIndexResponse indexResponse = ac.prepareCreate(indexName).setSettings(setting.build()).execute().actionGet() ;
		logger.info("CreateIndex "+indexResponse.isAcknowledged());

		PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(type).source(mapping) ;
		PutMappingResponse putMappingResponse = ac.putMapping(mappingRequest).actionGet() ;

		// 看是否成功设定索引结构
		logger.info("putMappingResponse " + putMappingResponse.isAcknowledged());
	}

	/**
	 * <p>Title: getEsClient</p>
	 * <p>Description: 获取客户端 </p>
	 * @param clusterName 集群名称
	 * @param serverHost 主机名
	 * @param serverPort 端口号
	 * @return
	 * @throws UnknownHostException
	 */
	public static TransportClient getEsClient(String clusterName, String serverHost, int serverPort) 
			throws UnknownHostException {
		if(client == null) {
			Settings settings = Settings.builder()
					.put("cluster.name", clusterName)
					.build() ;

			InetSocketTransportAddress node = new InetSocketTransportAddress(
					InetAddress.getByName(serverHost),
					serverPort) ;

			client = new PreBuiltTransportClient(settings) ;
			client.addTransportAddress(node) ;
			logger.info("创建客户端。") ;
		}
		return client ;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值