如何解决Java Elastic Search Api:无法运行简单示例:org.elasticsearch.transport.NodeDisconnectedException:?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (837)

我正在尝试运行以下简单示例:

我首先启动了服务器bin/elasticsearch,然后运行下面的代码

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();
        }
    }
}

抛出的追溯是这样的:

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)

可以在这里找到包含所有deps的github repo:https//github.com/uzresk/elasticsearch-javaapi-examples

[1]我做错了什么?

[2]我该如何解决这个问题?

我试着用谷歌搜索但找不到任何有希望的东西 - 我喜欢指向正确方向的一两个指针。:)

编辑:我的elasticsearch.yml是这样的:

# ======================== 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
提问于
用户回答回答于

将端口从9300更改为9200,如下所示:

try {
            transportClient = new TransportClient();
            client = transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9200))

扫码关注云+社区

领取腾讯云代金券