Elastic Searchでインデックスを作成するために次のコードを使用しました。デフォルトJava API:
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "myClusterName").put("client.transport.sniff", true).build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9200));
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("test1");
CreateIndexResponse response = createIndexRequestBuilder.execute().actionGet();
System.out.println(response.isAcknowledged());
RESTサービス:
HttpURLConnection con = null;
try
{
String url = "http://localhost:9200/test2";
URL resturl = new URL(url);
con = (HttpURLConnection) resturl.openConnection();
con.setDoOutput(true);
con.setRequestMethod("PUT");
BufferedReader in = null;
try
{
if (con.getInputStream() != null)
{
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
}
catch (IOException e)
{
if (con.getErrorStream() != null)
{
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
}
if (in == null)
{
throw new Exception("Unable to read response from server");
}
StringBuffer decodedString = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
decodedString.append(line);
}
in.close();
System.out.println("4");
Integer responseCode = con.getResponseCode();
System.out.println(responseCode);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (con != null)
{
con.disconnect();
}
}
REST APIを使用すると、インデックスを作成できます。デフォルトではJava APIの場合、次の例外が発生します。
org.elasticsearch.client.transport.NoNodeAvailableException: No node available
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.Java:202)
at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.Java:85)
at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.Java:200)
at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.Java:206)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.Java:62)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.Java:57)
at ElasticSearch.createIndex(ElasticSearch.Java:121)
at ElasticSearch.main(ElasticSearch.Java:157)
間違えたところを案内してください。前もって感謝します
TransportClientのポート(Java APIを使用)はHttpとは異なります。デフォルトでは、transportClientポートは9300です
レプリカとシャードの設定:
Settings indexSettings = ImmutableSettings.settingsBuilder()
.put("number_of_shards", 1)
.put("number_of_replicas", 1)
.build();
CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings);
client.admin().indices().create(indexRequest).actionGet();
あなたがクライアントを持っているとすれば、これを行うことができないはずです:
CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet();
コードの下でJava High-Level Rest Clientを使用すると、インデックスを作成できます。
CreateIndexRequest request = new CreateIndexRequest("users");
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("userId", message);
properties.put("name", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
CreateIndexResponse indexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("response id: "+indexResponse.index());
詳細については、次を参照してください: https://www.elastic.co/guide/en/elasticsearch/client/Java-rest/master/Java-rest-high-create-index.html#Java-rest-高作成インデックス