当前位置:   article > 正文

NoNodeAvailableException[None of the configured nodes are available

nonodeavailableexception[none of the configured nodes are available

可能的原因:

1、项目引用的ES版本和ES服务的版本不一样;

2、端口不正确,tcp访问端口是9300;

3、cluster.name不正确,可以通过访问http://localhost:9200/查看ES服务下的cluster.name;

4、ES服务没启动;

简单demo:

  1. import java.net.InetAddress;
  2. import java.net.UnknownHostException;
  3. import org.elasticsearch.action.search.SearchRequestBuilder;
  4. import org.elasticsearch.action.search.SearchResponse;
  5. import org.elasticsearch.client.transport.TransportClient;
  6. import org.elasticsearch.common.settings.Settings;
  7. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  8. import org.elasticsearch.index.query.QueryBuilders;
  9. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  10. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  11. public class ElasticDemo {
  12. //测试demo
  13. @SuppressWarnings({ "resource", "unchecked" })
  14. public static void main(String[] aa) throws UnknownHostException {
  15. TransportClient client = null;
  16. Settings esSettings = Settings.builder()
  17. .put("cluster.name", "elasticsearch") //设置ES实例的名称
  18. .build();
  19. client = new PreBuiltTransportClient(esSettings)
  20. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
  21. System.out.println("ElasticsearchClient 连接成功");
  22. // BulkRequestBuilder bulkRequest = client.prepareBulk();
  23. // try {
  24. // bulkRequest.add(client.prepareIndex("ko", "test", "14")
  25. // .setSource(jsonBuilder()
  26. // .startObject()
  27. // .field("tableId", "14")
  28. // .field("title", "tetstcccfffffccccccccccc")
  29. // .field("content", "ttttttttfffffffttttttttttt")
  30. // .endObject()
  31. // )
  32. // );
  33. // } catch (IOException e) {
  34. // e.printStackTrace();
  35. // }
  36. // BulkResponse bulkResponse = bulkRequest.get();
  37. // if (bulkResponse.hasFailures()) {
  38. // System.out.println("失败:" + bulkResponse.toString());
  39. // }
  40. // System.out.println("操作成功");
  41. HighlightBuilder highlightBuilder = new HighlightBuilder().field("title").requireFieldMatch(true);
  42. highlightBuilder.preTags("<span style=\"color:red\">");
  43. highlightBuilder.postTags("</span>");
  44. highlightBuilder.boundaryMaxScan(4);
  45. SearchRequestBuilder searchRequestBuilder = client.prepareSearch("ko"); //可以是多个index
  46. //searchRequestBuilder.setQuery(QueryBuilders.termQuery("title", "ffff")); //精确查询
  47. searchRequestBuilder.setQuery(QueryBuilders.wildcardQuery("title", "*文 el*")); //模糊查询
  48. searchRequestBuilder.setFrom(0);
  49. searchRequestBuilder.setSize(2);
  50. searchRequestBuilder.highlighter(highlightBuilder);
  51. SearchResponse response = searchRequestBuilder.get();
  52. System.out.println(response);
  53. }
  54. }

  

posted on 2019-05-31 17:05 三天打鱼,两天晒网 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/yanketao/p/10956299.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/457667
推荐阅读
相关标签
  

闽ICP备14008679号