ElasticSearch报错

ElasticSearch报错NoNodeAvailableException[None of the configured nodes are available:

代码如下:

Client client = TransportClient
				.builder()
				.build()
				.addTransportAddress(
						new InetSocketTransportAddress(InetAddress
								.getByName("127.0.0.1"), 9300));
		System.out.println(client);
		IndexResponse response = client
				.prepareIndex("test", "first", "1")
				.setSource(XContentFactory.jsonBuilder().startObject()
								.field("username", "xzq")
								.field("createDate", new Date())
								.field("message", "start Elasticsearch")
								.endObject()).get();
		System.out.println(response.getIndex());
		System.out.println(response.getType());
		System.out.println(response.getId());
		System.out.println(response.getVersion());
		System.out.println(response.isCreated());
		client.close();
遇到上述问题时,需要检查如下内容:

1、cluster.name是否与服务器配置一致(elasticsearch.yml中配置);

2、确保端口号(例如:9300)已开放;

修改代码后,程序运行正常。代码如下:

Settings settings = Settings.settingsBuilder()
				// 指定集群名称
				.put("cluster.name", "xzq-test")
				// 探测集群中机器状态
				.put("client.transport.sniff", true).build();
		Client client = TransportClient
				.builder()
				.settings(settings)
				.build()
				.addTransportAddress(
						new InetSocketTransportAddress(InetAddress
								.getByName("127.0.0.1"), 9300));
		System.out.println(client);
		IndexResponse response = client
				.prepareIndex("test", "first", "1")
				.setSource(XContentFactory.jsonBuilder().startObject()
								.field("username", "xzq")
								.field("createDate", new Date())
								.field("message", "start Elasticsearch")
								.endObject()).get();
		System.out.println(response.getIndex());
		System.out.println(response.getType());
		System.out.println(response.getId());
		System.out.println(response.getVersion());
		System.out.println(response.isCreated());
		client.close();



你可能感兴趣的:(elasticsearch,elasticsearch)