将MySQL中的数据导入到solr索引库
2018-04-20
1308
简介:
利用solrJ向索引库导入数据
需求:将MySQL中的数据导入到solr索引库
定义实体类:
[java] view plain copy
public class SearchItem implements Serializable{
private String id.
利用solrJ向索引库导入数据

需求:将MySQL中的数据导入到solr索引库
定义实体类:
-
public class SearchItem implements Serializable{
-
- private String id;
- private String title;
- private String sell_point;
- private long price;
- private String image;
- private String category_name;
- 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;
- }
- public String getSell_point() {
- return sell_point;
- }
- public void setSell_point(String sell_point) {
- this.sell_point = sell_point;
- }
- public long getPrice() {
- return price;
- }
- public void setPrice(long price) {
- this.price = price;
- }
- public String getImage() {
- return image;
- }
- public String[] getImages() {
- if(image != null && !"".equals(image)) {
- String[] images = image.split(",");
- return images;
- }
- return null;
- }
- public void setImage(String image) {
- this.image = image;
- }
- public String getCategory_name() {
- return category_name;
- }
- public void setCategory_name(String category_name) {
- this.category_name = category_name;
- }
- public SearchItem(String id, String title, String sell_point, long price, String image, String category_name) {
- super();
- this.id = id;
- this.title = title;
- this.sell_point = sell_point;
- this.price = price;
- this.image = image;
- this.category_name = category_name;
- }
- public SearchItem() {
- super();
-
- }
- @Override
- public String toString() {
- return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price
- + ", image=" + image + ", category_name=" + category_name + "]";
- }
-
定义mapper查询数据库:
- List<SearchItem> selectAllItem();
-
<select id="selectAllItem" resultType="com.e3mall.search.SearchItem">
- SELECT
- a.id,
- a.title,
- a.sell_point,
- a.price,
- a.image,
- b.`name` category_name
- FROM
- tb_item a
- LEFT JOIN tb_item_cat b ON a.cid = b.id
- WHERE a.`status`=1
-
</select>
-
</mapper>
利用solrJ向索引库导入数据:
- public E3Result saveSearch(){
- try {
-
- List<SearchItem> selectAllItem = searchMapper.selectAllItem();
- for (SearchItem searchItem : selectAllItem) {
-
- SolrInputDocument document = new SolrInputDocument();
-
- document.addField("id", searchItem.getId());
- document.addField("item_title", searchItem.getTitle());
- document.addField("item_sell_point", searchItem.getSell_point());
- document.addField("item_price", searchItem.getPrice());
- document.addField("item_image", searchItem.getImage());
- document.addField("item_category_name", searchItem.getCategory_name());
-
- solrServer.add(document);
- }
-
- solrServer.commit();
-
- return E3Result.ok();
- } catch (Exception e) {
-
- return E3Result.build(500, "导入失败!");
- }
- }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。