ElasticSearch 工具类

@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;
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值