@Component
@Slf4j
public class ElasticSearchUtil {
private static final Integer NUMBER_OF_SHARDS = 1;
private static final Integer NUMBER_OF_REPLICAS = 1;
@Autowired
private RestHighLevelClient client;
private String ioExcepMsg = "ElasticSearch连接失败";
/**
* 创建索引
*
* @param index
* @param mapping
*/
public void createIndex(String index, String mapping) {
try {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(getSettingBuilder());
request.mapping(mapping, XContentType.JSON);
client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn(ioExcepMsg);
throw new BusinessException(DevErrorCodeEnum.CANNOT_CONNECT_ES);
}
}
/**
* 创建索引
*
* @param index
* @throws IOException
*/
public void createIndex(String index) {
if (!ifExists(index)) {
log.warn("ES索引{}不存在,自动创建索引...", index);
try {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(getSettingBuilder());
XContentBuilder builder = getMappingBuilder();
request.mapping(builder);
client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn(ioExcepMsg);
throw new BusinessException(DevErrorCodeEnum.CANNOT_CONNECT_ES);
}
}
}
public SearchResponse search(String index, SearchSourceBuilder builder) {
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.source(builder);
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
//最长获取结果超时时间
builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
try {
return client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn(ioExcepMsg);
throw new BusinessException(DevErrorCodeEnum.CANNOT_CONNECT_ES);
}
}
/**
* 判断index是否存在
*
* @param index
* @return
*/
public boolean ifExists(String index) {
GetIndexRequest request = new GetIndexRequest(index);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
try {
return client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn(ioExcepMsg);
throw new BusinessException(DevErrorCodeEnum.CANNOT_CONNECT_ES);
}
}
/**
* 添加数据
*
* @param index
*/
public void createDoc(String index, Map<String, Object> data) {
createIndex(index);
IndexRequest indexRequest = new IndexRequest(index).source(data);
try {
client.index(indexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn(ioExcepMsg);
throw new BusinessException(DevErrorCodeEnum.CANNOT_CONNECT_ES);
}
}
private Settings.Builder getSettingBuilder() {
return Settings.builder()
.put("index.number_of_shards", NUMBER_OF_SHARDS)
.put("index.number_of_replicas", NUMBER_OF_REPLICAS);
}
private XContentBuilder getMappingBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("deviceNo");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("metric");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("numValue");
{
builder.field("type", "double");
}
builder.endObject();
builder.startObject("value");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("objectValue");
{
builder.field("type", "nested");
}
builder.endObject();
builder.startObject("timestamp");
{
builder.field("type", "date");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return builder;
}
}