Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2011 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1328836 to your computer and use it in GitHub Desktop.
Save anonymous/1328836 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.node.NodeBuilder;
public class DynamicTemplatesTest {
private static final String INDEX = "myindex";
private static final String TYPE = "mytype";
private static final String DT_MAPPING = "{" +
" \"mappings\" : {" +
" \"mytype\": {" +
" \"dynamic_templates\":[{" +
" \"list_mapping\": {" +
" \"match\":\"list__*\"," +
" \"mapping\":{" +
" \"index\" : \"not_analyzed\"" +
" }" +
" }" +
" },{" +
" \"set_mapping\": {" +
" \"match\":\"set__*\"," +
" \"mapping\":{" +
" \"index\" : \"not_analyzed\"" +
" }" +
" }" +
" }" +
" ]" +
" }" +
" }" +
"}";
// curl command equivalent
// curl -X POST "http://localhost:9200/myindex" -d '{
// "mappings" : {
// "mytype": {
// "dynamic_templates":[{
// "list_mapping": {
// "match":"list__*",
// "mapping":{
// "index" : "not_analyzed"
// }
// }
// },{
// "set_mapping": {
// "match":"set__*",
// "mapping":{
// "index" : "not_analyzed"
// }
// }
// }
// ]
// }
// }
//}'
public static void main(String[] args) throws IOException {
Settings settings = ImmutableSettings.settingsBuilder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
.build();
Client client = NodeBuilder.nodeBuilder()
.settings(settings)
.loadConfigSettings(false)
.client(true)
.node()
.client();
client.admin().indices().create(new CreateIndexRequest(INDEX, settings)).actionGet();
//the following doesn't work
client.admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(DT_MAPPING).execute().actionGet(new TimeValue(60, TimeUnit.SECONDS));
ClusterState cs = client.admin().cluster().prepareState().setFilterIndices(INDEX).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(INDEX);
MappingMetaData mdd = imd.mapping(TYPE);
System.out.println(mdd.source().string());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment