因为项目开发使用的是Java语言, 项目的开发架构是Spring MVC+ maven的jar包管理,  所以今天重点说说ES 5.4.3 的Java API的源码实战

1. pom.xml文件增加依赖:

  <!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.4.3</version>
</dependency> <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.4.3</version>
</dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.6.2</version>
</dependency>

2. 首先我们创建一个获取Client 的Factory类,该类中的配置信息是配置在项目的.properties文件中

注意端口号通常为9300,这个是ES为java保留的默认端口号。

 package com.cs99lzzs.elasticsearch;

 import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ElasticsearchFactory { @Value("${elasticsearch.ips}")
private String ES_IPS; @Value("${elasticsearch.cluster.name}")
private String CLUSTER_NAME; @Value("${elasticsearch.port}")
private int ES_PORT; private static TransportClient transportClient = null; /**
* 获取esClient实例
* @return
*/
@Bean(name = "esClient")
public Client getESClient(){ /**
* 1:通过 setting对象来指定集群配置信息
*/
if (transportClient == null) {
Settings settings = Settings.builder()
.put("cluster.name", CLUSTER_NAME)
.put("client.transport.sniff", true)
.build(); transportClient = new PreBuiltTransportClient(settings); String esIps[] = ES_IPS.split(",");
for (String esIp : esIps) {//添加集群IP列表
TransportAddress transportAddress = new InetSocketTransportAddress(InetAddresses.forString(esIp), ES_PORT);
transportClient.addTransportAddresses(transportAddress);
}
}
return transportClient;
}
}

3. 索引创建的Controller类, 主要是2个定时任务:全量更新和增量更新

另外一个重点就是:创建mapping。 该mapping包含了一个搜索提示的字段

 package com.cs99lzzs.elasticsearch;

 import java.net.InetAddress;
import java.sql.Timestamp; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import redis.clients.jedis.Jedis; import com.showjoy.data.page.JedisClient;
import com.showjoy.elasticsearch.service.ProductIndexService; @Controller
public class CreateIndexController { private static Logger logger = Logger.getLogger(CreateIndexController.class); private final static String _REDIS_DB_STORE_TIME_KEY_NAME = "es.shop.index.update.time";
private final static String _PRODUCT_INDEX_TIME_KEY = "es.shop.product.time"; @Resource
private ProductIndexService productIndexService; @Resource(name="esClient")
Client esClient; @Value("${elasticsearch.index}")
private String CLUSTER_INDEX; @Value("${elasticsearch.type}")
private String CLUSTER_TYPE;
/**
*
* @author chenxu
* 2017年7月13日 下午6:25:54
* @param request
* @param response
* @return
*/
@ResponseBody
@RequestMapping(value={"/", "/info"}, method = RequestMethod.GET)
public String info(HttpServletRequest request, HttpServletResponse response) {
return "success";
} @ResponseBody
@RequestMapping(value = "/es/index", method = RequestMethod.GET)
public void productIndex(@RequestParam(required = false, defaultValue = "2016-01-05 00:00:00") String fromTime) {
productIndexService.createIndex(Timestamp.valueOf(fromTime));
} /**
* 创建mapping
* @author chenxu
* 2017年7月10日 下午2:10:24
* @param indices
* @param mappingType
* @throws Exception
*/
public static void createMapping(String index, String type, Client client)
throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject(type)
.startObject("properties");
//搜索字段: text类型, ik_max_word分词
builder.startObject("brandZhName").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
builder.startObject("brandEnName").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
builder.startObject("brandAliases").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
builder.startObject("aliases").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
builder.startObject("zhName").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
builder.startObject("enName").field("type", "text").field("analyzer", "ik_max_word").field("store", "yes").endObject();
//排序字段
builder.startObject("price").field("type", "float").field("store", "yes").endObject();//现价
builder.startObject("salesVolume").field("type", "integer").field("store", "yes").endObject();//销量
builder.startObject("commission").field("type", "float").field("store", "yes").endObject();//收益
//其他字段
/* integer */
builder.startObject("id").field("type", "integer").field("store", "yes").endObject();
builder.startObject("spuId").field("type", "integer").field("store", "yes").endObject();
builder.startObject("inventory").field("type", "integer").field("store", "yes").endObject();
builder.startObject("brandId").field("type", "integer").field("store", "yes").endObject();
builder.startObject("cateId").field("type", "integer").field("store", "yes").endObject();
builder.startObject("cateScope").field("type", "integer").field("store", "yes").endObject();
builder.startObject("vipShopId").field("type", "integer").field("store", "yes").endObject(); /* boolean */
builder.startObject("isDelete").field("type", "boolean").field("store", "yes").endObject();
builder.startObject("triable").field("type", "boolean").field("store", "yes").endObject();
builder.startObject("isTrialPack").field("type", "boolean").field("store", "yes").endObject();
builder.startObject("searchable").field("type", "boolean").field("store", "yes").endObject();
builder.startObject("isHaitao").field("type", "boolean").field("store", "yes").endObject();
builder.startObject("isSupplier").field("type", "boolean").field("store", "yes").endObject(); /* keyword */
builder.startObject("spuStatusId").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("image").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("isSuit").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("unit").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("sex").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("brandPathName").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("brandImage").field("type", "keyword").field("index", "no").field("store", "yes").endObject(); builder.startObject("brandName").field("type", "keyword").field("store", "yes").endObject();
builder.startObject("cateName").field("type", "keyword").field("store", "yes").endObject();
builder.startObject("cateNameTree").field("type", "keyword").field("store", "yes").endObject();
builder.startObject("salesPromotionTag").field("type", "keyword").field("store", "yes").endObject(); //促销标签 builder.startObject("perfumeType").field("type", "keyword").field("index", "no").field("store", "yes").endObject();
builder.startObject("brandScope").field("type", "keyword").field("index", "no").field("store", "yes").endObject(); /* float */
builder.startObject("volume").field("type", "float").field("store", "yes").endObject();
builder.startObject("originalPrice").field("type", "float").field("store", "yes").endObject();
builder.startObject("discount").field("type", "float").field("store", "yes").endObject(); /* date */
builder.startObject("gmtCreate").field("type", "date").field("store", "yes").endObject();
builder.startObject("gmtModified").field("type", "date").field("store", "yes").endObject(); /* suggester */
builder.startObject("suggestName").field("type","completion").endObject();
// builder.startObject("suggestBrandZhName").field("type","completion").field("analyzer","ik_max_word")
// .field("search_analyzer","ik_max_word").endObject(); //结束
builder.endObject().endObject().endObject(); PutMappingRequest mapping = Requests.putMappingRequest(index)
.type(type).source(builder);
client.admin().indices().putMapping(mapping).actionGet(); } /**
* 全量更新商品索引任务,凌晨4点执行
* @author chenxu
*
**/
@Scheduled(cron = "0 0 4 * * ?")
@ResponseBody
@RequestMapping(value = "/search/index/allproduct", method = RequestMethod.GET)
public void indexProducts(){
Timestamp updateTime = new Timestamp(0);
saveTimestamp(_PRODUCT_INDEX_TIME_KEY, new Timestamp(System.currentTimeMillis()));
productIndexService.createIndex(updateTime);
} /**
* 增量更新商品索引 ,每隔5分钟执行
* @author chenxu
*
**/
@Scheduled(cron = "0 */5 * * * ?")
@ResponseBody
@RequestMapping(value = "/search/index/product", method = RequestMethod.GET)
public void indexProduct() {
Timestamp updateTime = getTimestamp(_PRODUCT_INDEX_TIME_KEY);
long currentTimeMillis = System.currentTimeMillis();
productIndexService.createIndex(updateTime);
saveTimestamp(_PRODUCT_INDEX_TIME_KEY, new Timestamp(currentTimeMillis));
} private static Timestamp getTimestamp(String name) {
Timestamp timestamp = null;
try {
Jedis jedis = JedisClient.getJedis();
jedis.select(1);
String timestr = jedis.hget(_REDIS_DB_STORE_TIME_KEY_NAME, name);
JedisClient.returnResource(jedis);
if (StringUtils.isNotBlank(timestr)) {
timestamp = Timestamp.valueOf(timestr);
} else {
timestamp = new Timestamp(0);
}
} catch (Exception e) {
logger.error("elasticsearch获取索引最新更新时间失败,检查存储redis是否出现问题", e);
timestamp = new Timestamp(System.currentTimeMillis());
}
return timestamp;
} private void saveTimestamp(String name, Timestamp timestamp) {
try {
Jedis jedis = JedisClient.getJedis();
jedis.select(1);
jedis.hset(_REDIS_DB_STORE_TIME_KEY_NAME, name, timestamp.toString());
JedisClient.returnResource(jedis);
} catch (Exception e) {
logger.error("存储索引最新更新时间失败,检查存储redis是否出现问题", e);
}
} public static void main(String[] args) {
Settings settings = Settings.builder()
.put("cluster.name", "showjoy-shop-search")
.put("client.transport.sniff", true)
.build(); TransportClient tc = new PreBuiltTransportClient(settings);
try { tc.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.0.124"), 9300)); createMapping("item_index", "shop_item", tc);
// 去数据库中扫描达人店商品的待搜索字段
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Map<String, Object> source = new HashMap<String, Object>();
// source.put("name", "酒");
// source.put("cateId", 5);
// source.put("brandId", 15);
// source.put("salesVolume", 250);
// source.put("price", 55.00d);
// source.put("income", 5.5d);
// source.put("createDate", sdf.format(new Date()));
//
// // 增加
// IndexResponse addResponse = tc
// .prepareIndex("item_index", "shop_item", "1")
// .setSource(source).get();
//
// if (addResponse != null
// && addResponse.getShardInfo().getSuccessful() == 1) {
// if (Result.CREATED.equals(addResponse.getResult())) {
// System.err.println("create success");
// } else {
// System.err.println("update success");
// }
// } else {
// System.err.println(JSON.toJSONString(addResponse));
// } } catch (Exception e) {
e.printStackTrace();
}
if (tc != null) {
tc.close();
} System.err.println("finished");
}
}

4.  先在head插件中创建你自己的index名字,执行main函数, 就能在它下面创建相应的mapping。

批量写入数据,请看下篇: http://www.cnblogs.com/cs99lzzs/p/7212474.html

Elasticsearch 5.4.3实战--Java API调用:索引mapping创建的更多相关文章

  1. Elasticsearch 5.4.3实战--Java API调用:搜索建议

    通常的搜索引擎,都会根据用户的输入,实时给予匹配的提示. 那么这个功能在elasticsearch中如何实现呢? Elasticsearch里设计了4种类别的Suggester,分别是: Term S ...

  2. Elasticsearch 5.4.3实战--Java API调用:搜索

    ES有多种查询方式,我自己的业务是需要对多个字段进行查询,具体实现类代码如下. package com.cs99lzzs.elasticsearch.service.imp; import java. ...

  3. Elasticsearch 5.4.3实战--Java API调用:批量写入数据

    这个其实比较简单,直接上代码. 注意部分逻辑可以换成你自己的逻辑 package com.cs99lzzs.elasticsearch.service.imp; import java.sql.Tim ...

  4. elasticsearch(一):JAVA api操作

    1.创建一个mavan项目,项目的以来配置如下. <?xml version="1.0" encoding="UTF-8"?> <projec ...

  5. ElasticSearch入门-增删改查(java api)

    1.增加Index PutMappingRequest mapping = Requests.putMappingRequest(indices).type(mappingType).source(g ...

  6. JAVA Api 调用Hbase报错锦集

    1. 报错 java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/protobuf/generated/MasterProtos$Master ...

  7. java api 调用es集群(1.7版本)

    public static void main(String[] args) { Settings settings = ImmutableSettings.settingsBuilder() // ...

  8. Zookeeper Java API调用

    引入zookeeper-3.4.11.jar public class ZooKeeperTest implements Watcher{ //public final static String z ...

  9. elasticsearch技术解析与实战(一) 入门和索引

    GET _cat/nodes GET _cat/health GET _cat/shards GET http://10.37.84.124:9200/secisland?pretty { " ...

随机推荐

  1. Python入门介绍

    Python入门介绍(人生苦短,我用 Python) Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹 ...

  2. Shell的Posix字符分类

    [:alnum:] 匹配范围为 [a-zA-Z0-9] [:alpha:] 匹配范围为 [a-zA-Z] [:blank:] 匹配范围为 空格和TAB键 [:cntrl:] 匹配控制键 例如 ^M 要 ...

  3. MapReduce-WordCount

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  4. Linux记录- Linux下限制rm -rf /

    操作说明: 为了防止在linux下执行操作的时候误操作rm -rf /,或者rm -rf 一些比较重要的目录,我们做以下操作来限制rm的删除 1.下载源码安装包 https://raw.githubu ...

  5. [NIO-3]Socket通道

    Socket通道 上文讲述了通道.文件通道,这篇文章来讲述一下Socket通道,Socket通道与文件通道有着不一样的特征,分三点说: 1.NIO的Socket通道类可以运行于非阻塞模式并且是可选择的 ...

  6. ruby批量插入数据,bulk_insert-----Gem包使用

    文档 https://github.com/jamis/bulk_insert class Book < ActiveRecord::Base end book_attrs = ... # so ...

  7. SpringBoot+Thyemleaf

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  8. Elasticsearch学习笔记——安装、数据导入和查询

    到elasticsearch网站下载最新版本的elasticsearch 6.2.1 ? 1 https://www.elastic.co/downloads/elasticsearch 中文文档请参 ...

  9. hdu 5179 beautiful number(数位dp)

    原题链接 题意:求[l,r]中高位%低位等于0的数字个数.(不含0)分析:此题有三种方法.1.暴搜,毕竟最多才10个位.2.数位dp,预处理好整体的,再处理细节. dp[i][j]表示第i位上的数字位 ...

  10. 2016vijos 1-3 兔子的晚会(生成函数+倍增FWT)

    求出序列的生成函数后,倍增FWT #include<cstdio> using namespace std; #define N 2048 ; int inv; ]; int Pow(in ...