java代码连接elasticsearch遇到的坑

 场景:

首先我用postman连接Elasticsearch测试是没有问题的如下

创建索引

PUT 192.168.100.88:9200/log_index?pretty
{
  "settings":{
    "number_of_shards":3,
    "number_of_replicas":2
  },
  "mappings":{
    "log_type":{
      "properties":{
               "id": {
                    "type": "string"        
                },
				"staffName": {
                    "type": "string"        
                },
				"userId": {
                    "type": "string"        
                },
				"opTitle": {
                    "type": "string"        
                },
				"opType": {
                    "type": "string"        
                },
				 "createDate": {
                    "type": "date",           
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
				 "loginIp": {
                    "type": "string"
                },
				 "userAgent": {
                    "type": "string"
                },
				 "requestUrl": {
                    "type": "string"
                },
                "method": {
                    "type": "string"
                },
				"exception": {
                    "type": "string"
                },
				"rev": {
                    "type": "string"
                },
				"username":{
				   "type": "string"
				}
       
            }
        }
    }
}

查询索引信息

java代码连接elasticsearch遇到的坑_第1张图片

config中关于elasticsearch的配置:

elk.clusterName=gis-elk
elk.clientTransportSniff=true
elk.InetAddress=elk.gisquest.com
elk.port=9200
elk.index=log_index
elk.type=log_type
elk.enable=true

代码测试(看着像正确的,其实遇到了一堆坑如下

测试功能:测试索引(log_index)是否存在

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class Test {

	public static void main(String[] args) {
        // 读取ES配置
        String host = "192.168.100.88";
        int port = 9200;
        String index ="log_index";

        //打印es连接信息
        System.out.println("host:" + host + ",port:" + port);

        // 获取settings
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "gis-elk").build();

        // 客户端对象
        TransportClient client = null;

        // 建立ES连接
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

       IndicesExistsRequest request = new IndicesExistsRequest(index);
       IndicesAdminClient adminClient =client.admin().indices();
	   IndicesExistsResponse inExistsResponse =  adminClient.exists(request).actionGet();

        // 测试index索引是否存在
        System.out.println(inExistsResponse.isExists());

        // 释放客户端
        client.close();
	}

}

问题一:版本不兼容问题,项目中同时引用了elasticsearch和solr及lunce(无语了!!!!)

   
			org.apache.solr
			solr-core
			5.3.1
		 



    
       
			org.apache.lucene
			lucene-core
			3.6.0
		


      
			org.elasticsearch.client
			transport
			5.5.1
		

三个都存在启动提示测试类提示: java.lang.NoClassDefFoundError: org/apache/lucene/util/Accountable

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/Accountable
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$100(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:96)
	at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:126)
	at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:265)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:130)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:116)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:106)
	at Test.main(Test.java:33)
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.util.Accountable
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)

发现在lucene3.6.0版本中org/apache/lucene/util/Accountable找不到Accountable这个类

java代码连接elasticsearch遇到的坑_第2张图片

在elasticsearch5.5.1版本内部依赖的lucene-core-6.6.0中有Accountable这个类(这里我直接将lucene-core-3.6.0直接屏蔽了)。

java代码连接elasticsearch遇到的坑_第3张图片

而solr中lucene-core-5.3.1中也存在Accountable这个类

java代码连接elasticsearch遇到的坑_第4张图片

同时存在两个     solr-core-5.3.1 和 org.elasticsearch.client-5.5.1客户端时,此时maven依赖中显示的是lucene-core-5.3.1

启动测试类提示java.lang.NoSuchFieldError: LUCENE_5_4_1

Exception in thread "main" java.lang.NoSuchFieldError: LUCENE_5_4_1
	at org.elasticsearch.Version.(Version.java:57)
	at org.elasticsearch.common.io.stream.StreamOutput.(StreamOutput.java:74)
	at org.elasticsearch.common.io.stream.BytesStream.(BytesStream.java:24)
	at org.elasticsearch.common.io.stream.BytesStreamOutput.(BytesStreamOutput.java:59)
	at org.elasticsearch.common.io.stream.BytesStreamOutput.(BytesStreamOutput.java:56)
	at org.elasticsearch.common.io.stream.BytesStreamOutput.(BytesStreamOutput.java:46)
	at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:69)
	at org.elasticsearch.common.settings.Setting.arrayToParsableString(Setting.java:868)
	at org.elasticsearch.common.settings.Setting.lambda$listSetting$29(Setting.java:814)
	at org.elasticsearch.common.settings.Setting$2.getRaw(Setting.java:818)
	at org.elasticsearch.common.settings.Setting.lambda$listSetting$27(Setting.java:802)
	at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:807)
	at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:802)

版本冲突问题 

参考:elasticsearch启动出现了java.lang.NoSuchFieldError: LUCENE_5_5_2异常_t0404的博客-CSDN博客

无语了,也不知道脑残公司为啥这么搞!!!!

问题二:提示NoNodeAvailableException[None of the configured nodes are available连接不上

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{246qOz7mQpy3A6BX5rGwog}{192.168.100.88}{192.168.100.88:9200}]]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:397)
	at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1250)
	at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.exists(AbstractClient.java:1272)
	at Test.main(Test.java:55)

java的通信端口是9300,我也不是很清楚为什么要这样!!

参考:解决springboot集成es 报错NoNodeAvailableException None of the configured nodes are available_Mars'Ares的博客-CSDN博客

暂时屏蔽掉solr和lucene-core依赖再次测试(同时存在上述依赖的问题需要讨论),我用的是elasticsearch中lucene-core-6.6.0,再次启动测试类返回为true,

因为我先创建的索引log_index,因此返回true

java代码连接elasticsearch遇到的坑_第5张图片

你可能感兴趣的:(java,elasticsearch,java,搜索引擎)