Browse Source

elastic search api foundation

huangxy 6 years ago
parent
commit
bd798f9499

+ 16 - 0
study/pom.xml

@@ -25,6 +25,15 @@
     </properties>
 
     <dependencies>
+        <!--搜索引擎-->
+        <dependency>
+            <groupId>io.searchbox</groupId>
+            <artifactId>jest</artifactId>
+            <version>5.3.3</version>
+        </dependency>
+
+        <!--commons start-->
+        <!--通用辅助插件-->
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
@@ -37,6 +46,13 @@
             <artifactId>commons-httpclient</artifactId>
             <version>3.1</version>
         </dependency>
+        <!--commons stop-->
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 28 - 0
study/src/main/java/com/hxy/study/config/ElasticSearchConfig.java

@@ -0,0 +1,28 @@
+package com.hxy.study.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties("elastic-search")
+public class ElasticSearchConfig {
+    private String serverIp;
+
+    private Integer serverPort;
+
+    public String getServerIp() {
+        return serverIp;
+    }
+
+    public void setServerIp(String serverIp) {
+        this.serverIp = serverIp;
+    }
+
+    public Integer getServerPort() {
+        return serverPort;
+    }
+
+    public void setServerPort(Integer serverPort) {
+        this.serverPort = serverPort;
+    }
+}

+ 2 - 1
study/src/main/java/com/hxy/study/config/MainConfig.java

@@ -7,7 +7,8 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class MainConfig {
     @Bean("httpClientHelper")
-    public HttpClientHelper httpClientHelper(){
+    public HttpClientHelper httpClientHelper() {
         return new HttpClientHelper();
     }
+
 }

+ 54 - 0
study/src/main/java/com/hxy/study/controller/dto/Account.java

@@ -0,0 +1,54 @@
+package com.hxy.study.controller.dto;
+
+public class Account {
+
+    private Long id;
+
+    private String user;
+
+    private String title;
+
+    private String desc;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public void setDesc(String desc) {
+        this.desc = desc;
+    }
+
+    @Override
+    public String toString() {
+        return "Account{" +
+                "id=" + id +
+                ", user='" + user + '\'' +
+                ", title='" + title + '\'' +
+                ", desc='" + desc + '\'' +
+                '}';
+    }
+}

+ 5 - 0
study/src/main/java/com/hxy/study/service/SearchService.java

@@ -0,0 +1,5 @@
+package com.hxy.study.service;
+
+public interface SearchService {
+
+}

+ 6 - 2
study/src/main/java/com/hxy/study/service/impl/DemoServiceImpl.java

@@ -13,10 +13,14 @@ import java.util.Map;
 
 @Service
 public class DemoServiceImpl implements DemoService {
-    public static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
+    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
+
+    private final HttpClientHelper httpClientHelper;
 
     @Autowired
-    private HttpClientHelper httpClientHelper;
+    public DemoServiceImpl(HttpClientHelper httpClientHelper) {
+        this.httpClientHelper = httpClientHelper;
+    }
 
     @Override
     public void httpGet() {

+ 4 - 0
study/src/main/java/com/hxy/study/service/impl/SearchServiceImpl.java

@@ -0,0 +1,4 @@
+package com.hxy.study.service.impl;
+
+public class SearchServiceImpl {
+}

+ 3 - 0
study/src/main/resources/application.yaml

@@ -0,0 +1,3 @@
+elastic-search:
+  serverIp: 192.168.232.157
+  serverPort: 9300

+ 94 - 0
study/src/test/java/com/hxy/study/SearchClientTest.java

@@ -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();
+        }
+    }
+}