SpringDataSolr( 一 )

SpringDataSolr

Solr是一个开源搜索平台,用于构建搜索应用程序。 它建立在Lucene(全文搜索引擎)之上。 Solr是企业级的,快速的和高度可扩展的。 使用Solr构建的应用程序非常复杂,可提供高性能。

Solr可以和Hadoop一起使用。由于Hadoop处理大量数据,Solr帮助我们从这么大的源中找到所需的信息。不仅限于搜索,Solr也可以用于存储目的。像其他NoSQL数据库一样,它是一种非关系数据存储和处理技术。

​ 总之,Solr是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的数据。

1. solr索引库配置文件 schema.xml

<!-- IK中文分词器 -->
<fieldType name="text_ik" class="solr.TextField">
    <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!-- 配置域 字段 所有name必须为小写 -->
<!-- goodsid type 为 long 表示不分词-->
<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<!--  title type 为Ik 表示这个字段会按照ik分词器规则进行分词 -->
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<!--  type 为 String 也不分词-->
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />
<!-- 复制域 本身不存储数据 映射source中的数据 查询keyword(关键字)就可一起查询title category seller brand -->
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
<!-- 动态域 字段不确定时 定义动态域 -->
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />	
2. service层配置

导入依赖

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

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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemaLocation="http://www.springframework.org/schema/data/solr 
                 http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
                           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">
    
    <!-- solr服务器地址 -->
    <solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr/collection1" />
    
    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>
    
</beans>

pojo类加注解 对应schema的域

public class TbItem implements Serializable{
	//对应主键 id
    @Field
    private Long id;
	//对应item_title
    @Field("item_title")
    private String title;

    @Field("item_price")
    private BigDecimal price;

    @Field("item_image")
    private String image;

    @Field("item_goodsid")
    private Long goodsId;

    @Field("item_category")
    private String category;

    @Field("item_brand")
    private String brand;

    @Field("item_seller")
    private String seller;
    
    //@Dynamic + @Field 表示配置动态域
    //需要一个map属性 map中的key代替* map中的value对应域的值
    //例如 {"内存":"8g","尺寸":"5.5寸"}
    //item_spec_内存:8g
    //item_spec_尺寸:5.5寸
    //适合不确定的数据 因为SKU的规格不确定
    @Dynamic
    @Field("item_spec_*")
    private Map<String,String> specMap;
    public Map<String, String> getSpecMap() {
        return specMap;
    }
    public void setSpecMap(Map<String, String> specMap) {
        this.specMap = specMap;
    }
    
    ....getter setter
    
}
3. 简单API
@Autowired
private SolrTemplate solrTemplate;
// 添加/修改单个对象
solrTemplate.saveBean(javaBean);
// 添加/修改多个对象
solrTemplate.saveBean(javaBeanList);
//按主键查询
TbItem item = solrTemplate.getById(1, TbItem.class);
//按主键删除
solrTemplate.deleteById("1");
//删除所有
Query query=new SimpleQuery("*:*");
solrTemplate.delete(query);

//提交 增删改都需要做
solrTemplate.commit();

//分页查询
Query query=new SimpleQuery("*:*");//查询所有
query.setOffset(20);//开始索引(默认0)
query.setRows(20);//每页记录数(默认10)
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List<TbItem> list = page.getContent();

//条件查询
Query query=new SimpleQuery("*:*");
//item_title中包含2的数据
Criteria criteria=new Criteria("item_title").contains("2");
//并且包含5 一定要重新返回criteria
criteria=criteria.and("item_title").contains("5");		
query.addCriteria(criteria);
//默认分页参数
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List<TbItem> list = page.getContent();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值