Maven整合Spring与Solr
首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itszt.DemoSS1</groupId>
<artifactId>DemoSS1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>DemoSS1</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency> <!--solr客户端solrj的依赖 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.1</version>
</dependency> <!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <!--spring框架整合单元测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置solr.properties文件中的solr服务器信息:
solr.Url=http://127.0.0.1:8090/solr/products2
solr.maxRetries=2
solr.connectionTimeout=5000
配置spring-solr-config.xml文件中的solrj客户端信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!--定义solr的server-->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg index="0" value="${solr.Url}"/>
<!-- 设置响应解析器 -->
<property name="parser">
<bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
</property>
<!-- 设置重试次数-->
<property name="maxRetries" value="${solr.maxRetries}"/>
<!-- 建立连接的最长时间 -->
<property name="connectionTimeout" value="${solr.connectionTimeout}"/>
</bean>
</beans>
配置spring-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!--整合solr服务器与客户端信息-->
<context:property-placeholder location="classpath:solr.properties"></context:property-placeholder>
<import resource="classpath:spring-solr-config.xml"></import> <!-- 为了让SpringIoC可以基于注解来做,注解支持-->
<context:annotation-config/>
<!--指明注解的扫描包,即将来去哪个包里找注解
SpringIoC只管扫描service和dao即可
-->
<context:component-scan base-package="com.itszt.DemoSS1">
</context:component-scan>
</beans>
测试代码如下:
package com.itszt.DemoSS1; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set; /**
* maven整合spring与solr
*/
@ContextConfiguration(locations = { "classpath:spring-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestUpdate {
@Autowired
HttpSolrServer httpSolrServer;
@Test
public void testInsertData() throws IOException, SolrServerException { SolrInputDocument solrInputDocument=new SolrInputDocument();
solrInputDocument.addField("id","10086");
solrInputDocument.addField("product_price","998");
solrInputDocument.addField("product_name","大黄音乐"); httpSolrServer.add(solrInputDocument);
httpSolrServer.commit();
} @Test
public void testUpdateData() throws IOException, SolrServerException { SolrInputDocument solrInputDocument=new SolrInputDocument();
solrInputDocument.addField("id","10086");
solrInputDocument.addField("product_price","998888");
solrInputDocument.addField("product_name","大黄音乐555"); httpSolrServer.add(solrInputDocument);
httpSolrServer.commit();
} @Test
public void testDelete() throws IOException, SolrServerException { // httpSolrServer.deleteById("10086");
httpSolrServer.deleteByQuery("product:大黄音乐");
httpSolrServer.commit(); }
@Test
public void testQuery() throws SolrServerException { SolrQuery solrQuery=new SolrQuery();
solrQuery.setQuery("product:挂钩");
QueryResponse queryResponse = httpSolrServer.query(solrQuery); SolrDocumentList results = queryResponse.getResults();
for (SolrDocument result : results) {
Collection<String> fieldNames = result.getFieldNames();
for (String fieldName : fieldNames) { System.out.println(fieldName+" ----> "+result.get(fieldName));
}
}
} @Test
public void testQuery2() throws SolrServerException { SolrQuery solrQuery=new SolrQuery();
solrQuery.setQuery("product_name:家居");
solrQuery.setFields("product_name,product_price");
solrQuery.setFilterQueries("product_price:[10 TO 100]");
solrQuery.setSort("product_price", SolrQuery.ORDER.asc);
solrQuery.setHighlight(true);
solrQuery.set("hl.fl","product_name");
QueryResponse queryResponse = httpSolrServer.query(solrQuery); SolrDocumentList results = queryResponse.getResults(); System.out.println("查询回来的数量:"+results.size());
for (SolrDocument result : results) {
Collection<String> fieldNames = result.getFieldNames();
for (String fieldName : fieldNames) { System.out.println(fieldName+" ----> "+result.get(fieldName));
System.out.println("------------------------------------");
}
}
//获取高亮数据:
System.out.println("获取高亮数据:");
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
Set<String> set1 = highlighting.keySet();
for (String key1 : set1) {
System.out.println("key1---->"+key1);
Map<String, List<String>> map2 = highlighting.get(key1);
System.out.println("map2 = " + map2);
Set<String> set2 = map2.keySet();
for (String key2 : set2) {
System.out.println("key2---->"+key2);
}
}
}
}
Maven整合Spring与Solr的更多相关文章
- Maven 整合 spring profile实现多环境自动切换
Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- SpringBoot整合Spring Data Solr
此文不讲solr相关,只讲整合,内容清单如下 1. maven依赖坐标 2. application.properties配置 3. Java Config配置 1. maven坐标 <depe ...
- 记录一下Maven整合spring,hibernate,strusts2我程序中出的bug
action类如下 package com.itheima.movenweb.action; import java.util.List; import org.apache.struts2.Serv ...
- 基于maven进行spring 和mybatis的整合(Myeclpise)
学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
写在前面的话 承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...
- idea+springmvc+spring+mybatis+maven整合返回json数据webapi
首先看一张目录结构图: : 创建步骤: 1.创建maven webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...
- springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
@_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...
随机推荐
- P2323 [HNOI2006]公路修建问题
题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 输出样例# ...
- 【BZOJ4198】【NOI2015】荷马史诗(贪心,Huffman树)
[BZOJ4198][NOI2015]荷马史诗(贪心,Huffman树) 题面 BZOJ 洛谷 题解 合并果子都是不知道多久以前做过的了.现在才知道原来本质就是一棵哈夫曼树啊. 这题我们仔细研究一下题 ...
- Unity3D手游开发日记(4) - 适合移动平台的热浪扭曲
热浪扭曲效果的实现,分两部分,一是抓图,二是扭曲扰动.其中难点在于抓图的处理,网上的解决方案有两种,在移动平台都有很多问题,只好自己实现了一种新的方案,效果还不错. 网上方案1. 用GrabPass抓 ...
- 洛谷 P1013 进制位 【搜索 + 进制运算】
题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E KL KK E E ...
- 洛谷 3201 [HNOI2009]梦幻布丁 解题报告
3201 [HNOI2009]梦幻布丁 题目描述 \(N\)个布丁摆成一行,进行\(M\)次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为\(1,2,2 ...
- 在 Xamarin.Forms 实现密码输入EntryCell
在 Xamarin.Forms 中,我们通常使用 TableView 来构建输入表单.Xamarin 为我们提供了 EntryCell 用于输入文本,但是其并不支持密码输入,即密码掩码.这里要对 En ...
- Linux之静态库与动态库20160706
所谓静态链接是指把要调用的函数或者过程链接到可执行文件中,成为可执行文件的一部分.当多个程序都调用相同函数时,内存中就会存在这个函数的多个拷贝,这样就浪费了宝贵的内存资源..so文件是共享库文件(动态 ...
- POJ1011 木棒(dfs+剪枝)
问题重述: Description乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始 ...
- lua和C++的交互(1)
/* 以前听的一个故事,当年Java的创造者讲课的时候,一开始先拿一个简单的不能简单的小例子, 不断的扩展,最后成为一个复杂而完美的程序. 一个重要之重要的概念,就是栈.Lua与别的语言交互以及交换数 ...
- 【题解】征途 SDOI 2016 BZOJ 4518
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4518 首先推式子,我们用$x_i$表示第$i$段的路程,$sum$表示总路程,根据方差和平均 ...