设为首页 收藏本站
查看: 1347|回复: 0

[经验分享] elasticsearch结合spring springmvc jest 使用做成WEB架构

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-5-21 10:21:08 | 显示全部楼层 |阅读模式
做成WEB的架构,当然我不用servlet了...直接使用spring springmvc去做吧...也当是一个ES跟spring springmvc 集成的例子,为了简单起见,我这里不用freemarker了..我直接使用jsp做视图...我也用了bootstrap去管理一个web页面,这样可以省很多时间...
当然我也是用maven了...如果不有熟悉maven的朋友们,可以跟我交流下,大家学习学习...
提示:项目可以用mvn jetty:run 就可以跑起来了...
代码可能有点长,想学习的童鞋们认真些了...
首先我们看看web界面
首页:
DSC0000.jpg
点击:创建索引
DSC0001.jpg
创建索引成功..
DSC0002.jpg
搜索:
DSC0003.jpg
搜索结果:
DSC0004.jpg
好了,下面开始真正的代码....
 
 


 
1.看看项目的目录结构:
DSC0005.jpg
我就贴重要的几个文件代码出来,源代码已经有了,大家可以下载
下面pom.xml代码
 

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mkfree</groupId><artifactId>soso</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>java-jest-sample</name><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>3.1.2.RELEASE</spring.version><slf4j.version>1.5.10</slf4j.version><slf4j-log4j12.version>1.6.1</slf4j-log4j12.version><java.version>1.6</java.version><junit.version>4.8.2</junit.version><org.aspectj-version>1.6.9</org.aspectj-version></properties><repositories><repository><id>sonatype</id><name>Sonatype Groups</name><url>https://oss.sonatype.org/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>io.searchbox</groupId><artifactId>jest</artifactId><version>0.0.2</version></dependency><!-- ES dependency for query builder --><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>0.19.11</version></dependency><!-- Spring Dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version><exclusions><!-- Exclude Commons Logging in favor of SLF4j --><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><type>jar</type><scope>test</scope></dependency><!-- AspectJ --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${org.aspectj-version}</version></dependency><dependency><groupId>javax.inject</groupId><artifactId>javax.inject</artifactId><version>1</version></dependency><!-- View Dependencies --><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version><type>jar</type><scope>compile</scope></dependency><!-- Test Dependencies --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.1.2</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j-log4j12.version}</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2</version></dependency></dependencies><pluginRepositories><pluginRepository><id>cloudbees-public-release</id><url>http://repository-cloudbees.forge.cloudbees.com/public-release</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories><build><finalName>java-jest-sample</finalName><plugins><!-- Plugin to run and test through maven --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.10</version><configuration><stopPort>9966</stopPort><stopKey>foo</stopKey></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.3</version><executions><execution><phase>package</phase><goals><goal>copy</goal></goals><configuration><artifactItems><artifactItem><groupId>org.mortbay.jetty</groupId><artifactId>jetty-runner</artifactId><version>7.5.4.v20111024</version><destFileName>jetty-runner.jar</destFileName></artifactItem></artifactItems></configuration></execution></executions></plugin><!-- Ensures we are compiling at 1.6 level --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin><plugin><groupId>com.cloudbees</groupId><artifactId>bees-maven-plugin</artifactId><version>1.3.2</version></plugin></plugins><outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes/</outputDirectory></build></project>
 
  SearchController 类
 
 

package com.mkfree.soso.action;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView;import com.mkfree.soso.model.News;import com.mkfree.soso.service.SearchService;/**
* 搜索控制
*
* @author hk
*
*         2013-1-16 下午8:26:09
*/
@Controller@RequestMapping("/")publicclassSearchController{@AutowiredSearchService searchService;@RequestMapping(method =RequestMethod.GET)publicModelAndView home(){ModelAndView mv =newModelAndView();
mv
.setViewName("home");return mv;}@RequestMapping(method =RequestMethod.GET, value ="/search")publicModelAndView search(@RequestParam("q")String query){Listarticles= searchService.searchsNews(query);ModelAndView mv =newModelAndView();
mv
.setViewName("search");
mv
.addObject("articles", articles);return mv;}@RequestMapping(method =RequestMethod.GET, value ="/search/create")publicModelAndView createInitialData(){
searchService
.builderSearchIndex();ModelAndView mv =newModelAndView("forward:/");
mv
.addObject("message","文章索引已创建成功!");return mv;}@RequestMapping(method =RequestMethod.GET, value ="/about")publicModelAndView about(){ModelAndView mv =newModelAndView();
mv
.setViewName("about");return mv;}}

  配置客户端 SpringConfiguration 类 
 
 

package com.mkfree.soso.configur;import io.searchbox.client.JestClient;import io.searchbox.client.JestClientFactory;import io.searchbox.client.config.ClientConfig;import io.searchbox.client.config.ClientConstants;import java.util.LinkedHashSet;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/**
* @author hk
*
*         2013-1-16 下午8:49:51
*/
@ConfigurationpublicclassSpringConfiguration{public@BeanClientConfig clientConfig(){String connectionUrl ="http://192.168.56.101:9200";ClientConfig clientConfig =newClientConfig();LinkedHashSetservers=newLinkedHashSet();
servers
.add(connectionUrl);
clientConfig
.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
clientConfig
.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED,false);return clientConfig;}public@BeanJestClient jestClient(){JestClientFactory factory =newJestClientFactory();
factory
.setClientConfig(clientConfig());return factory.getObject();}}

实体类
 

package com.mkfree.soso.model;import io.searchbox.annotations.JestId;/**
* 虚拟news 搜索文章
*
* @author hk
*
*         2013-1-12 下午11:38:29
*/
publicclassNews{@JestIdprivateint id;privateString title;privateString content;publicint getId(){return id;}publicvoid setId(int id){this.id = id;}publicString getTitle(){return title;}publicvoid setTitle(String title){this.title = title;}publicString getContent(){return content;}publicvoid setContent(String content){this.content = content;}}

  搜索服务类
 
 

package com.mkfree.soso.service;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Bulk;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.indices.CreateIndex;import io.searchbox.indices.DeleteIndex;import java.io.IOException;import java.util.List;import org.elasticsearch.index.query.QueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.mkfree.soso.model.News;/**
* es简单服务接口
*
* @author hk
*
*         2013-1-12 下午11:47:16
*/
@ServicepublicclassSearchService{@AutowiredprivateJestClient jestClient;int num =100000;/**
* 创建es news索引
*/
publicvoid builderSearchIndex(){long start =System.currentTimeMillis();try{// 如果索引存在,删除索引DeleteIndex deleteIndex =newDeleteIndex("news");
jestClient
.execute(deleteIndex);// 创建索引CreateIndex createIndex =newCreateIndex("news");
jestClient
.execute(createIndex);// Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)Bulk bulk =newBulk("news","article");// 添加添加100万条假数据去服务端(ES)for(int i =0; i < num; i++){News news =newNews();
news
.setId(i +1);
news
.setTitle("elasticsearch结合spring springmvc jest 使用做成WEB架构"+(i +1));
news
.setContent("oyhk 学习笔记 上一篇文章,说到了先利用jest junit构架一个ES的搜索入门例子...现在准备要做一个ES的WEB架构例子,希望大家都学习学习ES分布式搜索引擎,真的非常不错的...欢迎大家一起讨论讨论... 做成WEB的架构,当然我不用servlet了...直接使用spring springmvc去做吧...也当是一个ES跟spring springmvc 集成的例子,为了简单起见,我这里不用freemarker了..我直接使用jsp做视图... 当然我也是用maven了...如果不有熟悉maven的朋友们,可以跟我交流下,大家学习学习..."+(i +1));
bulk
.addIndex(newIndex.Builder(news).build());}
jestClient
.execute(bulk);}catch(Exception e){
e
.printStackTrace();}long end =System.currentTimeMillis();System.out.println("创建索引时间:数据量是  "+ num +"记录,共用时间 -->> "+(end - start)+" 毫秒");}/**
* 搜索新闻
*
* @param param
* @return
*/
publicListsearchsNews(String param){try{long start =System.currentTimeMillis();QueryBuilder queryBuilder =QueryBuilders.queryString(param);Search search =newSearch(Search.createQueryWithBuilder(queryBuilder.toString()));
search
.addIndex("news");
search
.addType("article");JestResult result = jestClient.execute(search);long end =System.currentTimeMillis();System.out.println("在"+ num +"条记录中,搜索新闻,共用时间 -->> "+(end - start)+" 毫秒");return result.getSourceAsObjectList(News.class);}catch(IOException e){
e
.printStackTrace();}catch(Exception e){
e
.printStackTrace();}returnnull;}}

  come from internet
  源碼下載:見附件

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379525-1-1.html 上篇帖子: elasticsearch结合spring springmvc jest 使用做成WEB架构 下篇帖子: Elasticsearch升级至1.x后API的变化-三
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表