首页 技术 正文
技术 2022年11月6日
0 收藏 827 点赞 611 浏览 4984 个字
 
    <!--solr的maven依赖-->
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>

//pojopackage com.j1.solrj.pojo;import org.apache.solr.client.solrj.beans.Field;public class Foo {    @Field("id")
private String id; @Field("title")
private String title; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} @Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Foo [id=");
builder.append(id);
builder.append(", title=");
builder.append(title);
builder.append("]");
return builder.toString();
}}
//Servicepackage com.j1.solrj.service;import java.util.ArrayList;
import java.util.List;
import java.util.Map;import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;import cn.itcast.solrj.pojo.Foo;public class SolrjService { // 定义http的solr服务
private HttpSolrServer httpSolrServer; public SolrjService(HttpSolrServer httpSolrServer) {
this.httpSolrServer = httpSolrServer;
} /**
* 新增数据到solr服务
*
* @param foo
* @throws Exception
*/
public void add(Foo foo) throws Exception {
this.httpSolrServer.addBean(foo); //添加数据到solr服务器
this.httpSolrServer.commit(); //提交
} public void delete(List<String> ids) throws Exception {
this.httpSolrServer.deleteById(ids);
this.httpSolrServer.commit(); //提交
} public List<Foo> search(String keywords, Integer page, Integer rows) throws Exception {
List<Foo> foos=new ArrayList <Foo> ();
SolrQuery solrQuery = new SolrQuery(); //构造搜索条件
solrQuery.setQuery("title:" + keywords); //搜索关键词
// 设置分页 start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
solrQuery.setStart((Math.max(page, 1) - 1) * rows);
solrQuery.setRows(rows); //是否需要高亮
boolean isHighlighting = !StringUtils.equals("*", keywords) && StringUtils.isNotEmpty(keywords); if (isHighlighting) {
// 设置高亮
solrQuery.setHighlight(true); // 开启高亮组件
solrQuery.addHighlightField("title");// 高亮字段
solrQuery.setHighlightSimplePre("<em>");// 标记,高亮关键字前缀
solrQuery.setHighlightSimplePost("</em>");// 后缀
} // 执行查询
QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
try{
foos = queryResponse.getBeans(Foo.class);
}catch(Exception e){
e.printStackTrace();
} if (isHighlighting) {
// 将高亮的标题数据写回到数据对象中
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
for (Foo foo : foos) {
if (!highlighting.getKey().equals(foo.getId().toString())) {
continue;
}
foo.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
break;
}
}
} return foos;
}}
//test 测试package com.j1.solrj.service;import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;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.impl.XMLResponseParser;
import org.junit.Before;
import org.junit.Test;import cn.itcast.solrj.pojo.Foo;public class SolrjServiceTest { private SolrjService solrjService; private HttpSolrServer httpSolrServer; @Before
public void setUp() throws Exception {
// 在url中指定core名称:taotao
// String url = "http://solr.taotao.com/taotao";
String url = "http://solr.taotao.com/taotao";
HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定义solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间 this.httpSolrServer = httpSolrServer;
solrjService = new SolrjService(httpSolrServer);
} @Test
public void testAdd() throws Exception {
Foo foo = new Foo();
foo.setId("1425954987819");
foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
try{
this.solrjService.add(foo);
}catch(Exception e){
e.printStackTrace();
} } @Test
public void testDelete() throws Exception {
this.solrjService.delete(Arrays.asList("1425954987818"));
} @Test
public void testSearch() throws Exception {
List<Foo> foos = this.solrjService.search("new", 1, 10);
for (Foo foo : foos) {
System.out.println(foo);
}
} @Test
public void testDeleteByQuery() throws Exception{
httpSolrServer.deleteByQuery("title:轻量级Java");
httpSolrServer.commit();
}}

测试结果:

<!–
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco}
–>

Foo [id=1474391928, title=ip6-测试-<em>new</em>2]

Foo [id=1473655354, title=<em>new</em>19 – 小米 红米Note 8G内存移动4G合约增强版 不含合约计划 白色 移动4G]

Foo [id=1473655354, title=new19 – 小米 红米Note 8G内存移动4G合约增强版 不含合约计划 白色 移动4G]

Foo [id=1425955126820, title=<em>new</em> – 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)]

solr java demo 基础入门

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903