首先,在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的更多相关文章

  1. Maven 整合 spring profile实现多环境自动切换

    Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...

  2. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

  3. springboot+maven整合spring security

    springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...

  4. SpringBoot整合Spring Data Solr

    此文不讲solr相关,只讲整合,内容清单如下 1. maven依赖坐标 2. application.properties配置 3. Java Config配置 1. maven坐标 <depe ...

  5. 记录一下Maven整合spring,hibernate,strusts2我程序中出的bug

    action类如下 package com.itheima.movenweb.action; import java.util.List; import org.apache.struts2.Serv ...

  6. 基于maven进行spring 和mybatis的整合(Myeclpise)

    学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...

  7. Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM

    写在前面的话   承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...

  8. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

  9. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

随机推荐

  1. "流量监管"和"流量整形"的区别

    "流量监管" (Traffic Policing) 就是对流量进行控制,通过监督进入交换机端口的流量速率,对超出部分的流量进行"惩罚" (采用监管方式时是直接丢 ...

  2. Period II FZU - 1901(拓展kmp)

    拓展kmp板题 emm...我比较懒 最后一个字母进了vector两个1  不想改了...就加了个去重... 哈哈 #include <iostream> #include <cst ...

  3. Shortest Prefixes POJ - 2001(统计次数)

    题意: 输出每个单词的缩写  使得每个单词 互不相同.. 解析: 统计每个前出现的次数...然后在查询的时候  遇到次数为1的返回即可.. #include <iostream> #inc ...

  4. 转:Python 文本挖掘:使用gensim进行文本相似度计算

    Python使用gensim进行文本相似度计算 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101895642665/ 在文本处理 ...

  5. 【刷题】BZOJ 1195 [HNOI2006]最短母串

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...

  6. The meterprter basic commonds

    Using Meterpeter commands Since the Meterpreter provides awhole new environment, we will cover some ...

  7. Yura

    Portal --> broken qwq Description ​  给你一个长度为\(n\)的序列\(a\)和一个正整数\(k\),求满足如下条件的区间\([l,r]\)的数量:\((\s ...

  8. 【BZOJ 4449】[Neerc2015]Distance on Triangulation 多边形分治结构

    这题好神啊……正解方向是分治,据我所知的分治方法有:I.离线后直接对多边形以及所有的询问进行分治 II.建立多边形的分治结构(对于三角形来说类似线段树,对于对角线来说类似平衡树),然后每次在这个分治结 ...

  9. C++中添加配置文件读写方法

    比如有一个工程,一些变量有可能需要不时的修改,这时候可以通过从配置文件中读取该数值,需要修改时只需要修改配位文件即可. 比如有一个这样的变量m_nTest; 我么可以写两个函数ReadConfig() ...

  10. HDU 4584 splay

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...