将MySQL中的数据导入到solr索引库

利用solrJ向索引库导入数据http://www.bieryun.com/3229.html

将MySQL中的数据导入到solr索引库

需求:将MySQL中的数据导入到solr索引库
定义实体类:

[java] view plain copy

  1. public class SearchItem implements Serializable{
  2.  
  3.     private String id;
  4.     private String title;
  5.     private String sell_point;
  6.     private long price;
  7.     private String image;
  8.     private String category_name;
  9.     public String getId() {
  10.         return id;
  11.     }
  12.     public void setId(String id) {
  13.         this.id = id;
  14.     }
  15.     public String getTitle() {
  16.         return title;
  17.     }
  18.     public void setTitle(String title) {
  19.         this.title = title;
  20.     }
  21.     public String getSell_point() {
  22.         return sell_point;
  23.     }
  24.     public void setSell_point(String sell_point) {
  25.         this.sell_point = sell_point;
  26.     }
  27.     public long getPrice() {
  28.         return price;
  29.     }
  30.     public void setPrice(long price) {
  31.         this.price = price;
  32.     }
  33.     public String getImage() {
  34.         return image;
  35.     }
  36.     public String[] getImages() {
  37.         if(image != null && !"".equals(image)) {
  38.             String[] images = image.split(",");
  39.             return images;
  40.         }
  41.         return null;
  42.     }
  43.     public void setImage(String image) {
  44.         this.image = image;
  45.     }
  46.     public String getCategory_name() {
  47.         return category_name;
  48.     }
  49.     public void setCategory_name(String category_name) {
  50.         this.category_name = category_name;
  51.     }
  52.     public SearchItem(String id, String title, String sell_point, long price, String image, String category_name) {
  53.         super();
  54.         this.id = id;
  55.         this.title = title;
  56.         this.sell_point = sell_point;
  57.         this.price = price;
  58.         this.image = image;
  59.         this.category_name = category_name;
  60.     }
  61.     public SearchItem() {
  62.         super();
  63.         // TODO Auto-generated constructor stub
  64.     }
  65.     @Override
  66.     public String toString() {
  67.         return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price
  68.                 + ", image=" + image + ", category_name=" + category_name + "]";
  69.     }
  70.  
定义mapper查询数据库:
 

[java] view plain copy

  1. List<SearchItem> selectAllItem();

[html] view plain copy

  1. <select id="selectAllItem" resultType="com.e3mall.search.SearchItem">
  2. SELECT
  3.     a.id,
  4.     a.title,
  5.     a.sell_point,
  6.     a.price,
  7.     a.image,
  8.     b.`name` category_name
  9. FROM
  10.     tb_item a
  11. LEFT JOIN tb_item_cat b ON a.cid = b.id
  12. WHERE a.`status`=1
  13. </select>
  14. </mapper>
利用solrJ向索引库导入数据:
 

[java] view plain copy

  1. /**
  2.      * 向索引库添加数据
  3.      */
  4.     public E3Result saveSearch(){
  5.         try {
  6.         //从数据库中查询数据
  7.         List<SearchItem> selectAllItem = searchMapper.selectAllItem();
  8.         for (SearchItem searchItem : selectAllItem) {
  9.             // 创建一个文档对象SolrInputDocument
  10.             SolrInputDocument document = new SolrInputDocument();
  11.             // 向文档对象中添加域,文档中必须包含一个id域,所有的域的名称必须在schema.xml中定义
  12.             document.addField("id", searchItem.getId());
  13.             document.addField("item_title", searchItem.getTitle());
  14.             document.addField("item_sell_point", searchItem.getSell_point());
  15.             document.addField("item_price", searchItem.getPrice());
  16.             document.addField("item_image", searchItem.getImage());
  17.             document.addField("item_category_name", searchItem.getCategory_name());
  18.             // 把文档写入索引库
  19.             solrServer.add(document);
  20.         }
  21.         // 提交
  22.         solrServer.commit();
  23.         //返回成功
  24.         return E3Result.ok();
  25.         } catch (Exception e) {
  26.             // TODO: handle exception
  27.             return E3Result.build(500"导入失败!");
  28.         }
  29.     }
上一篇:集合的总结


下一篇:C#调用非托管的代码