在Java API的Elastic Search中创建索引

Ram*_*amy 12 java elasticsearch

我使用以下代码在Elastic Search,Default 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());  
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

通过使用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)
Run Code Online (Sandbox Code Playgroud)

请指导我错误的地方.提前致谢

小智 9

TransportClient的端口(通过Java API)与Http不同默认情况下,transportClient端口为9300


rsi*_*va4 8

使用副本和分片设置:

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();
Run Code Online (Sandbox Code Playgroud)


小智 6

鉴于你有客户端,你不应该这样做:

CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet();
Run Code Online (Sandbox Code Playgroud)

  • 我只想补充一点,`createIndexRequest`方法是`Requests.createIndexRequest("test1")` (2认同)