提交 6db3fbbb 编写于 作者: FelixHPP's avatar FelixHPP

处理不同版本不能识别include_type_name报错问题

上级 9acb1c13
...@@ -7,14 +7,19 @@ import indi.felix.easy.core.elastic.client.EasyEsService; ...@@ -7,14 +7,19 @@ import indi.felix.easy.core.elastic.client.EasyEsService;
import indi.felix.easy.core.elastic.utool.Const; import indi.felix.easy.core.elastic.utool.Const;
import indi.felix.easy.core.elastic.utool.EsUtils; import indi.felix.easy.core.elastic.utool.EsUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.Build;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.client.ClusterClient;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import indi.felix.easy.core.elastic.client.ClientManage; import indi.felix.easy.core.elastic.client.ClientManage;
import indi.felix.easy.core.elastic.rest.EasyRest; import indi.felix.easy.core.elastic.rest.EasyRest;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
...@@ -37,6 +42,8 @@ public class EasyEs { ...@@ -37,6 +42,8 @@ public class EasyEs {
private volatile EasyEsService esService; private volatile EasyEsService esService;
private volatile EasyRest esRest; private volatile EasyRest esRest;
private String version;
/** /**
* 一个集群名称 创建一个实例 * 一个集群名称 创建一个实例
*/ */
...@@ -139,6 +146,8 @@ public class EasyEs { ...@@ -139,6 +146,8 @@ public class EasyEs {
String newHost = String.join(",",host); String newHost = String.join(",",host);
HOST = newHost; HOST = newHost;
client = getSingleClient(newHost); client = getSingleClient(newHost);
getEsInfo();
} }
...@@ -146,6 +155,8 @@ public class EasyEs { ...@@ -146,6 +155,8 @@ public class EasyEs {
public EasyEs(ClientConfigProperties params) { public EasyEs(ClientConfigProperties params) {
HOST = params.getHost(); HOST = params.getHost();
client = getSingleClient(params); client = getSingleClient(params);
getEsInfo();
} }
/** /**
...@@ -157,6 +168,23 @@ public class EasyEs { ...@@ -157,6 +168,23 @@ public class EasyEs {
return client.ping(RequestOptions.DEFAULT); return client.ping(RequestOptions.DEFAULT);
} }
public MainResponse getEsInfo(){
MainResponse response = null;
try {
response = client.info(RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
//返回集群的各种信息
ClusterName clusterName = response.getClusterName(); //集群名称
String clusterUuid = response.getClusterUuid(); //群集的唯一标识符
String nodeName = response.getNodeName(); //已执行请求的节点的名称
Version version = response.getVersion(); //已执行请求的节点的版本
Build build = response.getBuild(); //已执行请求的节点的构建信息
return response;
}
/** /**
* 调用高级客户端search接口 * 调用高级客户端search接口
......
...@@ -106,7 +106,7 @@ public enum DSLEnum { ...@@ -106,7 +106,7 @@ public enum DSLEnum {
private String requestType; private String requestType;
private String dslMethod; private String dslMethod;
// 默认的模板
private String dslTemp; private String dslTemp;
DSLEnum(String requestType, String dslMethod, String dslTemp) { DSLEnum(String requestType, String dslMethod, String dslTemp) {
...@@ -131,7 +131,11 @@ public enum DSLEnum { ...@@ -131,7 +131,11 @@ public enum DSLEnum {
public static DSLEnum getByQuery(String requestType, String dslMethod) { public static DSLEnum getByQuery(String requestType, String dslMethod) {
if (StringUtils.isNotEmpty(requestType) && StringUtils.isNotEmpty(dslMethod)) { if (StringUtils.isNotEmpty(requestType) && StringUtils.isNotEmpty(dslMethod)) {
for (DSLEnum dslEnum : DSLEnum.values()) { for (DSLEnum dslEnum : DSLEnum.values()) {
if (dslEnum.getRequestType().equals(requestType) && dslEnum.getDslMethod().equals(dslMethod)) { requestType = requestType.toLowerCase();
dslMethod = dslMethod.toLowerCase();
String curRequestType = dslEnum.getRequestType().toLowerCase();
String curDslMethod = dslEnum.getDslMethod().toLowerCase();
if (curRequestType.equals(requestType) && curDslMethod.equals(dslMethod)) {
return dslEnum; return dslEnum;
} }
} }
......
...@@ -11,6 +11,7 @@ import org.apache.http.HttpEntity; ...@@ -11,6 +11,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity; import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
...@@ -33,6 +34,7 @@ public abstract class BaseRest { ...@@ -33,6 +34,7 @@ public abstract class BaseRest {
public static final String public static final String
GET = "GET", POST = "POST", PUT = "PUT", DELETE = "DELETE", HEAD = "HEAD"; GET = "GET", POST = "POST", PUT = "PUT", DELETE = "DELETE", HEAD = "HEAD";
public String version; public String version;
public String defaultDocType; public String defaultDocType;
private final static String DEFAULT_ES_VERSION = "6.1.3"; private final static String DEFAULT_ES_VERSION = "6.1.3";
...@@ -94,6 +96,14 @@ public abstract class BaseRest { ...@@ -94,6 +96,14 @@ public abstract class BaseRest {
return responseBody; return responseBody;
} }
/**
* 判断版本是否允许用include_type_name
* @return
*/
protected boolean isAllowIncludeType(){
return !(compareVersion(this.version, "7.0.0") > 0 || compareVersion(this.version, "6.3.0") < 0);
}
private String response(String method, String endpoint, Map<String, String> params, Header[] headers, String dsl) { private String response(String method, String endpoint, Map<String, String> params, Header[] headers, String dsl) {
Response response = null; Response response = null;
String responseBody = null; String responseBody = null;
......
...@@ -36,7 +36,7 @@ public class ClusterRestApi extends BaseRest { ...@@ -36,7 +36,7 @@ public class ClusterRestApi extends BaseRest {
if(index.length == 1){ if(index.length == 1){
newEndpoint = endpoint + "/state/" + index[0]; newEndpoint = endpoint + "/state/" + index[0];
} else { } else {
newEndpoint = endpoint + "/state/_all/ " + String.join(",", index); newEndpoint = endpoint + "/state/_all/" + String.join(",", index);
} }
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
...@@ -104,6 +104,7 @@ public class ClusterRestApi extends BaseRest { ...@@ -104,6 +104,7 @@ public class ClusterRestApi extends BaseRest {
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
} }
// todo setSetting
public String setSetting() { public String setSetting() {
String newEndpoint = endpoint + "/settings"; String newEndpoint = endpoint + "/settings";
return responseBody(PUT, newEndpoint); return responseBody(PUT, newEndpoint);
......
...@@ -191,8 +191,14 @@ public class EasyRest extends BaseRest { ...@@ -191,8 +191,14 @@ public class EasyRest extends BaseRest {
map.put("mappings", mappingMap); map.put("mappings", mappingMap);
String jsonString = JSON.toJSONString(map); String jsonString = JSON.toJSONString(map);
// 6.x版本无法识别include_type_name参数 // 6.x版本无法识别include_type_name参数
// String newEndpoint = index + "?include_type_name=true"; String newEndpoint = "";
String newEndpoint = "/" + index;
if(isAllowIncludeType()){
newEndpoint = index + "?include_type_name=true";
} else {
newEndpoint = "/" + index;
}
Request request = new Request( Request request = new Request(
"PUT", "PUT",
newEndpoint); newEndpoint);
......
...@@ -2,7 +2,6 @@ package indi.felix.easy.core.elastic.rest; ...@@ -2,7 +2,6 @@ package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.client.XContentUtils; import indi.felix.easy.core.elastic.client.XContentUtils;
import indi.felix.easy.core.elastic.model.ESFieldTypeEnum; import indi.felix.easy.core.elastic.model.ESFieldTypeEnum;
import indi.felix.easy.core.elastic.model.ElasticConstant;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity; import org.apache.http.nio.entity.NStringEntity;
...@@ -18,9 +17,6 @@ import java.text.MessageFormat; ...@@ -18,9 +17,6 @@ import java.text.MessageFormat;
import java.util.Collections; import java.util.Collections;
import static indi.felix.easy.core.elastic.utool.Const.DOC_TYPE; import static indi.felix.easy.core.elastic.utool.Const.DOC_TYPE;
import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
public class IndexRestApi extends BaseRest { public class IndexRestApi extends BaseRest {
public IndexRestApi(RestClient restClient) { public IndexRestApi(RestClient restClient) {
...@@ -35,12 +31,19 @@ public class IndexRestApi extends BaseRest { ...@@ -35,12 +31,19 @@ public class IndexRestApi extends BaseRest {
* 查看集群状态 api * 查看集群状态 api
* curl -XHEAD 'localhost:9200/twitter * curl -XHEAD 'localhost:9200/twitter
* *
* @param index, 多个索引用英文逗号分隔
* @return * @return
*/ */
public boolean exists(String index) { public boolean exists(String index) {
// 6.x版本无法识别include_type_name参数 String newEndpoint = "";
// String newEndpoint = index + "?include_type_name=true"; if (!isAllowIncludeType()) {
String newEndpoint = "/" + index; // 7.x 版本中_doc 为默认路径
newEndpoint = "/" + index;
} else {
// 6.x版本无法识别include_type_name参数
newEndpoint = index + "?include_type_name=true";
}
Response response = res(HEAD, newEndpoint, Collections.EMPTY_MAP, null); Response response = res(HEAD, newEndpoint, Collections.EMPTY_MAP, null);
int code = response.getStatusLine().getStatusCode(); int code = response.getStatusLine().getStatusCode();
return code == 200; return code == 200;
...@@ -60,10 +63,16 @@ public class IndexRestApi extends BaseRest { ...@@ -60,10 +63,16 @@ public class IndexRestApi extends BaseRest {
/** /**
* curl -XGET 'localhost:9200/_mapping * curl -XGET 'localhost:9200/_mapping
* *
* @return * @return 多个索引用英文逗号分隔
*/ */
public String getMapping() { public String getMapping() {
String newEndpoint = "_mapping"; String newEndpoint = "";
if (!isAllowIncludeType()) {
newEndpoint = "_mapping";
} else {
// 6.x版本无法识别include_type_name参数
newEndpoint = "_mapping?include_type_name=true";
}
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
} }
...@@ -74,7 +83,14 @@ public class IndexRestApi extends BaseRest { ...@@ -74,7 +83,14 @@ public class IndexRestApi extends BaseRest {
* @return * @return
*/ */
public String getMapping(String index) { public String getMapping(String index) {
String newEndpoint = index + "/_mapping"; String newEndpoint = "";
if (!isAllowIncludeType()) {
newEndpoint = index + "/_mapping";
} else {
// 6.x版本无法识别include_type_name参数
newEndpoint = index + "/_mapping?include_type_name=true";
}
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
} }
...@@ -84,7 +100,14 @@ public class IndexRestApi extends BaseRest { ...@@ -84,7 +100,14 @@ public class IndexRestApi extends BaseRest {
* @return * @return
*/ */
public String putMapping(String index, String body) { public String putMapping(String index, String body) {
String newEndpoint = MessageFormat.format("/{0}/_mapping/{1}", index, DOC_TYPE); String newEndpoint = "";
if (!isAllowIncludeType()) {
newEndpoint = MessageFormat.format("/{0}/_mapping/{1}", index, DOC_TYPE);
} else {
// 6.x版本无法识别include_type_name参数
newEndpoint = MessageFormat.format("/{0}/_mapping/{1}?include_type_name=true", index, DOC_TYPE);
}
return responseBody(PUT, newEndpoint, body); return responseBody(PUT, newEndpoint, body);
} }
...@@ -116,18 +139,19 @@ public class IndexRestApi extends BaseRest { ...@@ -116,18 +139,19 @@ public class IndexRestApi extends BaseRest {
return null; return null;
} }
public String putDateProperty(String index, String field) { // public String putDateProperty(String index, String field) {
String newEndpoint = index + "/_mapping/" + DOC_TYPE + "?pretty"; // String newEndpoint = index + "/_mapping/" + DOC_TYPE + "?pretty";
String jsonStr = "{\n" + //
" \"properties\": {\n" + // String jsonStr = "{\n" +
" \" " + field + "\": {\n" + // " \"properties\": {\n" +
" \"type\": \"date\"\n" + // " \" " + field + "\": {\n" +
" }\n" + // " \"type\": \"date\"\n" +
" }\n" + // " }\n" +
"}"; // " }\n" +
HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON); // "}";
// HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON);
return responseBody(PUT, newEndpoint, Collections.emptyMap(), entity); //
} // return responseBody(PUT, newEndpoint, Collections.emptyMap(), entity);
// }
} }
...@@ -2,9 +2,11 @@ package indi.felix.easy.core.elastic.rest; ...@@ -2,9 +2,11 @@ package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.EasyEs; import indi.felix.easy.core.elastic.EasyEs;
import indi.felix.easy.core.elastic.model.ESFieldTypeEnum; import indi.felix.easy.core.elastic.model.ESFieldTypeEnum;
import indi.felix.easy.core.enums.EndpointType;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.junit.Assert; import org.junit.Assert;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -43,8 +45,8 @@ public class EasyRestTest extends TestCase { ...@@ -43,8 +45,8 @@ public class EasyRestTest extends TestCase {
easyEs.rest().document().addDoc(testIndexName, "123457", "123457", map2); easyEs.rest().document().addDoc(testIndexName, "123457", "123457", map2);
Assert.assertEquals("{\"docs\":[{\"_index\":\"test_20210907\",\"_type\":\"_doc\",\"_id\":\"123456\",\"_version\":1,\"_seq_no\":1,\"_primary_term\":1,\"found\":true,\"_source\":{\"name\":\"李四\",\"age\":\"11\"}}]}", // Assert.assertEquals("{\"docs\":[{\"_index\":\"test_20210907\",\"_type\":\"_doc\",\"_id\":\"123456\",\"_version\":1,\"_seq_no\":1,\"_primary_term\":1,\"found\":true,\"_source\":{\"name\":\"李四\",\"age\":\"11\"}}]}",
easyEs.rest().search().requestById(testIndexName, "123456")); // easyEs.rest().search().requestById(testIndexName, "123456"));
easyEs.rest().document().deleteDoc(testIndexName, "123457"); easyEs.rest().document().deleteDoc(testIndexName, "123457");
...@@ -55,14 +57,14 @@ public class EasyRestTest extends TestCase { ...@@ -55,14 +57,14 @@ public class EasyRestTest extends TestCase {
public void testIndex() { public void testIndex() {
EasyEs easyEs = new EasyEs(ip); EasyEs easyEs = new EasyEs(ip);
String testIndexName = "test_20210907"; String testIndexName = "test_20210901237";
easyEs.rest().createIndex(testIndexName); easyEs.rest().createIndex(testIndexName);
// 获取全部索引 // 获取全部索引
String allMapping = easyEs.rest().index().getMapping(); String allMapping = easyEs.rest().index().getMapping();
System.out.println(allMapping); System.out.println(allMapping);
String mapping = easyEs.rest().index().getMapping(testIndexName); String mapping = easyEs.rest().index().getMapping(testIndexName);
Assert.assertEquals("{\"test_20210907\":{\"mappings\":{\"doc\":{}}}}", mapping); Assert.assertEquals("{\"test_20210901237\":{\"mappings\":{\"doc\":{}}}}", mapping);
//putMapping test //putMapping test
...@@ -81,6 +83,71 @@ public class EasyRestTest extends TestCase { ...@@ -81,6 +83,71 @@ public class EasyRestTest extends TestCase {
.putMapping(testIndexName, "name11", ESFieldTypeEnum.KEYWORD); .putMapping(testIndexName, "name11", ESFieldTypeEnum.KEYWORD);
System.out.println(easyEs.rest().index().getMapping(testIndexName)); System.out.println(easyEs.rest().index().getMapping(testIndexName));
easyEs.rest().deleteIndex(testIndexName); easyEs.rest().deleteIndex(testIndexName);
} }
public void testIndexExists(){
EasyEs easyEs = new EasyEs(ip);
String testIndexName = "test_202109077";
easyEs.rest().createIndex(testIndexName);
String testIndexName1 = "test_202109088";
easyEs.rest().createIndex(testIndexName1);
Assert.assertTrue(easyEs.rest().indexExists(testIndexName));
Assert.assertTrue(easyEs.rest().indexExists(testIndexName1));
Assert.assertFalse(easyEs.rest().indexExists("asdada"));
Assert.assertTrue(easyEs.rest().indexExists(testIndexName + "," + testIndexName1));
Assert.assertFalse(easyEs.rest().indexExists(testIndexName + ",aaaa"));
System.out.println(easyEs.rest().index().getMapping());
System.out.println(easyEs.rest().index().getMapping(testIndexName));
System.out.println(easyEs.rest().index().getMapping(testIndexName + "," + testIndexName1));
System.out.println(easyEs.rest().stats());
// cat TEST
System.out.println(easyEs.rest().cat().aliases());
System.out.println(easyEs.rest().cat().allocation());
System.out.println(easyEs.rest().cat().count());
System.out.println(easyEs.rest().cat().health());
System.out.println(easyEs.rest().cat().master());
System.out.println(easyEs.rest().cat().nodeAttrs());
System.out.println(easyEs.rest().cat().shards());
System.out.println(easyEs.rest().cat().fielddata());
System.out.println(easyEs.rest().cat().count(testIndexName));
// cluster TEST
System.out.println(easyEs.rest().cluster().health());
System.out.println(easyEs.rest().cluster().state());
System.out.println(easyEs.rest().cluster().state(testIndexName, testIndexName1));
System.out.println(easyEs.rest().cluster().health(testIndexName));
System.out.println(easyEs.rest().cluster().nodesStats());
System.out.println(easyEs.rest().cluster().nodesStats(testIndexName, testIndexName1));
System.out.println(easyEs.rest().cluster().getSetting());
// DSL
try {
Map<String, Object> res = easyEs.rest().queryDsl("POST", EndpointType.ANALYZER, testIndexName, "{\n" +
" \"analyzer\": \"whitespace\",\n" +
" \"text\": \"The quick brown fox.\"\n" +
"}\n");
System.out.println(res.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void testMain(){
testIndexExists();
testPing();
testIndex();
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册