• java创建elasticsearch索引


    创建索引,字段为id、utc

    创建索引

    public static void makeIndex(String indexName,String typeName){
            boolean existsIndex = isExistsIndex(indexName);
            if (existsIndex) {
    
            } else {
                TransportClient connection = null;
                List<String> result = new ArrayList<>();
                try {
                    connection = connectionPool.getConnection();
                    Settings build = Settings.builder().put("index.number_of_shards", 1) //设置分片数
                            .put("index.refresh_interval", "30s")  //执行刷新操作的频率,索引更新多久才对搜索可见
                            .put("index.routing.allocation.total_shards_per_node", 3)//每个节点上允许最多分片数
                            .put("index.translog.sync_interval", "30s") //将数据同步到磁盘的频率
                            .put("index.number_of_replicas", 1) //每个主分片拥有的副本数
                            .put("index.max_result_window", "10000000") //一次最多获取多少条记录
                            .build();
                    CreateIndexResponse createIndexResponse = connection.admin().indices().prepareCreate(indexName).setSettings(build).execute().actionGet();
                    result.add("settings设置:" + createIndexResponse.isAcknowledged());
                    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject()
                            .field("dynamic", "false")
                            .startObject("properties")
                            .startObject("id").field("type", "keyword").field("index", "true").endObject()
                            .startObject("utc").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()
                            .endObject()
                            .endObject();
                    PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(typeName).source(xContentBuilder);
                    AcknowledgedResponse acknowledgedResponse = connection.admin().indices().putMapping(mappingRequest).actionGet();
                    result.add("mapping设置:" + acknowledgedResponse.isAcknowledged());
                } catch (Exception e) {
                    logger2.error("makeIndex错误", e);
                } finally {
                    if (connection != null) {
                        connectionPool.releaseConnection(connection);
                    }
                }
    
            }
        }

    判断索引是否存在

    public static boolean isExistsIndex(String indexName) {
            TransportClient connection = null;
            IndicesExistsResponse response;
            try {
                connection = connectionPool.getConnection();
                response = connection.admin().indices().exists(new IndicesExistsRequest().indices(new String[]{indexName})).actionGet();
    
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("错误", e);
                return true;
            } finally {
                if (connection != null) {
                    connectionPool.releaseConnection(connection);
                }
            }
            return response.isExists();
        }
  • 相关阅读:
    常用集体名词的用法
    囊中羞涩的表达
    《当幸福来敲门》观后感
    <肖申克的救赎>观后感
    心语4
    补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.
    线程中对变量的用法
    添加按键变量数组,就是很多同种类型按键关联变量,这些变量是一个数组;
    不容按钮、下拉框 执行同一个函数或者同一种函数的用法
    CImage显示位图与CDC双缓冲冲突,使用路径层解决.
  • 原文地址:https://www.cnblogs.com/asksk/p/15480775.html
Copyright © 2020-2023  润新知