Java Elastic Search Api:无法运行简单的示例:org.elasticsearch.transport.NodeDisconnectedException:

时间:2018-10-28 23:24:44

标签: java database elasticsearch

我已经从https://www.elastic.co/downloads/elasticsearch下载了弹性搜索二进制文件,一切正常。

现在:我正在尝试在下面运行一个简单的示例:

我首先启动了服务器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)

带有所有部门的github仓库可以在这里找到: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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

network.host: 192.168.1.40

默认主机列表为[“ 127.0.0.1”,“ [:: 1]”]。 如果不是127.0.0.1,则需要指定主机。