第八章 索引api
8.1 代码示例
设置分片API:
private void buildSetting(CreateIndexRequest request) {
request.settings(Settings.builder().put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2));
}
生成索引mapping(以短信下发表为例):
/**
* 生成短信下发表索引结构
*
* createDate 创建时间
* sendDate 发送时间
* longCode 发送的长号码
* mobile 下发手机号
* corpName 发送公司名称
* smsContent 下发短信内容
* state 短信下发状态 0 成功 1 失败
* operatorId '运营商编号 1 移动 2 联通 3 电信
* province 省份
* ipAddr 下发服务器IP地址
* replyTotal 短信状态报告返回时长(秒)
* fee 费用
* @param request
* @param type
* @throws IOException
*/
private void buildIndexMapping(CreateIndexRequest request, String type) throws IOException {
XContentBuilder mappingBuilder = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("mobile")
.field("type", "keyword")
.field("index", "true")
.endObject()
.startObject("createDate")
.field("type", "date")
.field("index", "true")
.endObject()
.startObject("sendDate")
.field("type", "date")
.field("index", "true")
.endObject()
.startObject("longCode")
.field("type", "keyword")
.field("index", "true")
.endObject()
.startObject("corpName")
.field("type", "keyword")
.field("index", "true")
.endObject()
.startObject("smsContent")
.field("type", "text")
.field("index", "true")
.field("analyzer", "ik_max_word")
.endObject()
.startObject("state")
.field("type", "integer")
.field("index", "true")
.endObject()
.startObject("province")
.field("type", "keyword")
.field("index", "true")
.endObject()
.startObject("operatorId")
.field("type", "integer")
.field("index", "true")
.endObject()
.startObject("ipAddr")
.field("type", "ip")
.field("index", "true")
.endObject()
.startObject("replyTotal")
.field("type", "integer")
.field("index", "true")
.endObject()
.startObject("fee")
.field("type", "integer")
.field("index", "true")
.endObject()
.endObject()
.endObject();
request.mapping(type, mappingBuilder);
}
package com.javablog.elasticsearch.document.impl;
import com.javablog.elasticsearch.document.IndexService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service("indexService")
public class IndexServiceImpl implements IndexService {
private final static Logger log = LoggerFactory.getLogger(IndexServiceImpl.class);
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引
* @param index 索引名称
* @param type 索引类型
* @param request 创建索引的REQUEST
* @throws IOException
*/
@Override
public void createIndex(String index,String type,CreateIndexRequest request) throws IOException {
log.info("source:" + request.toString());
if (!existsIndex(index)) {
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
log.info("索引创建结查:" + response.isAcknowledged());
} else {
log.warn("索引:{},已经存在,不能再创建。", index);
}
}
/**
* 删除索引
* @param index 索引名称
* @throws IOException
*/
@Override
public void deleteIndex(String index) throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(index);
if (restHighLevelClient.indices().exists(getIndexRequest)) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
log.info("source:" + deleteIndexRequest.toString());
restHighLevelClient.indices().delete(deleteIndexRequest);
}
}
/**
* 判断索引是否存在
* @param index
* @return
* @throws IOException
*/
public boolean existsIndex(String index) throws IOException {
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
log.info("source:" + request.toString());
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
log.debug("existsIndex: " + exists);
return exists;
}
}
完整代码:https://github.com/chutianmen/elasticsearch-examples