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

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. 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. 4.0.0
  5. com.mkfree
  6. soso
  7. war
  8. 1.0-SNAPSHOT
  9. java-jest-sample
  10. UTF-8
  11. 3.1.2.RELEASE
  12. 1.5.10
  13. 1.6.1
  14. 1.6
  15. 4.8.2
  16. 1.6.9
  17.  
  18. sonatype
  19. Sonatype Groups
  20. https://oss.sonatype.org/content/groups/public/
  21.  
  22. io.searchbox
  23. jest
  24. 0.0.2
  25. org.elasticsearch
  26. elasticsearch
  27. 0.19.11
  28.  
  29. org.springframework
  30. spring-context
  31. ${spring.version}
  32. commons-logging
  33. commons-logging
  34. org.springframework
  35. spring-webmvc
  36. ${spring.version}
  37. org.springframework
  38. spring-orm
  39. ${spring.version}
  40. jar
  41. compile
  42. org.springframework
  43. spring-test
  44. ${spring.version}
  45. jar
  46. test
  47.  
  48. org.aspectj
  49. aspectjrt
  50. ${org.aspectj-version}
  51. javax.inject
  52. javax.inject
  53. 1
  54.  
  55. taglibs
  56. standard
  57. 1.1.2
  58. jar
  59. compile
  60. junit
  61. junit
  62. ${junit.version}
  63. test
  64.  
  65. jstl
  66. jstl
  67. 1.1.2
  68. jar
  69. compile
  70. org.springframework
  71. spring-beans
  72. ${spring.version}
  73. org.slf4j
  74. slf4j-log4j12
  75. ${slf4j-log4j12.version}
  76. cglib
  77. cglib
  78. 2.2
  79.  
  80. cloudbees-public-release
  81. http://repository-cloudbees.forge.cloudbees.com/public-release
  82. true
  83. false
  84.  
  85. java-jest-sample
  86. org.mortbay.jetty
  87. maven-jetty-plugin
  88. 6.1.10
  89. 9966
  90. foo
  91. org.apache.maven.plugins
  92. maven-dependency-plugin
  93. 2.3
  94. package
  95. copy
  96. org.mortbay.jetty
  97. jetty-runner
  98. 7.5.4.v20111024
  99. jetty-runner.jar
  100.  
  101. org.apache.maven.plugins
  102. maven-compiler-plugin
  103. 2.3.2
  104. ${java.version}
  105. ${java.version}
  106.  
  107. com.cloudbees
  108. bees-maven-plugin
  109. 1.3.2
  110. ${basedir}/src/main/webapp/WEB-INF/classes/

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研究....

你可能感兴趣的:(elasticsearch结合spring springmvc jest 使用做成WEB架构)