solr 3.5 配置及应用(三)
程序员文章站
2022-03-23 13:51:44
...
在solr 3.5 配置及应用(二)中在 Document文档和JavaBean相互转换时是比较麻烦的,现在讲用利用DocumentObjectBinder对象将SolrInputDocument 和 JavaBean对象相互转换。
1、在实体类(JavaBean),用@Field来注解字段,注解的名称要与文档的字段名称一致,如@Field("blogId")。如:blogsDO 这也有hibernate的注解要看清楚.
2、增加文档的方法(这方法就简单多了)
3、查询文档
参考:http://blog.csdn.net/chunming8302/article/details/7321501
solr 视频:http://www.icoolxue.com/play/2293
1、在实体类(JavaBean),用@Field来注解字段,注解的名称要与文档的字段名称一致,如@Field("blogId")。如:blogsDO 这也有hibernate的注解要看清楚.
package com.stu.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import org.apache.solr.client.solrj.beans.Field; import com.stu.commons.util.DateUtils; /** * Description: * @author LiChunming * @version V1.0 * @createDateTime:2011-5-17 下午04:38:11 * @Company: MSD. * @Copyright: Copyright (c) 2011 **/ @Entity @Table(name="blogs") public class BlogsDO implements Serializable{ /** * */ private static final long serialVersionUID = -4721368786493126226L; private String id; @Field("blogId") private Integer blogsId; @Field private String title; @Field private String content; //与文档的字段名称是createTime 不一样时要注明,保持一样 @Field("createTime") private Date gmtCreate; @Field private String nickName; @Field private String bTypeId; @Field private String bTypeName; private Date gmtModified; private String revDate; private String sDate=""; private String eDate=""; @Transient //(hibernate 的注解) @Field //在这注解也可以 public String getId() { return id; } public void setId(String id) { this.id = id; } //(hibernate 的注解) @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Integer getBlogsId() { return blogsId; } public void setBlogsId(Integer blogsId) { this.blogsId = blogsId; } 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; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public String getbTypeId() { return bTypeId; } public void setbTypeId(String bTypeId) { this.bTypeId = bTypeId; } @Column(name="gmt_create") public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } @Column(name="gmt_modified") public Date getGmtModified() { return gmtModified; } public void setGmtModified(Date gmtModified) { this.gmtModified = gmtModified; } @Transient public String getRevDate() { if (this.gmtCreate == null) { return null; } return DateUtils.formatDate(gmtCreate, "yyyy-MM-dd HH:mm:ss"); } public void setRevDate(String revDate) { this.revDate = revDate; } @Transient public String getbTypeName() { return bTypeName; } public void setbTypeName(String bTypeName) { this.bTypeName = bTypeName; } @Transient public String getsDate() { return sDate; } public void setsDate(String sDate) { this.sDate = sDate; } @Transient public String geteDate() { return eDate; } public void seteDate(String eDate) { this.eDate = eDate; } @Override public String toString() { return this.id + "#" + this.blogsId + "#" + this.title + "#" + this.content + "#" + this.bTypeId + "#" + this.bTypeName + "#" + this.nickName+"#" + this.gmtCreate; } }
2、增加文档的方法(这方法就简单多了)
public void writerBlog(BlogsDO blog) { // TODO Auto-generated method stub try { blog.setId(SerialNumberUtil.getRandomNum(4)); //获取连接服务 CommonsHttpSolrServer solrServer= SolrServer.getInstance().getServer(); //实例化 DocumentObjectBinder binder = new DocumentObjectBinder(); //javabean对象转化为SolrInputDocument对象 SolrInputDocument doc1 = binder.toSolrInputDocument(blog); solrServer.add(doc1); //提交事务才能生效 solrServer.commit(); } catch (SolrServerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3、查询文档
public List<BlogsDO> searchBlogsListbyBinder( Page page) throws IOException, ParseException { List<BlogsDO> blogList=new ArrayList<BlogsDO>(); CommonsHttpSolrServer solrServer= SolrServer.getInstance().getServer(); SolrQuery sQuery = new SolrQuery(); String para="*:*"; sQuery.setQuery(para); try { QueryResponse response=solrServer.query(sQuery); //获取查询文档 SolrDocumentList list = response.getResults(); //初始化DocumentObjectBinder对象 DocumentObjectBinder binder = new DocumentObjectBinder(); //SolrDocumentList对象转化为 List<BlogsDO>对象 blogList=binder.getBeans(BlogsDO.class, list); //总记录数 Integer counts=(int) list.getNumFound(); page.setCounts(counts); } catch (SolrServerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return blogList; }
参考:http://blog.csdn.net/chunming8302/article/details/7321501
solr 视频:http://www.icoolxue.com/play/2293
推荐阅读
-
Android开发中使用Intent打开第三方应用及验证可用性的方法详解
-
ConfiguraionSource节点及多个配置文件的应用
-
SharePoint 2007图文开发教程(1) 简介,安装,配置及创建Web应用程序
-
Python3.5装饰器原理及应用实例详解
-
SD-WAN 配置及应用模板**(二)
-
三星galaxy tab s 10.5多少钱? 三星tab s 10.5报价及配置参数介绍
-
正则表达式在IOS中的应用及IOS中三种正则表达式的使用与比较
-
.NET Core实战项目之CMS 第三章 入门篇-源码解析配置文件及依赖注入
-
.NET Core实战项目之CMS 第十三章 开发篇-在MVC项目结构介绍及应用第三方UI
-
ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方案