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

build1.3.7版本 处理创建索引是 number 不兼容问题

上级 bce63fc6
......@@ -5,7 +5,7 @@
<parent>
<artifactId>easy-es</artifactId>
<groupId>indi.felix.easy</groupId>
<version>1.3.6</version>
<version>1.3.7</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>easy-es-core</artifactId>
......@@ -100,5 +100,10 @@
<version>1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -165,6 +165,7 @@ public class XContentUtils {
break;
case "integer":
case "int":
case "number":
NumberFieldMetadata integetFieldMetadata = (NumberFieldMetadata) baseFieldMetadata;
builder.startObject(curName);
builder.field("type", integetFieldMetadata.getType());
......
......@@ -117,6 +117,7 @@ public abstract class BaseRest {
}
response = restClient.performRequest(request);
responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
......
package indi.felix.easy.core.elastic.rest;
import com.alibaba.fastjson.JSONObject;
import indi.felix.easy.core.elastic.client.RequestHelper;
import indi.felix.easy.core.elastic.client.XContentUtils;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
......@@ -12,16 +26,27 @@ import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static indi.felix.easy.core.elastic.utool.Const.DOC_TYPE;
public class SearchRestApi extends BaseRest {
private SearchRest rest;
private OkHttpClient httpClient;
public SearchRestApi(RestClient restClient) {
super(restClient);
httpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) // default 10s
.writeTimeout(30, TimeUnit.SECONDS) // default 10s
.readTimeout(30, TimeUnit.SECONDS) // default 10s
.build();
rest = new SearchRest();
}
......@@ -30,6 +55,7 @@ public class SearchRestApi extends BaseRest {
String endpoint = "_search";
public SearchRest getRest(){
return rest;
}
......@@ -59,6 +85,150 @@ public class SearchRestApi extends BaseRest {
return responseBody(defaultMethod, newEndpoint, dsl);
}
private URI buildUri(String pathPrefix, String path, Map<String, String> params) {
Objects.requireNonNull(path, "path must not be null");
try {
String fullPath;
if (pathPrefix != null && !pathPrefix.isEmpty()) {
if (pathPrefix.endsWith("/") && path.startsWith("/")) {
fullPath = pathPrefix.substring(0, pathPrefix.length() - 1) + path;
} else if (!pathPrefix.endsWith("/") && !path.startsWith("/")) {
fullPath = pathPrefix + "/" + path;
} else {
fullPath = pathPrefix + path;
}
} else {
fullPath = path;
}
URIBuilder uriBuilder = new URIBuilder(fullPath);
Iterator var5 = params.entrySet().iterator();
while(var5.hasNext()) {
Map.Entry<String, String> param = (Map.Entry)var5.next();
uriBuilder.addParameter((String)param.getKey(), (String)param.getValue());
}
return uriBuilder.build();
} catch (URISyntaxException var7) {
throw new IllegalArgumentException(var7.getMessage(), var7);
}
};
public String httpSearchUrl(String index) {
String newEndpoint = MessageFormat.format("/{0}/_search", index);
try {
Map<String, String> params = Collections.singletonMap("pretty", "true");
org.elasticsearch.client.Request pingRequest = RequestHelper.getRequest("GET", "/", params);
org.elasticsearch.client.Response pingResponse = null;
pingResponse = restClient.performRequest(pingRequest);
String url = pingResponse.getHost().toURI() + newEndpoint;
return url;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public String httpSearch(String index, String dsl) {
String newEndpoint = MessageFormat.format("/{0}/_search", index);
try {
// 先ping一下获取URL
Map<String, String> params = Collections.singletonMap("pretty", "true");
org.elasticsearch.client.Request pingRequest = RequestHelper.getRequest("GET", "/", params);
org.elasticsearch.client.Response pingResponse = restClient.performRequest(pingRequest);
String url = pingResponse.getHost().toURI() + newEndpoint;
try {
HttpClient hclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// 解决中文乱码问题
StringEntity entity = new StringEntity(dsl, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse result11 = hclient.execute(post);
// 请求发送成功,并得到响应
if (result11.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result11.getEntity(), "utf-8");
// 把json字符串转换成json对象
// jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
log.error("post请求提交失败:" + url, e);
}
}
} catch (IOException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(
MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), dsl);
Request request = new Request.Builder()
.url(url) // 可以字符串,也可以是HttpUrl
// .addHeader("Content-Type", "application/json")
.post(body)
.build();
Call call = httpClient.newCall(request);
// call.enqueue(new Callback() {
// @Override
// public void onFailure(Call call, IOException e) {
// log.info("whq登录","失败了");
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// log.info("whq登录",response+"---------response---------");
// log.info("whq登录",response.message()+"---------message---------");
// log.info("whq登录",response.body().toString()+"------------------");
// }
// });
Response resp = null;
resp = call.execute();
System.out.println(resp.body().toString());
if (resp.isSuccessful()) {
//return response;
} else {
throw new IOException("Unexpected code " + resp);
}
if (resp.code() == 200) {
System.out.println("Response: " + resp.body().string());
} else {
// Error handle
System.out.println("Code:" + resp.code() + ", Msg:" + resp.message());
}
} catch (IOException e) {
e.printStackTrace();
}
// return responseBody;
// return responseBody(defaultMethod, newEndpoint, dsl);
return null;
}
/**
* 查询指定ID的文档
* 如果索引设置了routing, 会报routing_missing_exception 异常
......
package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.EasyEs;
import indi.felix.easy.core.elastic.client.ClientConfigProperties;
import junit.framework.TestCase;
import org.elasticsearch.index.query.TermQueryBuilder;
public class SearchRestApiTest extends TestCase {
private static final String ip = "localhost:9200";
public void testAuth() {
ClientConfigProperties properties = new ClientConfigProperties();
properties.setHost(ip);
properties.setUsername("admin");
properties.setPassword("csm@123!");
EasyEs easyEs = new EasyEs(properties);
String re1s = easyEs.rest().cat().indices();
System.out.println(re1s);
Boolean re2s = easyEs.search().indexExist("sub_project_125");
Boolean re3s = easyEs.search().indexExist("sub_project_121115");
System.out.println(re2s);
}
public void testRequestByIds() {
EasyEs easyEs = new EasyEs(ip);
String testIndexName = "edc_index";
......
......@@ -7,7 +7,7 @@
<groupId>indi.felix.easy</groupId>
<artifactId>easy-es</artifactId>
<packaging>pom</packaging>
<version>1.3.6</version>
<version>1.3.7</version>
<modules>
<module>easy-es-core</module>
<!-- <module>easy-es-spring</module>-->
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册