|
@@ -0,0 +1,94 @@
|
|
|
|
+package com.hxy.study;
|
|
|
|
+
|
|
|
|
+import com.hxy.study.config.ElasticSearchConfig;
|
|
|
|
+import com.hxy.study.controller.dto.Account;
|
|
|
|
+import com.hxy.study.util.HttpCallback;
|
|
|
|
+import com.hxy.study.util.HttpClientHelper;
|
|
|
|
+import io.searchbox.client.JestClient;
|
|
|
|
+import io.searchbox.client.JestClientFactory;
|
|
|
|
+import io.searchbox.client.JestResult;
|
|
|
|
+import io.searchbox.client.config.HttpClientConfig;
|
|
|
|
+import io.searchbox.core.Get;
|
|
|
|
+import org.apache.commons.httpclient.Cookie;
|
|
|
|
+import org.apache.commons.httpclient.HttpMethod;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.runner.RunWith;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.net.InetAddress;
|
|
|
|
+import java.net.UnknownHostException;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
|
+@SpringBootTest
|
|
|
|
+public class SearchClientTest {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ElasticSearchConfig elasticSearchConfig;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private HttpClientHelper httpClientHelper;
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void getInterAddress() {
|
|
|
|
+ try {
|
|
|
|
+ InetAddress inetAddress = InetAddress.getByName(elasticSearchConfig.getServerIp());
|
|
|
|
+ System.out.println();
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test() {
|
|
|
|
+ // Construct a new Jest client according to configuration via factory
|
|
|
|
+ JestClientFactory factory = new JestClientFactory();
|
|
|
|
+ factory.setHttpClientConfig(new HttpClientConfig
|
|
|
|
+ .Builder("http://192.168.232.157:9200")
|
|
|
|
+ .multiThreaded(true)
|
|
|
|
+ //Per default this implementation will create no more than 2 concurrent connections per given route
|
|
|
|
+ .defaultMaxTotalConnectionPerRoute(2)
|
|
|
|
+ // and no more 20 connections in total
|
|
|
|
+ .maxTotalConnection(20)
|
|
|
|
+ .build());
|
|
|
|
+ JestClient client = factory.getObject();
|
|
|
|
+
|
|
|
|
+ Get get = new Get.Builder("accounts", "1").build();
|
|
|
|
+ try {
|
|
|
|
+ JestResult result = client.execute(get);
|
|
|
|
+ List<Account> account = result.getSourceAsObjectList(Account.class);
|
|
|
|
+ System.out.println(account);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void restSearchTest() {
|
|
|
|
+ String url = "http://192.168.232.157:9200/accounts/person/_search";
|
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ Map<String, Object> match = new HashMap<>();
|
|
|
|
+ match.put("desc", "软件 系统");
|
|
|
|
+
|
|
|
|
+ data.put("query", match);
|
|
|
|
+ try {
|
|
|
|
+ httpClientHelper.get(url, null, new HttpCallback() {
|
|
|
|
+ @Override
|
|
|
|
+ public void callback(int httpstatus, HttpMethod httpMethod, Cookie[] cookies) throws IOException {
|
|
|
|
+ System.out.println(httpstatus);
|
|
|
|
+ System.out.println(httpMethod.getResponseBodyAsString());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|