zoukankan      html  css  js  c++  java
  • Add mappings to an Elasticsearch index in realtime

    Changing mapping on existing index is not an easy task. You may find the reason and possible solutions in here:

    http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/


    to get current mapping details, here is the sample code:

     

    ClusterState cs = client.admin().cluster().prepareState().setFilterIndices("myIndex").execute().actionGet().getState();
    IndexMetaData imd = cs.getMetaData().index("myIndex")
    MappingMetaData mdd = imd.mapping("myType")


    Put Mappings In Real time:

     

    private void putMapping() {
        if (client != null) {
           if (client.admin().indices().prepareExists(IndexName).execute().actionGet().isExists()) {
              XContentBuilder mappings = null;
              try {
                 mappings = XContentFactory.jsonBuilder()
    		.startObject()
    		    .startObject(INDEX_TYPE)
    		        .startObject("properties")
    			.startObject(FIELD_NAME)
           			 .field("type","string")
    		      	 .field("store","yes")
    			 .field("index", "analyzed")
    			 .field("analyzer", "simple")
    			.endObject()
    		        .endObject()
    		      .endObject()
    		.endObject();
              } catch (IOException e) {
    	     e.printStackTrace();
              }
              client.admin().indices().prepareClose(IndexName).execute().actionGet();
              client.admin().indices().prepareDeleteMapping(IndexName).setType(INDEX_TYPE).execute().actionGet();
              client.admin().indices().preparePutMapping(IndexName).setIgnoreConflicts(true).setType(INDEX_TYPE).setSource(mappings).execute().actionGet();
    	  client.admin().indices().prepareOpen(IndexName).execute().actionGet();
           }
    
        } else {
    	throw new IllegalStateException(" Elastic Search not initialized properly..");
        }
    }


  • 相关阅读:
    Spring自定义注解简单使用四步走
    关于Mybaits映射一点心得
    设置UIButton文字大小颜色不同
    AFNetworking上传文件
    解决UITableView头部空白
    iOS获取文件和文件夹大小
    编译ffmpeg(iOS)
    让MySql支持Emoji表情
    MySQL重置密码(OSX)
    iOS多线程总结
  • 原文地址:https://www.cnblogs.com/riskyer/p/3369549.html
Copyright © 2011-2022 走看看