elasticsearch5.4.0 java开发记录一

本次的项目是使用的技术体系有:Spring3.1.1+ Spring mvc3.1.1 +mybatis3.1.1 +oracle + maven + elasticsearch + jdk1.8+slf4j 技术体系新的东西只有es

本次系列主要是用来记录 elasticsearch API java 项目中使用,方便自己记忆。


项目采用maven构建,该项目主要用来方便运营人员从商品库中筛选商品做卖场,及分析销售的商品数据。

项目比较简单分为core,web端,数据表几张

web界面采用freemarker UI

core 提供服务

第一步,清楚项目的需求,目的,项目也很简单,也不用什么开发文档,了解需求就进行实现了。

maven 配置贴下主要是 es的,常用框架的配置不展示了

       <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.4.0</version>
        </dependency>

    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>

----------------------------------------------------分割线-------------------------------------------------------------------------

先大致讲下本次项目中使用elasticsearch的步骤:

1,获取elasticsearch的客户端TransportClient

2,建立索引的setting及mapping

3,分页获取数据批量建立索引及索引别名

4,索引查询包含 简单查询termsQuery、组合查询BooleanQuery、区间查询rangeQuery、聚合agg、排序sort、分页scroll、高亮hightlight、关键字搜索keywords、拼音搜索

有位大神很形象的描述使用Elasticsearch 的过程,建立索引 index 相当于我们建立了一个库 db, 建立索引类型 type 相当于我们建立库db 中的表table

索引类型中的document文档 就相当于我们表中的一行记录,文档中所包含的字段就是我们表中的每一列,文档字段的类型就是我们表中列字段的类型。

使用elasticsearch5.4.0 第一步是获取到 代码如下 单利模式:

public class ElasticSearchUtils {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ElasticSearchUtils.class);

	private ElasticSearchUtils() {
	}

	private volatile static TransportClient client;
	

	/**
	 * 获取es 客户端
	 * 
	 * @param hosts
	 *            集群IP地址
	 * @return 客户端实例
	 */
	public static TransportClient getClient(String[] hosts) throws Exception{

		if (client == null) {
			synchronized (ElasticSearchUtils.class) {
				if (client == null) {
					Settings settings = Settings.builder()
					        .put("cluster.name", "elasticsearch_lyl").build();
					client = new PreBuiltTransportClient(settings);
					if (hosts.length > 0) {
						for (String host : hosts) {
						try {
						     client = client.addTransportAddress(
                                                           new InetSocketTransportAddress(InetAddress.getByName(host),9300));
						  } catch (UnknownHostException e) {
								LOGGER.error("初始化客户端client异常:【{}】.",e);
								throw e;
						  }
						}
					}
				}
			}
		}
		return client;
	}

	public static IndicesAdminClient getIndicesAdminClient(
			TransportClient client) {
		return client.admin().indices();
	}

	/**
	 * 创建索引 indexName 相当于创建数据库 indexName
	 * 
	 * @param client
	 * @param indexName
	 * @return
	 */
	public static boolean createIndex(TransportClient client, String indexName) {
		if (!isIndexExists(client, indexName)) {
			CreateIndexResponse response = getIndicesAdminClient(client)
					.prepareCreate(indexName.toLowerCase()).get();
			return response.isAcknowledged();
		}
		LOGGER.info("该索引名称:【{}】已存在.", indexName.toLowerCase());
		return Boolean.FALSE;
	}

	/**
	 * 为索引创建别名
	 * 
	 * @param client
	 * @param index
	 * @param alias
	 * @return
	 */
	public static boolean addAliasIndex(TransportClient client,
			String indexName, String aliasNmae) {
			IndicesAliasesResponse response = getIndicesAdminClient(client)
					.prepareAliases().addAlias(indexName, aliasNmae).get();
			LOGGER.info("addAliasIndex 时,索引【{}】,添加别名【{}】状态:【{}】", indexName,
					aliasNmae, response.isAcknowledged());
		return response.isAcknowledged();
	}
/**
	 * 判断别名是否存在
	 * 
	 * @param client
	 * @param aliases
	 * @return
	 */
	public static boolean isAliasExist(TransportClient client,
			String... aliases) {
		AliasesExistResponse response = getIndicesAdminClient(client)
				.prepareAliasesExist(aliases).get();
		return response.isExists();
	}

	/**
	 * //索引删除别名
	 * 
	 * @param client
	 * @param indexName
	 * @param aliasNmae
	 * @return
	 */
	public static boolean removeAliasIndex(TransportClient client,
			String indexName, String aliasNmae) {
		if (isIndexExists(client, indexName)) {
			IndicesAliasesResponse response = getIndicesAdminClient(client)
					.prepareAliases().removeAlias(indexName, aliasNmae).get();
			return response.isAcknowledged();
		}
		LOGGER.info("removeAliasIndex时,该索引名称:【{}】不存在.", indexName.toLowerCase());
		return Boolean.FALSE;

	}

	/**
	 * 创建自定义mapping的索引 前提是得先要创建索引 建立mapping (相当于建立表结构)
	 * 
	 * @param client
	 * @param indexName
	 * @param typeName
	 * @param mapping
	 * @return
	 */
	public static boolean setIndexMapping(TransportClient client,
			String indexName, String typeName, String mapping) {
		PutMappingResponse response = getIndicesAdminClient(client)
				.preparePutMapping(indexName.toLowerCase()).setType(typeName)
				.setSource(mapping, XContentType.JSON).get();
		return response.isAcknowledged();
	}

	/**
	 * 索引是否存在
	 * 
	 * @param client
	 * @param indexName
	 * @return
	 */
	public static boolean isIndexExists(TransportClient client, String indexName) {
		IndicesExistsRequest request = new IndicesExistsRequest(
				indexName.toLowerCase());
		IndicesExistsResponse response = getIndicesAdminClient(client).exists(
				request).actionGet();
		return response.isExists();
	}

	/**
	 * 删除索引
	 * 
	 * @param client
	 * @param indexName
	 * @return
	 */
	public static boolean deleteIndex(TransportClient client, String indexName) {
		if (isIndexExists(client, indexName)) {
			DeleteIndexResponse response = getIndicesAdminClient(client)
					.prepareDelete(indexName.toLowerCase()).get();
			return response.isAcknowledged();
		}
		LOGGER.info("deleteIndex时,该索引名称:【{}】不存在.", indexName.toLowerCase());
		return Boolean.FALSE;
	}
  }

   第一部分就完成,不足之处,错误之处望指出;
   
   第二部分是设置setting 其实创建client的时候已经进行了简单的设计setting, 及设置mapping;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值