Create Index using Java API (Elasticsearch 7.1 ) gives exception

I am trying to create index onto Elasticsearch 7.1 using below code snippet.

private void createIndex(String indexName, String jsonFile) throws URISyntaxException, IOException {
String jsonData = getJsonString(jsonFile);

        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.settings(Settings.builder()
                .put("index.number_of_shards", 10)
                .put("index.number_of_replicas", 1)
        );
        request.source(jsonData, XContentType.JSON);
        CreateIndexResponse response = transportClient.admin().indices().create(request).actionGet();

        if (response.isAcknowledged()) {
            out.println("Index created successfully");
        } else {
            out.println("Failed to create index");
        }
    }

    protected String getJsonString(String jsonFile) throws IOException, URISyntaxException {
        return new String(Files.readAllBytes(
                Paths.get(Objects.requireNonNull(App.class.getClassLoader().getResource(jsonFile)).toURI())));
    }

    private void initializeClient() {
        System.setProperty(ES_SET_NETTY_RUNTIME_AVAILABLE_PROCESSORS, "false");
        Settings.Builder settings = Settings.builder()
                .put(CLUSTER_NAME, "cluster")
                .put(CLIENT_TRANSPORT_SNIFF, false);
        transportClient = new PreBuiltTransportClient(settings.build());
             String host = "hostname";
            int port = 32545;
            out.println("Adding Indexer - [Host: " + host + "] and [Port:" + port + "]");
            transportClient.addTransportAddress(new TransportAddress(new InetSocketAddress(host.trim(), port)));
            out.println("Successfully created transport client.");
    }

-------Json file content------------------

{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"field1": {
"type": "text"
}
}
}
}

Which is giving me below exception -

Elasticsearch index creation failed for Impact management service. Caused by: MapperParsingException[Failed to parse mapping [properties]: Root mapping definition has unsupported parameters: [field1 : {type=text}]]; nested: MapperParsingException[Root mapping definition has unsupported parameters: [field1 : {type=text}]];
at org.elasticsearch.index.mapper.MapperService.internalMerge(MapperService.java:394)
at org.elasticsearch.index.mapper.MapperService.merge(MapperService.java:323)
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$IndexCreationTask.execute(MetaDataCreateIndexService.java:476)
at org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:47)
at org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:687)
at org.elasticsearch.cluster.service.MasterService.calculateTaskOutputs(MasterService.java:310)
at org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:210)
at org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:142)
at org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:150)
at org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:188)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:681)
at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:252)
at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:215)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: MapperParsingException[Root mapping definition has unsupported parameters: [field1 : {type=text}]]
at org.elasticsearch.index.mapper.DocumentMapperParser.checkNoRemainingFields(DocumentMapperParser.java:152)
at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:140)
at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:92)
at org.elasticsearch.index.mapper.MapperService.internalMerge(MapperService.java:392)
... 15 more

Index gets created successfully when tried using Kibana 7.1.

Can someone please help me..

@anagha Have you tried using Java High Level Rest Client?
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html

TransportClient is going away in the future releases.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.