elasticsearch5.4.0 java开发记录一
来源:互联网 发布:lr软件怎么使用 编辑:程序博客网 时间:2024/06/10 21:15
本次的项目是使用的技术体系有: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;
- elasticsearch5.4.0 java开发记录一
- elasticsearch5.4.0 java开发记录二
- elasticsearch5.4.0 java开发记录三
- elasticsearch5.4.0 java开发记录四
- elasticsearch5.4.0 java开发注意事项
- elasticsearch5.4.0 查询记录
- elasticsearch5.2.2 插件开发(一)
- solr项目中添加elasticsearch5.4.0记录
- elasticsearch5.4.0 kibana小问题记录
- ElasticSearch5.x踩坑记录(一):windows系统下的ElasticSearch5.6.3及head插件安装
- ElasticSearch5.0使用记录
- ElasticSearch5.3插件开发(一)控制台打印信息
- java curl开发记录(一)
- 【JAVA大作业开发记录(一)】
- elasticsearch5.1.2安装过程记录
- ElasticSearch5.X尝试聚合(一)
- AM1808 开发记录(一)
- DM8148开发记录 一
- 2.SQL SERVER笔记——SQL SERVER系统概念
- NYOJ-ACM-A+B Problem II
- ARKit开发-准备阶段(SDK下载)
- flume抽取mysql数据至hdfs
- linux编译
- elasticsearch5.4.0 java开发记录一
- [java]微服务架构连载No3 Ribbon+Retry服务实现负载均衡和服务请求重试
- 1.Linux中超频及cpufreq相关汇总
- freeCodeCamp:jQuery
- webuploader的使用,mark一下
- pycharm中虚拟环境搭建
- The Heaviest Non-decreasing Subsequence Problem
- Spring-Boot整合freemarker引入静态资源css、js等
- IOS移动端1px解决方案