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

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

上级 9acb1c13
......@@ -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.EsUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.Build;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.RestHighLevelClient;
import indi.felix.easy.core.elastic.client.ClientManage;
import indi.felix.easy.core.elastic.rest.EasyRest;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
......@@ -37,6 +42,8 @@ public class EasyEs {
private volatile EasyEsService esService;
private volatile EasyRest esRest;
private String version;
/**
* 一个集群名称 创建一个实例
*/
......@@ -139,6 +146,8 @@ public class EasyEs {
String newHost = String.join(",",host);
HOST = newHost;
client = getSingleClient(newHost);
getEsInfo();
}
......@@ -146,6 +155,8 @@ public class EasyEs {
public EasyEs(ClientConfigProperties params) {
HOST = params.getHost();
client = getSingleClient(params);
getEsInfo();
}
/**
......@@ -157,6 +168,23 @@ public class EasyEs {
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接口
......
......@@ -106,7 +106,7 @@ public enum DSLEnum {
private String requestType;
private String dslMethod;
// 默认的模板
private String dslTemp;
DSLEnum(String requestType, String dslMethod, String dslTemp) {
......@@ -131,7 +131,11 @@ public enum DSLEnum {
public static DSLEnum getByQuery(String requestType, String dslMethod) {
if (StringUtils.isNotEmpty(requestType) && StringUtils.isNotEmpty(dslMethod)) {
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;
}
}
......
......@@ -11,6 +11,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
......@@ -33,6 +34,7 @@ public abstract class BaseRest {
public static final String
GET = "GET", POST = "POST", PUT = "PUT", DELETE = "DELETE", HEAD = "HEAD";
public String version;
public String defaultDocType;
private final static String DEFAULT_ES_VERSION = "6.1.3";
......@@ -94,6 +96,14 @@ public abstract class BaseRest {
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) {
Response response = null;
String responseBody = null;
......
......@@ -36,7 +36,7 @@ public class ClusterRestApi extends BaseRest {
if(index.length == 1){
newEndpoint = endpoint + "/state/" + index[0];
} else {
newEndpoint = endpoint + "/state/_all/ " + String.join(",", index);
newEndpoint = endpoint + "/state/_all/" + String.join(",", index);
}
return responseBody(defaultMethod, newEndpoint);
......@@ -104,6 +104,7 @@ public class ClusterRestApi extends BaseRest {
return responseBody(defaultMethod, newEndpoint);
}
// todo setSetting
public String setSetting() {
String newEndpoint = endpoint + "/settings";
return responseBody(PUT, newEndpoint);
......
......@@ -191,8 +191,14 @@ public class EasyRest extends BaseRest {
map.put("mappings", mappingMap);
String jsonString = JSON.toJSONString(map);
// 6.x版本无法识别include_type_name参数
// String newEndpoint = index + "?include_type_name=true";
String newEndpoint = "/" + index;
String newEndpoint = "";
if(isAllowIncludeType()){
newEndpoint = index + "?include_type_name=true";
} else {
newEndpoint = "/" + index;
}
Request request = new Request(
"PUT",
newEndpoint);
......
......@@ -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.model.ESFieldTypeEnum;
import indi.felix.easy.core.elastic.model.ElasticConstant;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
......@@ -18,9 +17,6 @@ import java.text.MessageFormat;
import java.util.Collections;
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 IndexRestApi(RestClient restClient) {
......@@ -35,12 +31,19 @@ public class IndexRestApi extends BaseRest {
* 查看集群状态 api
* curl -XHEAD 'localhost:9200/twitter
*
* @param index, 多个索引用英文逗号分隔
* @return
*/
public boolean exists(String index) {
// 6.x版本无法识别include_type_name参数
// String newEndpoint = index + "?include_type_name=true";
String newEndpoint = "/" + index;
String newEndpoint = "";
if (!isAllowIncludeType()) {
// 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);
int code = response.getStatusLine().getStatusCode();
return code == 200;
......@@ -60,10 +63,16 @@ public class IndexRestApi extends BaseRest {
/**
* curl -XGET 'localhost:9200/_mapping
*
* @return
* @return 多个索引用英文逗号分隔
*/
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);
}
......@@ -74,7 +83,14 @@ public class IndexRestApi extends BaseRest {
* @return
*/
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);
}
......@@ -84,7 +100,14 @@ public class IndexRestApi extends BaseRest {
* @return
*/
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);
}
......@@ -116,18 +139,19 @@ public class IndexRestApi extends BaseRest {
return null;
}
public String putDateProperty(String index, String field) {
String newEndpoint = index + "/_mapping/" + DOC_TYPE + "?pretty";
String jsonStr = "{\n" +
" \"properties\": {\n" +
" \" " + field + "\": {\n" +
" \"type\": \"date\"\n" +
" }\n" +
" }\n" +
"}";
HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON);
return responseBody(PUT, newEndpoint, Collections.emptyMap(), entity);
}
// public String putDateProperty(String index, String field) {
// String newEndpoint = index + "/_mapping/" + DOC_TYPE + "?pretty";
//
// String jsonStr = "{\n" +
// " \"properties\": {\n" +
// " \" " + field + "\": {\n" +
// " \"type\": \"date\"\n" +
// " }\n" +
// " }\n" +
// "}";
// HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON);
//
// return responseBody(PUT, newEndpoint, Collections.emptyMap(), entity);
// }
}
......@@ -2,9 +2,11 @@ package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.EasyEs;
import indi.felix.easy.core.elastic.model.ESFieldTypeEnum;
import indi.felix.easy.core.enums.EndpointType;
import junit.framework.TestCase;
import org.junit.Assert;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
......@@ -43,8 +45,8 @@ public class EasyRestTest extends TestCase {
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\"}}]}",
easyEs.rest().search().requestById(testIndexName, "123456"));
// 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().document().deleteDoc(testIndexName, "123457");
......@@ -55,14 +57,14 @@ public class EasyRestTest extends TestCase {
public void testIndex() {
EasyEs easyEs = new EasyEs(ip);
String testIndexName = "test_20210907";
String testIndexName = "test_20210901237";
easyEs.rest().createIndex(testIndexName);
// 获取全部索引
String allMapping = easyEs.rest().index().getMapping();
System.out.println(allMapping);
String mapping = easyEs.rest().index().getMapping(testIndexName);
Assert.assertEquals("{\"test_20210907\":{\"mappings\":{\"doc\":{}}}}", mapping);
Assert.assertEquals("{\"test_20210901237\":{\"mappings\":{\"doc\":{}}}}", mapping);
//putMapping test
......@@ -81,6 +83,71 @@ public class EasyRestTest extends TestCase {
.putMapping(testIndexName, "name11", ESFieldTypeEnum.KEYWORD);
System.out.println(easyEs.rest().index().getMapping(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.
先完成此消息的编辑!
想要评论请 注册