因为项目开发使用的是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. day14-jdbc案例(简单的curd&分页)

    回顾: mvc jsp的设计模式1: jsp+javabean jsp的设计模式2: jsp+javabean+servlet jsp:展示数据 javabean:封装数据 封装对数据的访问 serv ...

  2. Mac 上有哪些值得推荐的软件?冷门小众软件但实用

    确实Mac上有很多小众.冷门,但却是十分实用.值得推荐的工具,小编针对用的比较多的软件,整理了一些,希望有帮助. The Unarchive:解压缩工具 macOS 对于压缩文件的处理不是很好.如果你 ...

  3. python3.5和python3.6关于json模块的区别

    python3.5中 无法反序列化bytes数据必须decode成str才可以 >>> import json >>> a = b'{"username& ...

  4. Vue less使用scope时渗入修改子组件样式

    @deep: ~'>>>'; .wrap { @{deep} .component1 { width: 120px; } }

  5. Spring+Mybatis+SpringMVC+Atomikos多数据源共存+不同数据库事物一致性处理

    网上找了一大堆的例子,没一个跑通的,都是copy转发,哎,整理得好辛苦..做个笔记,方便正遇到此问题的猿们能够得到帮助....废话不多说,贴代码..... 项目结构说明: 1.dao层的admin.w ...

  6. HDU - 5119 Happy Matt Friends(dp)

    题目链接 题意:n个数,你可以从中选一些数,也可以不选,选出来的元素的异或和大于m时,则称满足情况.问满足情况的方案数为多少. 分析:本来以为是用什么特殊的数据结构来操作,没想到是dp,还好队友很强. ...

  7. 队列 Queue 与 生产者消费模型

    队列:先进先出 # from multiprocessing import Queue # Q = Queue(4) # Q.put('a') # Q.put('b') # Q.put('b') # ...

  8. python3 bytes数据类型探讨

    python3中str和bytes分开了,那么bytes与str之间到底是什么关系呢?下面从表现形式.处理方式.存储形式三个方面来阐述其区别 1. 在字符串前面加上b,就表示bytes数据类型 s1 ...

  9. 酷狗.kgtemp文件加密算法逆向

    该帖转载于孤心浪子--http://www.cnblogs.com/KMBlog/p/6877752.html 酷狗音乐上的一些歌曲是不能免费下载的,然而用户仍然可以离线试听,这说明有缓存文件,并且极 ...

  10. GCC编译器原理(一)------GCC 工具:addr2line、ar、as、c++filt和elfedit

    1.3 GCC 工具 1.3.1 binutils 工具集 工具 描述 addr2line 给出一个可执行文件的内部地址,addr2line 使用文件中的调试信息将地址翻译成源代码文件名和行号. ar ...