From aee906706e5ceb5d2bff3e4e4f9d2808de61f2d4 Mon Sep 17 00:00:00 2001 From: felix Date: Tue, 1 Nov 2022 16:55:50 +0800 Subject: [PATCH] =?UTF-8?q?build1.3.7=E7=89=88=E6=9C=AC=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=88=9B=E5=BB=BA=E7=B4=A2=E5=BC=95=E6=98=AF=20number?= =?UTF-8?q?=20=E4=B8=8D=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easy-es-core/pom.xml | 7 +- .../core/elastic/client/XContentUtils.java | 1 + .../easy/core/elastic/rest/BaseRest.java | 1 + .../easy/core/elastic/rest/SearchRestApi.java | 174 +++++++++++++++++- .../core/elastic/rest/SearchRestApiTest.java | 22 +++ pom.xml | 2 +- 6 files changed, 203 insertions(+), 4 deletions(-) diff --git a/easy-es-core/pom.xml b/easy-es-core/pom.xml index 27f7b88..76ed044 100644 --- a/easy-es-core/pom.xml +++ b/easy-es-core/pom.xml @@ -5,7 +5,7 @@ easy-es indi.felix.easy - 1.3.6 + 1.3.7 4.0.0 easy-es-core @@ -100,5 +100,10 @@ 1.2 test + + com.squareup.okhttp3 + okhttp + 3.9.0 + \ No newline at end of file diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java index a146643..22aad7d 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java @@ -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()); diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java index 87b571f..e2c58ca 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java @@ -117,6 +117,7 @@ public abstract class BaseRest { } response = restClient.performRequest(request); + responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java index d0fc6a1..c70a239 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java @@ -1,9 +1,23 @@ 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 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 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 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 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 异常 diff --git a/easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java b/easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java index 6fd67b6..15e6eba 100644 --- a/easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java +++ b/easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java @@ -1,11 +1,33 @@ 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"; diff --git a/pom.xml b/pom.xml index f70fb21..e389765 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ indi.felix.easy easy-es pom - 1.3.6 + 1.3.7 easy-es-core -- GitLab