springboot 集成solr

对于solr的安装和基础入门,今天就不多说了,可以去看之前写的文章。今天主要研究了一下solr如何集成到springboot项目中,实现基本的CRUD。

1、首先向solr中导入mysql中的数据,用于开发测试。在solrhome文件夹中,找到conf文件夹,修改data-config.xml

<dataConfig>
    <!-- 这是mysql的配置,学会jdbc的都应该看得懂 -->
    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/moe-csdp-test" user="root" password="Root123!"/>
    <document>
        <!-- name属性,就代表着一个文档,可以随便命名 -->
        <!-- query是一条sql,代表在数据库查找出来的数据 -->
        <entity name="product" query="select * from news">
            <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) -->
            <field column="id" name="id"/>
            <field column="title" name="headline"/>
            <field column="introduction" name="introduction"/>
			<field column="content" name="detail"/>
			<field column="picture" name="picture"/>
			<field column="upload_time" name="upload_time"/>
			<field column="browser" name="browser"/>
			<field column="isdisplayed" name="isdisplayed"/>
        </entity>
    </document>
</dataConfig>

2、data-config.xml中的name是solr的域,所以必须是在managed-schema文件中配置过相应的域才可以使用。对于managed-schema文件中各个标签的作用,在之前的solr基础中也有过记录。贴出部分代码

<!-- moe-csdp-test数据库news表导入solr-->
	<field name="headline" type="text_ik" indexed="true" stored="true"/>
	<field name="introduction" type="text_ik" indexed="true" stored="true"/>
	<field name="detail" type="text_ik" indexed="true" stored="true"/>
	<field name="picture" type="text_ik" indexed="true" stored="true"/>
	<field name="upload_time" type="text_ik" indexed="true" stored="true"/>
	<field name="browser" type="text_ik" indexed="true" stored="true"/>
	<field name="isdisplayed" type="text_ik" indexed="true" stored="true"/>

这里使用的类型是ik分词器,由于分词器类型一旦设置,想要修改只能是重新导入数据,所以一定要提前想好。

 3、导入maven依赖

<!-- springboot 集成solr-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-solr</artifactId>
		</dependency>

4、配置 文件中配置solr

#solr集成配置
spring.data.solr.host=http://127.0.0.1:9006/solr/core1

 5、创建相应的实体bean,可以是我们springboot+mybatis正常CRUD的实体类,只不过需要指定需要映射的solr核心core。还需要将属性和solr字段对应。

/**
 * 可以在实体类上添加上@SolrDocument(solrCoreName = "custom_core")这个注解,表明这个实体类可以转换成SolrDocument对象,
 * 此外一定不要忘了指定core的名称。如果实体类属性和solr的field对应不上,可以使用@Field(value="field名称")注解,
 * 实现实体类和solr field之间的对应关系。这样在像solr添加User对象的时候就不需要手动将其转换为SolrDocument了
 * @SolrDocument(solrCoreName = "custom_core")
    public class User {

    @Id
    @Field
    private int id;
    @Field
    private String userName;
    @Field
    private String sex;
}
 */
@SolrDocument(solrCoreName = "core1")
public class News implements Serializable {
    @Id
    @Field
    private Integer id;

    @Field(value="headline")
    private String title;

    @Field
    private String introduction;

    @Field
    private String picture;
    
    private String picture1;
    
    private String picture2;

    @Field(value = "upload_time")
    private String uploadTime;

    private Date time;

    @Field
    private Integer browser;

    private Integer type;

    private String source;

    private String author;

    @Field
    private Integer isdisplayed;

    private Integer displayOrder;

    @Field(value = "detail")
    private String content;
    
    public Integer counts;
    
    private String recommend;
    
    private String link;
    
    private String label;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction == null ? null : introduction.trim();
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture == null ? null : picture.trim();
    }

    public String getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(String uploadTime) {
		this.uploadTime = uploadTime;
	}

	public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public Integer getBrowser() {
        return browser;
    }

    public void setBrowser(Integer browser) {
        this.browser = browser;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source == null ? null : source.trim();
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author == null ? null : author.trim();
    }

    public Integer getIsdisplayed() {
        return isdisplayed;
    }

    public void setIsdisplayed(Integer isdisplayed) {
        this.isdisplayed = isdisplayed;
    }

    public Integer getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(Integer displayOrder) {
		this.displayOrder = displayOrder;
	}

	public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }

	public Integer getCounts() {
		return counts;
	}

	public void setCounts(Integer counts) {
		this.counts = counts;
	}

	public String getRecommend() {
		return recommend;
	}

	public void setRecommend(String recommend) {
		this.recommend = recommend;
	}

	public String getPicture1() {
		return picture1;
	}

	public void setPicture1(String picture1) {
		this.picture1 = picture1;
	}

	public String getPicture2() {
		return picture2;
	}

	public void setPicture2(String picture2) {
		this.picture2 = picture2;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
    
}

6、主要使用SolrClient和SolrQuery,我也没有封装,为了方便实践

 

@Controller
@RequestMapping("solr")
public class SolrTestController {

    @Autowired
    private SolrClient solrClient;


    // 根据id查询
    @RequestMapping("queryById")
    @ResponseBody
    public List<News> queryById(int id){

        List<News> newsList = new ArrayList<News>();

        SolrQuery query = new SolrQuery();

        query.setQuery("id:"+String.valueOf(id));
        query.setStart(0);
        query.setRows(10);

        try {
            QueryResponse response = solrClient.query(query);
            SolrDocumentList documentList = response.getResults();
            News news ;
            for(SolrDocument document : documentList){
                news = new News();
                news.setId(Integer.valueOf(document.getFieldValue("id").toString()));
                news.setTitle(document.getFieldValue("headline").toString());
                news.setContent(document.getFieldValue("detail").toString());
                news.setIntroduction(document.getFieldValue("introduction").toString());
                news.setPicture(document.getFieldValue("picture").toString());
                news.setUploadTime(document.getFieldValue("upload_time").toString());
                news.setBrowser(Integer.valueOf(document.getFieldValue("browser").toString()));
                news.setIsdisplayed(Integer.valueOf(document.getFieldValue("isdisplayed").toString()));
                newsList.add(news);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newsList;
    }

    // 查询全部
    @RequestMapping("queryAll")
    @ResponseBody
    public List<News> queryAll() {
        List<News> resultList1 = new ArrayList<News>();

        SolrQuery query = new SolrQuery();

        query.setQuery("*:*");
        query.setStart(0);
        query.setRows(20);

        try {
            QueryResponse response = solrClient.query(query);
            SolrDocumentList documentList = response.getResults();
            News news;
            for(SolrDocument document : documentList){
                news = new News();
                news.setId(Integer.valueOf(document.getFieldValue("id").toString()));
                news.setTitle(document.getFieldValue("headline").toString());
                news.setContent(document.getFieldValue("detail").toString());
                news.setIntroduction(document.getFieldValue("introduction").toString());
                news.setPicture(document.getFieldValue("picture").toString());
                news.setUploadTime(document.getFieldValue("upload_time").toString());
                news.setBrowser(Integer.valueOf(document.getFieldValue("browser").toString()));
                news.setIsdisplayed(Integer.valueOf(document.getFieldValue("isdisplayed").toString()));
                resultList1.add(news);

            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultList1;

    }

    // 向solr中添加对象
    @RequestMapping("add")
    @ResponseBody
    public String add(){

        News news = new News();

        // 这里的id 暂时自定义,后期实际开发时,不用mysql中自增的id,使用uuid
        news.setId(260);
        news.setTitle("这是向solr添加的第一条数据");
        news.setContent("这是向solr添加的第一条数据");
        news.setIntroduction("这是向solr添加的第一条数据");
        news.setPicture("123456");
        news.setUploadTime("2019-08-20");
        news.setBrowser(20);
        news.setIsdisplayed(1);

        try {
            UpdateResponse updateResponse = solrClient.addBean(news);
            int status = updateResponse.getStatus();
            if(status != 0){
                solrClient.rollback();
            }else {
                solrClient.commit();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    // 搜索功能,模糊查询
    @RequestMapping("search")
    @ResponseBody
    public List<News> search(String key) {

        List<News> searchList = new ArrayList<News>();

        SolrQuery query = new SolrQuery();

        query.setQuery("headline:" + key);
        query.setStart(0);
        query.setRows(10);

        try {
            QueryResponse response = solrClient.query(query);
            SolrDocumentList documentList = response.getResults();
            News news;
            for (SolrDocument document : documentList) {
                news = new News();
                news.setId(Integer.valueOf(document.getFieldValue("id").toString()));
                news.setTitle(document.getFieldValue("headline").toString());
                news.setContent(document.getFieldValue("detail").toString());
                news.setIntroduction(document.getFieldValue("introduction").toString());
                news.setPicture(document.getFieldValue("picture").toString());
                news.setUploadTime(document.getFieldValue("upload_time").toString());
                news.setBrowser(Integer.valueOf(document.getFieldValue("browser").toString()));
                news.setIsdisplayed(Integer.valueOf(document.getFieldValue("isdisplayed").toString()));
                searchList.add(news);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return searchList;
    }
}

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值