io.searchbox.indices.CreateIndex Java Examples

The following examples show how to use io.searchbox.indices.CreateIndex. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractClientFactory.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an index.
 * @param indexName
 * @throws Exception
 */
@SuppressWarnings("nls")
protected void createIndex(JestClient client, String indexName, String settingsName) throws Exception {
    URL settings = AbstractClientFactory.class.getResource(settingsName);
    String source = IOUtils.toString(settings);
    JestResult response = client.execute(new CreateIndex.Builder(indexName).settings(source).build());
    if (!response.isSucceeded()) {
        // When running in e.g. Wildfly, the Gateway exists as two separate WARs - the API and the
        // runtime Gateway itself.  They both create a registry and thus they both try to initialize
        // the ES index if it doesn't exist.  A race condition could result in both WARs trying to
        // create the index.  So a result of "IndexAlreadyExistsException" should be ignored.
        if (!indexAlreadyExistsException(response)) {
            throw new Exception("Failed to create index: '" + indexName + "' Reason: " + response.getErrorMessage());
        }
    }
}
 
Example #2
Source File: JestClientDemo.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    // Construct a new Jest client according to configuration via factory
    JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
        .Builder("http://172.22.6.9:9200")
        .multiThreaded(true)
        //Per default this implementation will create no more than 2 concurrent connections per given route
        .defaultMaxTotalConnectionPerRoute(2)
        // and no more 20 connections in total
        .maxTotalConnection(100)
        .build());
    JestClient client = factory.getObject();
    JestResult result = client.execute(new CreateIndex.Builder("user").build());
    System.out.println("result = " + result.getJsonString());
}
 
Example #3
Source File: IndexMsgController.java    From EserKnife with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/addNewIndex")
@ResponseBody
public Object addNewIndex(@RequestParam("clusterName") String clusterName,
                         @RequestParam("indexName") String indexName,
                          @RequestParam("settings") String settings) {
    Map<String, Object> map = Maps.newHashMap();
    try {
        JestClient jestClient = JestManager.getJestClient(clusterName);
        /*IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
        JestResult jestResult = jestClient.execute(indicesExists);
        if(!jestResult.isSucceeded()){*/
            CreateIndex createIndex = new CreateIndex.Builder(indexName).settings(settings).build();
            JestResult result = jestClient.execute(createIndex);
            if(result.isSucceeded()){
                map.put("success",true);
            }else{
                map.put("errMsg", result.getErrorMessage());
            }
       /* }else{
            map.put("exist",true);
        }*/
    } catch (Exception e) {
        LOGGER.error("添加索引失败:",e);
        e.printStackTrace();
        map.put("errMsg", "添加失败,请联系管理员");
    }
    return map;
}
 
Example #4
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 创建索引
 *
 * @param indexName
 * @throws Exception
 */
public JestResult createIndex(EsModel es) {
    JestResult result = null;
    RetData aReturn = validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return result;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    result = indicesExists(indexName);
    if (result == null) {
        return result;
    }
    int responseCode = result.getResponseCode();
    try {
        if (responseCode == 404) {
            result = jestClient.execute(new CreateIndex.Builder(indexName).build());
            responseCode = result.getResponseCode();
            if (responseCode == 200) {
                result = createMapping(es);
            }
        }
    } catch (Exception e) {
        String exceptionMsg = StringUtil.getExceptionMsg(e);
        logger.info(exceptionMsg);

    }
    return result;
}
 
Example #5
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public boolean init(String elasticHost, String elasticPort, String elasticIndex, String logType, Path processResultMappingFile) {
    this.elasticHost = elasticHost;
    this.elasticPort = elasticPort;
    this.elasticIndex = elasticIndex;
    this.logType = logType;

    try {
        final JestClient client = getElasticSearchClient();
        boolean indexExists = client.execute(new IndicesExists.Builder(elasticIndex).build()).isSucceeded();
        if (!indexExists){
            log.info("Creating elasticsearch index.");
            client.execute(new CreateIndex.Builder(elasticIndex).build());
        }

        if (StringUtils.isNotBlank(logType) && (processResultMappingFile != null)) {
            log.info("Updating type mapping.");
            final String mappingJson = new String(ByteStreams.toByteArray(Files.newInputStream(processResultMappingFile)));
            client.execute(new PutMapping.Builder(elasticIndex, logType, mappingJson).build());
        }

        log.info("Established elasticsearch connection to host '{}:{}', index '{}'.", elasticHost, elasticPort, elasticIndex);
        elasticClient = client;
    } catch (IOException e) {
        log.error("Error creating base index on ElasticSearch: {}", e.getMessage(), e);
        elasticClient = null;
    }
    return true;
}
 
Example #6
Source File: IndexManagerBean.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private void createIndex(String indexName) throws IndexerNotAvailableException, IndexerRequestException {
    Settings settings = Settings.builder().build();
    JestResult result;

    try {
        result = esClient.execute(new CreateIndex.Builder(indexName).settings(settings.toString()).build());
    } catch (IOException e) {
        throw new IndexerNotAvailableException();
    }

    if (!result.isSucceeded()) {
        LOGGER.log(Level.WARNING, "Cannot create index" + indexName + ": " + result.getErrorMessage());
        throw new IndexerRequestException(result.getErrorMessage());
    }
}
 
Example #7
Source File: SearchIndexingServiceImpl.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    log.info("Instantiating jestSearchIndexingService...");
    log.info(String.format("Checking if %s needs to be created", LOCATION_INDEX_NAME));
    IndicesExists indicesExists = new IndicesExists.Builder(LOCATION_INDEX_NAME).build();
    JestResult r = jestClient.execute(indicesExists);

    if (!r.isSucceeded()) {
        log.info("Index does not exist. Creating index...");
        // create new index (if u have this in elasticsearch.yml and prefer
        // those defaults, then leave this out
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        settings.put("number_of_shards", 1);
        settings.put("number_of_replicas", 0);

        CreateIndex.Builder builder = new CreateIndex.Builder(LOCATION_INDEX_NAME);
        builder.settings(settings.build().getAsMap());
        CreateIndex createIndex = builder.build();

        log.info(createIndex.toString());

        jestClient.execute(createIndex);

        log.info("Index created");
    } else {
        log.info("Index already exist!");
    }
}
 
Example #8
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @param indexName
 * @throws Exception
 */
private void createIndex(String indexName) throws Exception {
    URL settings = getClass().getResource("index-settings.json"); //$NON-NLS-1$
    String source = IOUtils.toString(settings);
    JestResult response = esClient.execute(new CreateIndex.Builder(indexName).settings(source).build());
    if (!response.isSucceeded()) {
        throw new StorageException("Failed to create index " + indexName + ": " + response.getErrorMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    } else {
        logger.info("Created index:" + indexName);
    }
}
 
Example #9
Source File: DefaultTimeTaskController.java    From easy-sync with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("esRebuildIndex.htm")
public String esRebuildIndex(long id,HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
    TimeTask timeTask=timeTaskService.get(id);
    SyncConfig syncConfig= syncConfigDao.queryOneByProps (null,"timeTaskId",id);
    EsConfig esConfig = Global.toObject(timeTask.getTaskConfig(), EsConfig.class);
    String newIndexName=esConfig.getEs().getIndexAlias()+"_"+ DateFormatUtils.format(new Date(),"yyMMdd_HHmmss");
    Jest jest =new Jest(esConfig.getEs());

    boolean safe= esConfig.getRebuild().isSwitchAfterFullSync();

    //-- first build
    if(StringUtils.isBlank( syncConfig.getTaskConfig1()) || !safe){


        if(esConfig.getRebuild().isDeleteOldIndex() && StringUtils.isNotBlank(syncConfig.getWaitIndexName1())){
            if(!jest.deleteIndex(syncConfig.getWaitIndexName1())){
                logger.error("delete old index "+syncConfig.getWaitIndexName1()+" fail.");
            }

        }

        syncConfig.setWaitIndexName1(newIndexName);
        syncConfig.setFullStatus1(Const.REBUILD_NEED);



    }else{
        //todo:delete old index , judge status not run first
        syncConfig.setWaitIndexName2(newIndexName);
        syncConfig.setFullStatus2(Const.REBUILD_NEED);
    }



  /*  String settings = "\"settings\" : {\n" +
            "        \"number_of_shards\" : 5,\n" +
            "        \"number_of_replicas\" : 1\n" +
            "    }\n"*/;

    logger.info("begin to create index {}",newIndexName);

   EsMapping esMapping= esConfig.getEsMapping();
    EsField[] esFields=esMapping.getEsFields();
    JSONObject  fields=new JSONObject();

    for(EsField esField:esFields){

        JSONObject  field=new JSONObject();
        field.put("type",esField.getType());
        if("text".equalsIgnoreCase(esField.getType())){
            field.put("analyzer", esConfig.getEs().getAnalyzer());
        }

        logger.debug("add custom config");
        if(StringUtils.isNotBlank(esField.getCustom())){
            field.putAll( Global.toObject( esField.getCustom()));
        }

        fields.put(esField.getTarget(),field);

    }

    JSONObject  mapping=new JSONObject();
    JSONObject  properties=new JSONObject();
    properties.put("properties",fields);



    final String type="doc";
    mapping.put(type,properties);

    PutMapping putMapping = new PutMapping.Builder(
            newIndexName,
            type,
            mapping.toJSONString()
    ).build();

    CreateIndex.Builder builder=new CreateIndex.Builder(newIndexName);
    if(StringUtils.isNotBlank(esConfig.getEs().getIndexSettings())){
        builder.settings(esConfig.getEs().getIndexSettings());
    }

    JestResult jestResult= jest.getJestClient().execute(builder.build());

    if(! jestResult.isSucceeded()){
        throw new Exception(jestResult.getErrorMessage());
    }

    jestResult= jest.getJestClient().execute(putMapping);

    if(! jestResult.isSucceeded()){
        throw new Exception(jestResult.getErrorMessage());
    }
    syncConfigDao.save(syncConfig);

    return newIndexName;


}
 
Example #10
Source File: JestClient.java    From wES with MIT License 4 votes vote down vote up
@Override
public boolean createIndex(String index) throws Exception {
	JestResult result = _exec(new CreateIndex.Builder(index).build());
	return result != null;
}
 
Example #11
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}
 
Example #12
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}