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 ...
随机推荐
- 转: 解决【Unable to make the session state request to the session state server】
错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...
- SQL通用优化方案(where优化、索引优化、分页优化、事务优化、临时表优化)
SQL通用优化方案:1. 使用参数化查询:防止SQL注入,预编译SQL命令提高效率2. 去掉不必要的查询和搜索字段:其实在项目的实际应用中,很多查询条件是可有可无的,能从源头上避免的多余功能尽量砍掉, ...
- vector 和数组 之间的转化
1.数组转vector float arrHeight[] = { 1.68,1.72,1.83,2.05,2.35,1.78,2.1,1.96 }; vector<float> vec ...
- vs下取得资源文件中的版本信息
在Windows Mobile和Wince(Windows Embedded CE)下开发的产品,有时候需要显示当前产品的版本信息.一般来说,版本信息是保存在资源文件里面的,例如下图: 为了保持一致, ...
- poj3648 Wedding
Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10975 Accepted: 3355 Specia ...
- 洛谷P1455 搭配购买
题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...
- 使用OAuth2.0协议的github、QQ、weibo第三方登录接入总结
目录 第三方接入总结 OAuth2.0介绍 github OAuth2.0登录接入 国内第三方应用商SDK使用 微博SDK 腾讯QQ SDK passport.js插件使用 安装 相关中间件.路由 返 ...
- 效应量Effect Size
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- Android 基于身份证号的自定义键盘
上图上代码 public class MainActivity extends AppCompatActivity { EditText writebankcard_mobileedit; Custo ...
- Oracle用imp导入dmp 提示遇到 ORACLE 错误 12560 TNS: 协议适配器错误 解决方法
用imp命令导入dmp文件时提示以下错误: IMP-00058: 遇到 ORACLE 错误 12560 : ORA-12560: TNS: 协议适配器错误 : IMP-00000: 未成功终止导入 : ...