Elasticsearch教程---High Level REST Client创建索引(六)

第八章 索引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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值