io.searchbox.indices.mapping.PutMapping Java Examples

The following examples show how to use io.searchbox.indices.mapping.PutMapping. 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: JestServiceImpl.java    From EserKnife with Apache License 2.0 5 votes vote down vote up
@Override
public JestResult addType(String clustName, String indexName, String typeName, String json) {
    JestResult result = null ;
    try {
        PutMapping putMapping = new PutMapping.Builder(indexName,typeName,json).build();
        result = JestManager.getJestClient(clustName).execute(putMapping);
    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.error("putMapping失败:",e);
    }
    return result ;
}
 
Example #2
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 #3
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 #4
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}
 
Example #5
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}