elasticsearch结合spring springmvc jest 使用做成WEB架构

来源:互联网 发布:七天网络阅卷查分入口 编辑:程序博客网 时间:2024/06/05 18:41

elasticsearch结合spring springmvc jest 使用做成WEB架构

oyhk 学习笔记

上一篇文章,说到了先利用jest junit构架一个ES的搜索入门例子...现在准备要做一个ES的WEB架构例子,希望大家都学习学习ES分布式搜索引擎,真的非常不错的...欢迎大家一起讨论讨论...

做成WEB的架构,当然我不用servlet了...直接使用spring springmvc去做吧...也当是一个ES跟spring springmvc 集成的例子,为了简单起见,我这里不用freemarker了..我直接使用jsp做视图...我也用了bootstrap去管理一个web页面,这样可以省很多时间...

当然我也是用maven了...如果不有熟悉maven的朋友们,可以跟我交流下,大家学习学习...

提示:项目可以用mvn jetty:run 就可以跑起来了...

代码可能有点长,想学习的童鞋们认真些了...

首先我们看看web界面

首页:

点击:创建索引

创建索引成功..

搜索:

搜索结果:

好了,下面开始真正的代码....





1.看看项目的目录结构:

我就贴重要的几个文件代码出来,源代码已经有了,大家可以下载

下面pom.xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.mkfree</groupId>
  6. <artifactId>soso</artifactId>
  7. <packaging>war</packaging>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>java-jest-sample</name>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <spring.version>3.1.2.RELEASE</spring.version>
  13. <slf4j.version>1.5.10</slf4j.version>
  14. <slf4j-log4j12.version>1.6.1</slf4j-log4j12.version>
  15. <java.version>1.6</java.version>
  16. <junit.version>4.8.2</junit.version>
  17. <org.aspectj-version>1.6.9</org.aspectj-version>
  18. </properties>
  19.  
  20. <repositories>
  21. <repository>
  22. <id>sonatype</id>
  23. <name>Sonatype Groups</name>
  24. <url>https://oss.sonatype.org/content/groups/public/</url>
  25. </repository>
  26. </repositories>
  27. <dependencies>
  28.  
  29. <dependency>
  30. <groupId>io.searchbox</groupId>
  31. <artifactId>jest</artifactId>
  32. <version>0.0.2</version>
  33. </dependency>
  34. <!-- ES dependency for query builder -->
  35. <dependency>
  36. <groupId>org.elasticsearch</groupId>
  37. <artifactId>elasticsearch</artifactId>
  38. <version>0.19.11</version>
  39. </dependency>
  40.  
  41. <!-- Spring Dependencies -->
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-context</artifactId>
  45. <version>${spring.version}</version>
  46. <exclusions>
  47. <!-- Exclude Commons Logging in favor of SLF4j -->
  48. <exclusion>
  49. <groupId>commons-logging</groupId>
  50. <artifactId>commons-logging</artifactId>
  51. </exclusion>
  52. </exclusions>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-webmvc</artifactId>
  57. <version>${spring.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-orm</artifactId>
  62. <version>${spring.version}</version>
  63. <type>jar</type>
  64. <scope>compile</scope>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-test</artifactId>
  69. <version>${spring.version}</version>
  70. <type>jar</type>
  71. <scope>test</scope>
  72. </dependency>
  73.  
  74. <!-- AspectJ -->
  75. <dependency>
  76. <groupId>org.aspectj</groupId>
  77. <artifactId>aspectjrt</artifactId>
  78. <version>${org.aspectj-version}</version>
  79. </dependency>
  80. <dependency>
  81. <groupId>javax.inject</groupId>
  82. <artifactId>javax.inject</artifactId>
  83. <version>1</version>
  84. </dependency>
  85.  
  86. <!-- View Dependencies -->
  87. <dependency>
  88. <groupId>taglibs</groupId>
  89. <artifactId>standard</artifactId>
  90. <version>1.1.2</version>
  91. <type>jar</type>
  92. <scope>compile</scope>
  93. </dependency>
  94. <!-- Test Dependencies -->
  95. <dependency>
  96. <groupId>junit</groupId>
  97. <artifactId>junit</artifactId>
  98. <version>${junit.version}</version>
  99. <scope>test</scope>
  100. </dependency>
  101.  
  102. <dependency>
  103. <groupId>jstl</groupId>
  104. <artifactId>jstl</artifactId>
  105. <version>1.1.2</version>
  106. <type>jar</type>
  107. <scope>compile</scope>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.springframework</groupId>
  111. <artifactId>spring-beans</artifactId>
  112. <version>${spring.version}</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>org.slf4j</groupId>
  116. <artifactId>slf4j-log4j12</artifactId>
  117. <version>${slf4j-log4j12.version}</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>cglib</groupId>
  121. <artifactId>cglib</artifactId>
  122. <version>2.2</version>
  123. </dependency>
  124. </dependencies>
  125.  
  126. <pluginRepositories>
  127. <pluginRepository>
  128. <id>cloudbees-public-release</id>
  129. <url>http://repository-cloudbees.forge.cloudbees.com/public-release</url>
  130. <releases>
  131. <enabled>true</enabled>
  132. </releases>
  133. <snapshots>
  134. <enabled>false</enabled>
  135. </snapshots>
  136. </pluginRepository>
  137. </pluginRepositories>
  138.  
  139. <build>
  140. <finalName>java-jest-sample</finalName>
  141. <plugins>
  142. <!-- Plugin to run and test through maven -->
  143. <plugin>
  144. <groupId>org.mortbay.jetty</groupId>
  145. <artifactId>maven-jetty-plugin</artifactId>
  146. <version>6.1.10</version>
  147. <configuration>
  148. <stopPort>9966</stopPort>
  149. <stopKey>foo</stopKey>
  150. </configuration>
  151. </plugin>
  152. <plugin>
  153. <groupId>org.apache.maven.plugins</groupId>
  154. <artifactId>maven-dependency-plugin</artifactId>
  155. <version>2.3</version>
  156. <executions>
  157. <execution>
  158. <phase>package</phase>
  159. <goals>
  160. <goal>copy</goal>
  161. </goals>
  162. <configuration>
  163. <artifactItems>
  164. <artifactItem>
  165. <groupId>org.mortbay.jetty</groupId>
  166. <artifactId>jetty-runner</artifactId>
  167. <version>7.5.4.v20111024</version>
  168. <destFileName>jetty-runner.jar</destFileName>
  169. </artifactItem>
  170. </artifactItems>
  171. </configuration>
  172. </execution>
  173. </executions>
  174. </plugin>
  175.  
  176. <!-- Ensures we are compiling at 1.6 level -->
  177. <plugin>
  178. <groupId>org.apache.maven.plugins</groupId>
  179. <artifactId>maven-compiler-plugin</artifactId>
  180. <version>2.3.2</version>
  181. <configuration>
  182. <source>${java.version}</source>
  183. <target>${java.version}</target>
  184. </configuration>
  185. </plugin>
  186.  
  187. <plugin>
  188. <groupId>com.cloudbees</groupId>
  189. <artifactId>bees-maven-plugin</artifactId>
  190. <version>1.3.2</version>
  191. </plugin>
  192. </plugins>
  193. <outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes/</outputDirectory>
  194. </build>
  195. </project>

SearchController 类



  1. package com.mkfree.soso.action;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.servlet.ModelAndView;
  11.  
  12. import com.mkfree.soso.model.News;
  13. import com.mkfree.soso.service.SearchService;
  14.  
  15. /**
  16. * 搜索控制
  17. *
  18. * @author hk
  19. *
  20. * 2013-1-16 下午8:26:09
  21. */
  22. @Controller
  23. @RequestMapping("/")
  24. public class SearchController {
  25.  
  26. @Autowired
  27. SearchService searchService;
  28.  
  29. @RequestMapping(method = RequestMethod.GET)
  30. public ModelAndView home() {
  31. ModelAndView mv = new ModelAndView();
  32. mv.setViewName("home");
  33. return mv;
  34. }
  35.  
  36. @RequestMapping(method = RequestMethod.GET, value = "/search")
  37. public ModelAndView search(@RequestParam("q") String query) {
  38. Listarticles = searchService.searchsNews(query);
  39. ModelAndView mv = new ModelAndView();
  40. mv.setViewName("search");
  41. mv.addObject("articles", articles);
  42. return mv;
  43. }
  44.  
  45. @RequestMapping(method = RequestMethod.GET, value = "/search/create")
  46. public ModelAndView createInitialData() {
  47. searchService.builderSearchIndex();
  48. ModelAndView mv = new ModelAndView("forward:/");
  49. mv.addObject("message", "文章索引已创建成功!");
  50. return mv;
  51. }
  52.  
  53. @RequestMapping(method = RequestMethod.GET, value = "/about")
  54. public ModelAndView about() {
  55. ModelAndView mv = new ModelAndView();
  56. mv.setViewName("about");
  57. return mv;
  58. }
  59. }
配置客户端 SpringConfiguration 类 



  1. package com.mkfree.soso.configur;
  2.  
  3. import io.searchbox.client.JestClient;
  4. import io.searchbox.client.JestClientFactory;
  5. import io.searchbox.client.config.ClientConfig;
  6. import io.searchbox.client.config.ClientConstants;
  7.  
  8. import java.util.LinkedHashSet;
  9.  
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12.  
  13. /**
  14. * @author hk
  15. *
  16. * 2013-1-16 下午8:49:51
  17. */
  18. @Configuration
  19. public class SpringConfiguration {
  20.  
  21. public @Bean
  22. ClientConfig clientConfig() {
  23.  
  24. String connectionUrl = "http://192.168.56.101:9200";
  25.  
  26. ClientConfig clientConfig = new ClientConfig();
  27. LinkedHashSetservers = new LinkedHashSet();
  28. servers.add(connectionUrl);
  29. clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
  30. clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false);
  31. return clientConfig;
  32. }
  33.  
  34. public @Bean
  35. JestClient jestClient() {
  36. JestClientFactory factory = new JestClientFactory();
  37. factory.setClientConfig(clientConfig());
  38. return factory.getObject();
  39. }
  40. }

实体类


  1. package com.mkfree.soso.model;
  2.  
  3. import io.searchbox.annotations.JestId;
  4.  
  5. /**
  6. * 虚拟news 搜索文章
  7. *
  8. * @author hk
  9. *
  10. * 2013-1-12 下午11:38:29
  11. */
  12. public class News {
  13.  
  14. @JestId
  15. private int id;
  16. private String title;
  17. private String content;
  18.  
  19. public int getId() {
  20. return id;
  21. }
  22.  
  23. public void setId(int id) {
  24. this.id = id;
  25. }
  26.  
  27. public String getTitle() {
  28. return title;
  29. }
  30.  
  31. public void setTitle(String title) {
  32. this.title = title;
  33. }
  34.  
  35. public String getContent() {
  36. return content;
  37. }
  38.  
  39. public void setContent(String content) {
  40. this.content = content;
  41. }
  42.  
  43. }
搜索服务类



  1. package com.mkfree.soso.service;
  2.  
  3. import io.searchbox.client.JestClient;
  4. import io.searchbox.client.JestResult;
  5. import io.searchbox.core.Bulk;
  6. import io.searchbox.core.Index;
  7. import io.searchbox.core.Search;
  8. import io.searchbox.indices.CreateIndex;
  9. import io.searchbox.indices.DeleteIndex;
  10.  
  11. import java.io.IOException;
  12. import java.util.List;
  13.  
  14. import org.elasticsearch.index.query.QueryBuilder;
  15. import org.elasticsearch.index.query.QueryBuilders;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18.  
  19. import com.mkfree.soso.model.News;
  20.  
  21. /**
  22. * es简单服务接口
  23. *
  24. * @author hk
  25. *
  26. * 2013-1-12 下午11:47:16
  27. */
  28. @Service
  29. public class SearchService {
  30.  
  31. @Autowired
  32. private JestClient jestClient;
  33. int num = 100000;
  34.  
  35. /**
  36. * 创建es news索引
  37. */
  38. public void builderSearchIndex() {
  39. long start = System.currentTimeMillis();
  40. try {
  41. // 如果索引存在,删除索引
  42. DeleteIndex deleteIndex = new DeleteIndex("news");
  43. jestClient.execute(deleteIndex);
  44.  
  45. // 创建索引
  46. CreateIndex createIndex = new CreateIndex("news");
  47. jestClient.execute(createIndex);
  48. // Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)
  49. Bulk bulk = new Bulk("news", "article");
  50. // 添加添加100万条假数据去服务端(ES)
  51. for (int i = 0; i < num; i++) {
  52. News news = new News();
  53. news.setId(i + 1);
  54. news.setTitle("elasticsearch结合spring springmvc jest 使用做成WEB架构" + (i + 1));
  55. news.setContent("oyhk 学习笔记 上一篇文章,说到了先利用jest junit构架一个ES的搜索入门例子...现在准备要做一个ES的WEB架构例子,希望大家都学习学习ES分布式搜索引擎,真的非常不错的...欢迎大家一起讨论讨论... 做成WEB的架构,当然我不用servlet了...直接使用spring springmvc去做吧...也当是一个ES跟spring springmvc 集成的例子,为了简单起见,我这里不用freemarker了..我直接使用jsp做视图... 当然我也是用maven了...如果不有熟悉maven的朋友们,可以跟我交流下,大家学习学习..."
  56. + (i + 1));
  57. bulk.addIndex(new Index.Builder(news).build());
  58. }
  59. jestClient.execute(bulk);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. long end = System.currentTimeMillis();
  64. System.out.println("创建索引时间:数据量是 " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒");
  65. }
  66.  
  67. /**
  68. * 搜索新闻
  69. *
  70. * @param param
  71. * @return
  72. */
  73. public ListsearchsNews(String param) {
  74. try {
  75. long start = System.currentTimeMillis();
  76. QueryBuilder queryBuilder = QueryBuilders.queryString(param);
  77. Search search = new Search(Search.createQueryWithBuilder(queryBuilder.toString()));
  78. search.addIndex("news");
  79. search.addType("article");
  80. JestResult result = jestClient.execute(search);
  81. long end = System.currentTimeMillis();
  82. System.out.println("在" + num + "条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒");
  83. return result.getSourceAsObjectList(News.class);
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return null;
  90. }
  91. }
其他的...jsp文件我就不贴出代码了....


这博客希望对大家有用...进一步对ES研究....

原创粉丝点击