文档章节

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

liuyuantao
 liuyuantao
发布于 2016/04/09 09:52
字数 1446
阅读 62
收藏 0

原文地址:http://blog.mkfree.com/posts/40

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

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

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

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

首先我们看看web界面

首页:

点击:创建索引

创建索引成功..

搜索结果:

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

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

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

下面pom.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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("/")
public class SearchController {

	@Autowired
	SearchService searchService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView home() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("home");
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/search")
	public ModelAndView search(@RequestParam("q") String query) {
		Listarticles = searchService.searchsNews(query);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("search");
		mv.addObject("articles", articles);
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/search/create")
	public ModelAndView createInitialData() {
		searchService.builderSearchIndex();
		ModelAndView mv = new ModelAndView("forward:/");
		mv.addObject("message", "文章索引已创建成功!");
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/about")
	public ModelAndView about() {
		ModelAndView mv = new ModelAndView();
		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
 */
@Configuration
public class SpringConfiguration {

	public @Bean
	ClientConfig clientConfig() {

		String connectionUrl = "http://192.168.56.101:9200";

		ClientConfig clientConfig = new ClientConfig();
		LinkedHashSetservers = new LinkedHashSet();
		servers.add(connectionUrl);
		clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
		clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false);
		return clientConfig;
	}

	public @Bean
	JestClient jestClient() {
		JestClientFactory factory = new JestClientFactory();
		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
 */
public class News {

	@JestId
	private int id;
	private String title;
	private String content;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void 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
 */
@Service
public class SearchService {

	@Autowired
	private JestClient jestClient;
	int num = 100000;

	/**
	 * 创建es news索引
	 */
	public void builderSearchIndex() {
		long start = System.currentTimeMillis();
		try {
			// 如果索引存在,删除索引
			DeleteIndex deleteIndex = new DeleteIndex("news");
			jestClient.execute(deleteIndex);

			// 创建索引
			CreateIndex createIndex = new CreateIndex("news");
			jestClient.execute(createIndex);
			// Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)
			Bulk bulk = new Bulk("news", "article");
			// 添加添加100万条假数据去服务端(ES)
			for (int i = 0; i < num; i++) {
				News news = new News();
				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(new Index.Builder(news).build());
			}
			jestClient.execute(bulk);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("创建索引时间:数据量是  " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒");
	}

	/**
	 * 搜索新闻
	 * 
	 * @param param
	 * @return
	 */
	public ListsearchsNews(String param) {
		try {
			long start = System.currentTimeMillis();
			QueryBuilder queryBuilder = QueryBuilders.queryString(param);
			Search search = new Search(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();
		}
		return null;
	}
}
其他的...jsp文件我就不贴出代码了....





本文转载自:http://blog.csdn.net/u013510614/article/details/50838997

liuyuantao
粉丝 5
博文 134
码字总数 27666
作品 0
武汉
高级程序员
私信 提问
springBoot使用elasticsearch

1 安装elasticsearch 2 运行elasticsearch docker run -d -p 9200:9200 -p 9300:9300 -e "ESJAVAOPTS=-Xms216m -Xmx216m" --name ES01 elasticsearch:2.4.0 3 测试安装结果 4 新建一个sprin......

南桥北木
07/15
51
0
ClassNotFoundException: org.elasticsearch.plugins.ActionPlugin

由于驱动和ES版本不一样导致的,参考: As @Roma Khomyshyn mentioned, Spring Data Elasticsearch only support transport client currently, but the version of Elasticsearch server and......

Airship
2018/05/09
41
0
spring-data-elasticsearch 基本案例详解(三)

『 风云说:能分享自己职位的知识的领导是个好领导。 』 运行环境:JDK 7 或 8,Maven 3.0+ 技术栈:SpringBoot 1.5+, Spring Data Elasticsearch 1.5+ ,ElasticSearch 2.3.2 本文提纲 一、...

夜黑人模糊灬
2018/05/13
990
1
ElasticSearch开发问题汇总(不断更新中)

1、Mapping: [译]ElasticSearch数据类型--string类型已死, 字符串数据永生 ElasticSearch动态日期映射 2、Spring Data Elasticsearch: Spring Data Elasticsearch教程...

九州暮云
2018/07/18
246
0
【ElasticSearch】---SpringBoot整合ElasticSearch

SpringBoot整合ElasticSearch 一、基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elasticSearch-5.6.8.jar版本:5.6.8,服务器部署ElasticSearch版本:......

雨点的名字
2018/08/23
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Handler简解

Handler 这里简化一下代码 以便理解 Handler不一定要在主线程建 但如Handler handler = new Handler(); 会使用当前的Looper的, 由于要更新UI 所以最好在主线程 new Handler() { mLooper = Lo...

shzwork
16分钟前
2
0
h5获取摄像头拍照功能

完整代码展示: <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum......

诗书易经
19分钟前
1
0
正向代理和反向代理

文章来源 运维公会:正向代理和反向代理 1、正向代理 (1)服务对象不同 正向代理服务器的服务对象是客户端,可以将客户端和代理服务器看作一个整体。 (2)配置方法不同 需要在客户端配置代...

运维团
35分钟前
2
0
5个避免意外论文重复率高的方法

即使你不是故意抄袭,但你可能在无意中抄袭了别人的论文, 这个叫做意外抄袭,它可能正发生在你身上,如果你不熟悉学术 道德规范,这里将告诉你5个基本的方法来避免意外抄袭。 Tip1 熟悉其他...

论文辅导员
36分钟前
2
0
Maven通过profiles标签读取不同的配置

<profiles> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> ......

时刻在奔跑
42分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部