• 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:

    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    
    public class ElasticDemo {
    
    	 //测试demo
    		@SuppressWarnings({ "resource", "unchecked" })
    		public static void main(String[] aa) throws UnknownHostException {
    	    	TransportClient client = null;
    	    	Settings esSettings = Settings.builder()
    	                .put("cluster.name", "elasticsearch") //设置ES实例的名称
    	                .build();
    	        client = new PreBuiltTransportClient(esSettings)
    	                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    	        System.out.println("ElasticsearchClient 连接成功");
    	    	
    //	    	BulkRequestBuilder bulkRequest = client.prepareBulk();
    //	        try {
    //				bulkRequest.add(client.prepareIndex("ko", "test", "14")
    //				        .setSource(jsonBuilder()
    //				                .startObject()
    //				                .field("tableId", "14")
    //				                .field("title", "tetstcccfffffccccccccccc")
    //				                .field("content", "ttttttttfffffffttttttttttt")
    //				                .endObject()
    //				        )
    //				);
    //			} catch (IOException e) {
    //				e.printStackTrace();
    //			}
    //	        BulkResponse bulkResponse = bulkRequest.get();
    //	        if (bulkResponse.hasFailures()) {
    //	            System.out.println("失败:" + bulkResponse.toString());
    //	        }
    //	        System.out.println("操作成功");
    	    	
    	        HighlightBuilder highlightBuilder = new HighlightBuilder().field("title").requireFieldMatch(true);
    	        highlightBuilder.preTags("<span style="color:red">");
    	        highlightBuilder.postTags("</span>");
    	        highlightBuilder.boundaryMaxScan(4);
    	        
    	        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("ko");	//可以是多个index
    	        //searchRequestBuilder.setQuery(QueryBuilders.termQuery("title", "ffff"));	//精确查询
    	        searchRequestBuilder.setQuery(QueryBuilders.wildcardQuery("title", "*文 el*"));	//模糊查询
    	        searchRequestBuilder.setFrom(0);
    	        searchRequestBuilder.setSize(2);
    	        searchRequestBuilder.highlighter(highlightBuilder);
    	        SearchResponse response = searchRequestBuilder.get();
    	        System.out.println(response);
    	    }
    		
    }
    

      

  • 相关阅读:
    linq in 查询
    sql数据分组取第一条
    获取mac地址
    计算机网络体系结构分层 (OSI TCP/IP)
    2048游戏代码
    go——变量、类型、常量、函数
    关于装饰器 开放封闭
    linux杂碎知识
    crawl——scrapy(配置文件,持久化,请求传递参数,提高爬虫效率,爬虫中间件,集成selenium,去重规则)
    crawl——xpath使用
  • 原文地址:https://www.cnblogs.com/yanketao/p/10956299.html
Copyright © 2020-2023  润新知