Add mappings to an Elasticsearch index in realtime
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", "易做图yzed") .field("易做图yzer", "易做图") .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.."); } }
补充:软件开发 , Java ,