spring data solr 配置

1、在pom.xml中加入所需api

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-solr</artifactId>
        <version>2.1.13.RELEASE</version>

    </dependency>

2、增加applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/solr
    http://www.springframework.org/schema/data/solr/spring-solr.xsd">
<bean id="httpSolrClient" class="org.apache.solr.client.solrj.impl.HttpSolrClient">
    <constructor-arg name="baseURL" value="http://10.10.33.13:8983/solr"/>
    </bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="httpSolrClient"/>
</bean>

</beans>

3、增加bean

public class SolrEntity {


private String id;
private String name;
private String age;
private String context;


public String getId() {
return id;
}


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


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getAge() {
return age;
}


public void setAge(String age) {
this.age = age;
}


public String getContext() {
return context;
}


public void setContext(String context) {
this.context = context;
}


}

4、增加service

@Service
public class SolrService {
@Resource
private SolrTemplate solrTemplate;
public  ScoredPage<SolrEntity > queryAll() {
   // 查询所有
        Query query = new SimpleQuery("*:*");
        //设置分页
        query.setOffset(0); //开始索引(默认0)
        query.setRows(15);   //每页记录数(默认10)
        ScoredPage<SolrEntity > pages = solrTemplate.queryForPage("edu",query, SolrEntity .class);
        System.out.println("pages.getTotalElements() = " + pages.getTotalElements());
        List<SolrEntity > content = pages.getContent();
        for (SolrEntity  item : content) {
            System.out.println("item = " + item);
        }
return pages;
}



}

solr 可以直接把mysql的数据导入,导入后通过以上方式进行查询。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值