首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java Elastic Search Api:无法运行简单示例: org.elasticsearch.transport.NodeDisconnectedException:

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

Stack Overflow用户
提问于 2018-10-29 07:24:44
回答 2查看 498关注 0票数 0

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

现在:我试着运行一个简单的例子如下:

我首先启动了服务器bin/elasticsearch,并运行了以下代码

代码语言:javascript
复制
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();
        }
    }
}

抛出的回溯如下:

代码语言:javascript
复制
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)

包含所有dep的github代码库可以在以下位置找到:https://github.com/uzresk/elasticsearch-javaapi-examples

1我做错了什么?

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

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

编辑:我的elasticsearch.yml是:

代码语言:javascript
复制
# ======================== 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
EN

回答 2

Stack Overflow用户

发布于 2018-10-29 10:13:14

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

代码语言:javascript
复制
try {
            transportClient = new TransportClient();
            client = transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9200))
票数 0
EN

Stack Overflow用户

发布于 2018-10-29 15:17:48

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

代码语言:javascript
复制
network.host: 192.168.1.40

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53036945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档