通过Java API在弹性搜索中创建索引

我使用以下代码在弹性搜索中创建索引,
默认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

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写: 一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...