提交 b9e75ea6 编写于 作者: W wangpenghui
......@@ -54,6 +54,12 @@
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
......@@ -74,11 +80,19 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
......
package indi.felix.easy.core.elastic.rest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import indi.felix.easy.core.elastic.client.RequestHelper;
import indi.felix.easy.core.elastic.utool.Const;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
......@@ -29,9 +32,33 @@ public abstract class BaseRest {
public static final String
GET = "GET", POST = "POST", PUT = "PUT", DELETE = "DELETE", HEAD = "HEAD";
public String version;
public String defaultDocType;
protected BaseRest(RestClient restClient) {
this.restClient = restClient;
this.version = getVersion();
if (compareVersion(this.version, "7.0.0") > 0) {
// 7.x 版本中_doc 为默认路径
defaultDocType = "_doc";
} else {
defaultDocType = Const.DOC_TYPE;
}
}
/**
* 获取ES版本号
*
* @return
*/
public String getVersion() {
String json = responseBody(GET, "");
JSONObject jsonObject = JSON.parseObject(json);
JSONObject versionJson = jsonObject != null ? jsonObject.getJSONObject("version") : null;
String version = versionJson != null ? versionJson.getString("number") : null;
return version;
}
public String responseBody(String method, String endpoint) {
......@@ -130,4 +157,44 @@ public abstract class BaseRest {
return response;
}
/**
* 版本号比较
*
* @param v1
* @param v2
* @return 0代表相等,1代表左边大,-1代表右边大
* Utils.compareVersion("1.0.358_20180820090554","1.0.358_20180820090553")=1
*/
public int compareVersion(String v1, String v2) {
if (v1.equals(v2)) {
return 0;
}
String[] version1Array = v1.split("[._]");
String[] version2Array = v2.split("[._]");
int index = 0;
int minLen = Math.min(version1Array.length, version2Array.length);
long diff = 0;
while (index < minLen
&& (diff = Long.parseLong(version1Array[index])
- Long.parseLong(version2Array[index])) == 0) {
index++;
}
if (diff == 0) {
for (int i = index; i < version1Array.length; i++) {
if (Long.parseLong(version1Array[i]) > 0) {
return 1;
}
}
for (int i = index; i < version2Array.length; i++) {
if (Long.parseLong(version2Array[i]) > 0) {
return -1;
}
}
return 0;
} else {
return diff > 0 ? 1 : -1;
}
}
}
package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.EasyEs;
import junit.framework.TestCase;
public class BaseRestTest extends TestCase {
public void testGetVersion() {
EasyEs es = new EasyEs("localhost:9200");
String version = es.rest().getVersion();
System.out.println(version);
}
}
\ No newline at end of file
......@@ -36,6 +36,7 @@
<commons-dbutils.version>1.7</commons-dbutils.version>
<disruptor.version>3.4.2</disruptor.version>
<spring-boot.version>2.4.1</spring-boot.version>
<log4j2.version>2.15.0</log4j2.version>
</properties>
<dependencyManagement>
<dependencies>
......@@ -70,7 +71,21 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册