Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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 KnowledgeGarden/63fb01605f8ae67ffc4d9acfcb1161bd to your computer and use it in GitHub Desktop.
Save KnowledgeGarden/63fb01605f8ae67ffc4d9acfcb1161bd to your computer and use it in GitHub Desktop.
Elasticsearch create index with mappings
## Code
public IResult createIndex(String indexName, String mappings, String numberOfShards, String numberOfReplicas) {
//System.out.println("ProviderClient.createIndex "+mappings);
IResult result = new ResultPojo();
IndexSettings.Builder isb = new IndexSettings.Builder();
isb.numberOfShards(numberOfShards);
isb.numberOfReplicas(numberOfReplicas);
//@see https://github.com/elastic/elasticsearch-java/blob/66da097630dccc29da677f4a32ed8b468bceff3d/java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java
CreateIndexRequest.Builder b = new CreateIndexRequest.Builder()
.index(indexName)
.settings(isb.build())
.withJson(new StringReader(mappings));
try {
CreateIndexResponse createIndexResponse = client.indices().create(b.build()); <<<<<<<<<<<<<<<<
result.setResultObject(new Boolean(createIndexResponse.acknowledged()));
} catch (Exception e) {
e.printStackTrace();
environment.logError(e.getMessage(), e);
}
return result;
}
## ES console
[2022-06-12T14:00:30,007][WARN ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [Jacks-MacBook-Pro.local] received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{localAddress=/127.0.0.1:9200, remoteAddress=/127.0.0.1:64392}
## Java console
org.apache.http.ConnectionClosedException: Connection is closed
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:908)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:299)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146)
at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:266)
at org.topicquests.es.ProviderClient.createIndex(ProviderClient.java:108) <<<<<<<<<<<<<<<,
at devtests.FirstQueryTest.setMappings(FirstQueryTest.java:142)
at devtests.FirstQueryTest.<init>(FirstQueryTest.java:41)
at devtests.TestHarness.main(TestHarness.java:19)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment