首页 > 解决方案 > Java Elastic Search Api: Unable to run simple example: org.elasticsearch.transport.NodeDisconnectedException:

问题描述

I have downloaded the elastic search binaries from https://www.elastic.co/downloads/elasticsearch and everything works okay.

Now: I'm trying to run a simple example below:

I first started up the server bin/elasticsearch, and ran the code below

package jp.gr.java_conf.uzresk.es.search;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;

public class Search {

    public static void main(String[] args) {

        new Search().run();
    }

    public void run() {
        Client client = null;
        TransportClient transportClient = null;
        try {
            transportClient = new TransportClient();
            client = transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9300));

            SearchResponse response = client.prepareSearch("tms-allflat").setTypes("personal")
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .setQuery(QueryBuilders.termQuery("skill_1", "Java"))
                    .setPostFilter(FilterBuilders.rangeFilter("age").from(25).to(30)).setFrom(0).setSize(10)
                    .setExplain(true).execute().actionGet();

            SearchHits hits = response.getHits();
            hits.forEach(s -> System.out.println(s.getSourceAsString()));

        } finally {
            transportClient.close();
            client.close();
        }
    }
}

The traceback thrown was this:

Oct 28, 2018 11:15:52 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Achelous] loaded [], sites []
Oct 28, 2018 11:15:53 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Achelous] failed to get node info for [#transport#-1][dell-Inspiron-7577][inet[localhost/127.0.0.1:9300]], disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][inet[localhost/127.0.0.1:9300]][cluster:monitor/nodes/info] disconnected

Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
    at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106)
    at org.elasticsearch.client.support.AbstractClient.search(AbstractClient.java:338)
    at org.elasticsearch.client.transport.TransportClient.search(TransportClient.java:430)
    at org.elasticsearch.action.search.SearchRequestBuilder.doExecute(SearchRequestBuilder.java:1112)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
    at jp.gr.java_conf.uzresk.es.search.Search.run(Search.java:30)
    at jp.gr.java_conf.uzresk.es.search.Search.main(Search.java:16)

The github repo, with all the deps, can be found here: https://github.com/uzresk/elasticsearch-javaapi-examples

[1] What am I doing wrong?

[2] How do I fix this?

I tried googling around but couldn't find anything promising - I'd love a pointer or two in the right direction. :)

EDIT: My elasticsearch.yml is this:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

标签: javadatabaseelasticsearch

解决方案


编辑您的 elasticsearch.yml 文件添加以下设置,然后重新启动服务器。

network.host: 192.168.1.40

默认的主机列表是 ["127.0.0.1", "[::1]"]。您需要指定您的主机,如果它不是 127.0.0.1。


推荐阅读